Skip to content

Commit 653514c

Browse files
committed
better handle the different opencv matchTemplate methods
1 parent 543ad29 commit 653514c

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

MTM/__init__.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -107,18 +107,18 @@ def findMatches(listTemplates, image, method=cv2.TM_CCOEFF_NORMED, N_object=floa
107107
if N_object==1: # Detect global Min/Max
108108
minVal, maxVal, minLoc, maxLoc = cv2.minMaxLoc(corrMap)
109109

110-
if method==1:
110+
if method in (0,1):
111111
Peaks = [minLoc[::-1]] # opposite sorting than in the multiple detection
112112

113-
elif method in (3,5):
113+
else:
114114
Peaks = [maxLoc[::-1]]
115115

116116

117117
else:# Detect local max or min
118-
if method==1: # Difference => look for local minima
118+
if method in (0,1): # Difference => look for local minima
119119
Peaks = _findLocalMin_(corrMap, score_threshold)
120120

121-
elif method in (3,5):
121+
else:
122122
Peaks = _findLocalMax_(corrMap, score_threshold)
123123

124124

@@ -150,7 +150,8 @@ def matchTemplates(listTemplates, image, method=cv2.TM_CCOEFF_NORMED, N_object=f
150150
- image : Grayscale or RGB numpy array
151151
image in which to perform the search, it should be the same bitDepth and number of channels than the templates
152152
- method : int
153-
one of OpenCV template matching method (0 to 5), default 5=0-mean cross-correlation
153+
one of OpenCV template matching method (1 to 5), default 5=0-mean cross-correlation
154+
method 0 is not supported (no NMS implemented for non-bound difference score), use 1 instead
154155
- N_object: int
155156
expected number of objects in the image
156157
- score_threshold: float in range [0,1]
@@ -173,8 +174,9 @@ def matchTemplates(listTemplates, image, method=cv2.TM_CCOEFF_NORMED, N_object=f
173174

174175
tableHit = findMatches(listTemplates, image, method, N_object, score_threshold, searchBox)
175176

176-
if method == 1: sortAscending = True
177-
elif method in (3,5): sortAscending = False
177+
if method == 0: raise ValueError("The method TM_SQDIFF is not supported. Use TM_SQDIFF_NORMED instead.")
178+
elif method == 1: sortAscending = True
179+
else: sortAscending = False
178180

179181
return NMS(tableHit, score_threshold, sortAscending, N_object, maxOverlap)
180182

0 commit comments

Comments
 (0)