-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageDifference.py
42 lines (31 loc) · 1.19 KB
/
ImageDifference.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from skimage.measure import compare_ssim
import argparse
import imutils
import cv2
imagePath =r'C:\Users\sachin13390\Desktop\InputImage5.jpg'
templatePath =r'C:\Users\sachin13390\Desktop\InputImage3.jpg'
imageA = cv2.imread(imagePath)
imageB = cv2.imread(templatePath)
grayA = cv2.cvtColor(imageA, cv2.COLOR_BGR2GRAY)
grayB = cv2.cvtColor(imageB, cv2.COLOR_BGR2GRAY)
(score, diff) = compare_ssim(grayA, grayB, full=True)
diff = (diff * 255).astype("uint8")
print("SSIM: {}".format(score))
thresh = cv2.threshold(diff, 0, 255,
cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if imutils.is_cv2() else cnts[1]
for c in cnts:
(x, y, w, h) = cv2.boundingRect(c)
cv2.rectangle(imageA, (x, y), (x + w, y + h), (0, 0, 255), 2)
cv2.rectangle(imageB, (x, y), (x + w, y + h), (0, 0, 255), 2)
# show the output images
cv2.imshow("Original", imageA)
cv2.imshow("Modified", imageB)
cv2.imshow("Diff", diff)
cv2.imshow("Thresh", thresh)
##cv2.imwrite(r'C:\Users\sachin13390\Desktop\InputImage5.jpg',imageA)
##cv2.imwrite(r'C:\Users\sachin13390\Desktop\InputImage3.jpg',imageB)
cv2.waitKey(0)
cv2.destroyAllWindows()