-
Notifications
You must be signed in to change notification settings - Fork 0
/
diff2.py
142 lines (123 loc) · 4.05 KB
/
diff2.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# import the necessary packages
from skimage.measure import compare_ssim
import argparse
import imutils
import cv2
x1 = 0
y1 = 0
x2 = 0
y2 = 0
imageB_copy = cv2.imread("./before_pic/2020_12_13_16_19_52.jpg")
draw_flag = 0
flag = 0
def draw_rectangle(event, x, y, flags, param):
global imageB_copy
global x1
global y1
global x2
global y2
global draw_flag
global flag
if event == cv2.EVENT_LBUTTONUP:
if flag == 0:
x1 = x
y1 = y
cv2.line(imageB_copy, (x1, y1), (x1 + 10, y1), (255, 0, 0), 1)
cv2.line(imageB_copy, (x1, y1), (x1, y1 + 10), (255, 0, 0), 1)
flag = 1
elif flag == 1:
x2 = x
y2 = y
cv2.rectangle(imageB_copy, (x1, y1), (x2, y2), (255, 0, 0), 2)
draw_flag = 1
flag = 0
def compare(before_pic, after_pic):
global imageB_copy
global x1
global y1
global x2
global y2
global flag
x_final = 0
y_final = 0
w_final = 0
h_final = 0
# load the two input images
imageA = cv2.imread('./before_pic/' + before_pic + '.jpg')
imageB = cv2.imread('./after_pic/' + after_pic + '.jpg')
imageB_copy2 = imageB.copy()
# convert the images to grayscale
grayA = cv2.cvtColor(imageA, cv2.COLOR_BGR2GRAY)
grayB = cv2.cvtColor(imageB, cv2.COLOR_BGR2GRAY)
# compute the Structural Similarity Index (SSIM) between the two
# images, ensuring that the difference image is returned
(score, diff) = compare_ssim(grayA, grayB, full=True)
diff = (diff * 255).astype("uint8")
#print("SSIM: {}".format(score))
# threshold the difference image, followed by finding contours to
# obtain the regions of the two input images that differ
thresh = cv2.threshold(diff, 30, 255, cv2.THRESH_BINARY_INV)[1]
cnts = cv2.findContours(
thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
sum = 0
# loop over the contours
for c in cnts:
# compute the bounding box of the contour and then draw the
# bounding box on both input images to represent where the two
# images differ
(x, y, w, h) = cv2.boundingRect(c)
if w > 80 and h > 10:
#print("width:" + str(w))
#print("height:" + str(h))
if w + h > sum:
x_final = x
y_final = y
w_final = w
h_final = h
sum = w + h
if x_final != 0:
cv2.rectangle(imageA, (x_final, y_final), (x_final +
w_final, y_final + h_final), (0, 0, 255), 2)
cv2.rectangle(imageB, (x_final, y_final), (x_final +
w_final, y_final + h_final), (0, 0, 255), 2)
# show the output images
#cv2.imshow("A", grayA)
#cv2.imshow("B", grayB)
#cv2.imshow("Original", imageA)
#cv2.imshow("Compare", imageB)
#cv2.imshow("Diff", diff)
#cv2.imshow("Thresh", thresh)
imageB_copy = imageB.copy()
cv2.namedWindow('Compare')
cv2.setMouseCallback('Compare', draw_rectangle)
while(1):
cv2.imshow("Compare", imageB_copy)
k = cv2.waitKey(1)
if k == 121:
break
elif k == 110:
imageB_copy = imageB.copy()
flag = 0
continue
# if (imageB == imageB_orig).all():
# print("True")
# else:
# print("False")
cv2.destroyAllWindows()
if draw_flag == 1:
w_final = x2 - x1
h_final = y2 - y1
x_final = x1
y_final = y1
cv2.rectangle(imageB_copy2, (x_final, y_final), (x_final +
w_final, y_final + h_final), (0, 0, 255), 2)
cv2.imwrite("./location/" + after_pic + ".jpg", imageB_copy2)
return after_pic
#before_pic = "2020_12_13_16_19_52"
#after_pic = "2020_12_13_16_20_39"
#x, y, w, h = compare(before_pic, after_pic)
#print("x:" + str(x))
#print("y:" + str(y))
#print("w:" + str(w))
#print("h:" + str(h))