Skip to content
This repository was archived by the owner on Apr 26, 2023. It is now read-only.

Commit 95bd63c

Browse files
committed
Fix #52 Comparisons with empty masks
1 parent a479681 commit 95bd63c

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

src/compare.py

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ def compare_histograms(source, capture):
1616

1717
source_hist = cv2.calcHist([source], [0, 1, 2], None, [8, 8, 8], [0, 256, 0, 256, 0, 256])
1818
capture_hist = cv2.calcHist([capture], [0, 1, 2], None, [8, 8, 8], [0, 256, 0, 256, 0, 256])
19-
19+
2020
cv2.normalize(source_hist, source_hist)
2121
cv2.normalize(capture_hist, capture_hist)
22-
22+
2323
return 1 - cv2.compareHist(source_hist, capture_hist, cv2.HISTCMP_BHATTACHARYYA)
2424

2525
def compare_histograms_masked(source, capture, mask):
@@ -34,10 +34,10 @@ def compare_histograms_masked(source, capture, mask):
3434
"""
3535
source_hist = cv2.calcHist([source], [0, 1, 2], mask, [8, 8, 8], [0, 256, 0, 256, 0, 256])
3636
capture_hist = cv2.calcHist([capture], [0, 1, 2], mask, [8, 8, 8], [0, 256, 0, 256, 0, 256])
37-
37+
3838
cv2.normalize(source_hist, source_hist)
3939
cv2.normalize(capture_hist, capture_hist)
40-
40+
4141
return 1 - cv2.compareHist(source_hist, capture_hist, cv2.HISTCMP_BHATTACHARYYA)
4242

4343
def compare_l2_norm(source, capture):
@@ -51,10 +51,10 @@ def compare_l2_norm(source, capture):
5151
"""
5252

5353
error = cv2.norm(source, capture, cv2.NORM_L2)
54-
54+
5555
# The L2 Error is summed across all pixels, so this normalizes
5656
max_error = (source.size ** 0.5) * 255
57-
57+
5858
return 1 - (error/max_error)
5959

6060
def compare_l2_norm_masked(source, capture, mask):
@@ -73,6 +73,8 @@ def compare_l2_norm_masked(source, capture, mask):
7373
# The L2 Error is summed across all pixels, so this normalizes
7474
max_error = (3 * numpy.count_nonzero(mask) * 255 * 255) ** 0.5
7575

76+
if not max_error:
77+
return 0
7678
return 1 - (error / max_error)
7779

7880
def compare_template(source, capture):
@@ -117,7 +119,7 @@ def compare_phash(source, capture):
117119
"""
118120
Compares the pHash of the two given images and returns the similarity between
119121
the two.
120-
122+
121123
@param source: Image of any given shape as a numpy array
122124
@param capture: Image of any given shape as a numpy array
123125
@return: The similarity between the hashes of the image as a number 0 to 1.
@@ -135,7 +137,7 @@ def compare_phash_masked(source, capture, mask):
135137
"""
136138
Compares the pHash of the two given images and returns the similarity between
137139
the two.
138-
140+
139141
@param source: Image of any given shape as a numpy array
140142
@param capture: Image of any given shape as a numpy array
141143
@param mask: An image matching the dimensions of the source, but 1 channel grayscale
@@ -149,11 +151,27 @@ def compare_phash_masked(source, capture, mask):
149151
# the same
150152
source = cv2.bitwise_and(source, source, mask=mask)
151153
capture = cv2.bitwise_and(capture, capture, mask=mask)
152-
154+
153155
source = Image.fromarray(source)
154156
capture = Image.fromarray(capture)
155157

156158
source_hash = imagehash.phash(source)
157159
capture_hash = imagehash.phash(capture)
158160

159-
return 1 - ((source_hash - capture_hash)/64.0)
161+
if not source_hash + capture_hash:
162+
return 0
163+
return 1 - ((source_hash - capture_hash) / 64.0)
164+
165+
166+
def checkIfImageHasTransparency(image):
167+
# Check if there's a transparency channel (4th channel) and if at least one pixel is transparent (< 255)
168+
if image.shape[2] != 4:
169+
return False
170+
mean = np.mean(image[:, :, 3])
171+
if mean != 0:
172+
# Non-transparent images code path is usually faster and simpler, so let's return that
173+
return False
174+
# TODO error message if all pixels are transparent
175+
# (the image appears as all black in windows, so it's not obvious for the user what they did wrong)
176+
177+
return mean != 255

0 commit comments

Comments
 (0)