-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsrc.py
35 lines (26 loc) · 1.24 KB
/
src.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
import cv2 as cv
import imutils
from skimage.measure import compare_ssim
imgA = cv.imread("sample_images/test1.png", cv.IMREAD_GRAYSCALE)
imgB = cv.imread("sample_images/test1_modified.png", cv.IMREAD_GRAYSCALE)
# apply Structural Similarity Index Method (SSIM) between the two images
(similarity, difference_img) = compare_ssim(imgA, imgB, full=True)
# shift the range of values form (0,1) to (0,255)
difference_img = (difference_img * 255).astype("uint8")
# apply OTSU's threshold algorithm along with GLOBAL_BINARY_INVERSE threshold algorithm
thresholded = cv.threshold(difference_img, 0, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU)[1]
# extract the contours from the thresholded image
contours = cv.findContours(thresholded.copy(), cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
# get the coordinates of contours
contours = imutils.grab_contours(contours)
# draw rectangles around each contour
for contour in contours:
(x, y, w, h) = cv.boundingRect(contour)
cv.rectangle(imgA, (x, y), (x + w, y + h), 0, 2)
cv.rectangle(imgB, (x, y), (x + w, y + h), 0, 2)
cv.imshow("original", imgA)
cv.imshow("modified", imgB)
cv.imshow("difference", difference_img)
print("similarity between the two images:", similarity * 100.0)
cv.waitKey(0)
cv.destroyAllWindows()