-
Notifications
You must be signed in to change notification settings - Fork 3
/
img_diff1.py
59 lines (48 loc) · 1.72 KB
/
img_diff1.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
#===========================#
# Spot the Differences #
# with Python and OpenCV #
#===========================#
# Konstantinos Thanos #
# Mathematician, Msc #
#===========================#
# Import packages
import cv2
import imutils
import numpy as np
# Load the two images
img1 = cv2.imread('images/camels1.jpg')
img2 = cv2.imread("images/camels2.jpg")
# Resize images if necessary
img1 = cv2.resize(img1, (600,360))
img2 = cv2.resize(img2, (600,360))
img_height = img1.shape[0]
# Grayscale
gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
# Find the difference between the two images
# Calculate absolute difference between two arrays
diff = cv2.absdiff(gray1, gray2)
cv2.imshow("diff(img1, img2)", diff)
# Apply threshold. Apply both THRESH_BINARY and THRESH_OTSU
thresh = cv2.threshold(diff, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
cv2.imshow("Threshold", thresh)
# Dilation
kernel = np.ones((5,5), np.uint8)
dilate = cv2.dilate(thresh, kernel, iterations=2)
cv2.imshow("Dilate", dilate)
# Calculate contours
contours = cv2.findContours(dilate.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = imutils.grab_contours(contours)
for contour in contours:
if cv2.contourArea(contour) > 100:
# Calculate bounding box around contour
x, y, w, h = cv2.boundingRect(contour)
# Draw rectangle - bounding box on both images
cv2.rectangle(img1, (x, y), (x+w, y+h), (0,0,255), 2)
cv2.rectangle(img2, (x, y), (x+w, y+h), (0,0,255), 2)
# Show images with rectangles on differences
x = np.zeros((img_height,10,3), np.uint8)
result = np.hstack((img1, x, img2))
cv2.imshow("Differences", result)
cv2.waitKey(0)
cv2.destroyAllWindows()