-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPictureComparator.py
More file actions
153 lines (125 loc) · 4.61 KB
/
PictureComparator.py
File metadata and controls
153 lines (125 loc) · 4.61 KB
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
143
144
145
146
147
148
149
150
151
152
import sys
import cv2
import numpy as np
class PictureComparator:
"""
A class for comparing two images, pixel by pixel.
"""
TITLE = "Image grid difference app"
TEXT_COLOR = (0, 0, 255)
def __init__(self, img1 = None, img2 = None):
"""
Initializes a class instance with images to be compared.
Args:
img1 (numpy.ndarray) : first image
img2 (numpy.ndarray) : second image
"""
self.img1 = img1
self.img2 = img2
if self.img1.shape != self.img2.shape:
raise ValueError("Images are different size")
self.diff_avg = None
self.tolerance = None
def setTolerance(self, tol):
"""
Sets pixel comparison tolerance.
Args:
tol (int) : Pixel comparison tolerance
"""
if tol is not None and (tol > 255 or tol < 0):
raise ValueError("Tolerance should be in range [0, 255]")
self.tolerance = tol
def getDiffImg(self):
"""
Returns compared image with differences marked red and printed difference percentage.
Returns:
Compared image
"""
diff_percentage = self.calculateDiffPixels()
masked_img = self.mergeDiffImage()
masked_img[self.diff_avg != 0, :2] = 0
diff_perc_txt = "{:.2f}%".format(round(diff_percentage, 2))
diff_img = self.addText(masked_img, diff_perc_txt)
return diff_img
def calculateDiffPixels(self):
"""
Computes an array of differences and merges BGR colors layers to one layer of average values.
Returns:
The percentage difference value
"""
self.diff = cv2.absdiff(self.img1, self.img2)
self.diff_avg = np.mean(self.diff, axis=2)
if self.tolerance: self.diff_avg = self.diff_avg > self.tolerance
diff_pix = np.count_nonzero(self.diff_avg)
total = self.diff_avg.shape[0] * self.diff_avg.shape[1]
self.percentage_diff = diff_pix / total * 100.
return self.percentage_diff
def getTranspDiffImage(self):
"""
Computes a transparent mask with different pixels marked red.
Returns:
A transparent mask with different pixels marked red
"""
if self.diff_avg is None:
self.calculateDiffPixels()
matching_pixels = np.where(self.diff_avg == 0., 255., 0.).astype(np.uint8)
different_pixels = np.where(self.diff_avg != 0., 255., 0.).astype(np.uint8)
diff_mask = np.zeros((self.img1.shape[0], self.img1.shape[1], 4), dtype=np.uint8)
diff_mask[:,:,2] = different_pixels
diff_mask[:,:,3] = matching_pixels
return diff_mask
def mergeDiffImage(self):
"""
Merges (first) original image with a red mask.
Returns:
Original image with differences marked red.
"""
diff_mask = self.getTranspDiffImage()
original_img = cv2.cvtColor(self.img1, cv2.COLOR_BGR2BGRA)
masked = cv2.bitwise_or(original_img, diff_mask)
return masked
@staticmethod
def addText(img, text):
"""
Adds red text to the image.
Args:
img (numpy.ndarray) : image
text (str) : text
Returns:
Image with text
"""
font = cv2.FONT_HERSHEY_COMPLEX
scale = 3
thickness = 4
text_size, _ = cv2.getTextSize(text, font, scale, thickness)
x = int((img.shape[1] - text_size[0]) / 2)
y = int((img.shape[0] + text_size[1]) / 2)
text_img = cv2.putText(img=img, text=text, org=(x, y), fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=scale, color=PictureComparator.TEXT_COLOR, thickness=thickness)
return text_img
@staticmethod
def showImage(img, title="Title"):
"""
Shows image in window.
Args:
img (numpy.ndarray) : image
title (str) : window title
"""
cv2.namedWindow(title, cv2.WINDOW_NORMAL)
cv2.imshow(title, img)
while cv2.getWindowProperty(title, cv2.WND_PROP_VISIBLE) >= 1:
keyCode = cv2.waitKey(1000)
if (keyCode & 0xFF) == ord("q"):
cv2.destroyAllWindows()
break
@staticmethod
def printLayers(img, prefix=""):
"""
Prints color layers for debug purposes.
Args:
img (numpy.ndarray) : image
prefix (str) : prefix printed before color layers
"""
print(prefix)
for i in range(img.shape[2]):
print(f"Layer {i}:")
print(img[:,:,i])