Skip to content

Commit a7744e6

Browse files
committed
Take care of identical datatype casting
1 parent be01e93 commit a7744e6

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

MTM/__init__.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from .NMS import NMS
88

99
__all__ = ['NMS']
10-
__version__ = '1.5.1'
10+
__version__ = '1.5.2'
1111

1212
def _findLocalMax_(corrMap, score_threshold=0.6):
1313
'''
@@ -82,15 +82,27 @@ def findMatches(listTemplates, image, method=cv2.TM_CCOEFF_NORMED, N_object=floa
8282
else:
8383
xOffset=yOffset=0
8484

85-
## 16-bit image are converted to 32-bit for matchTemplate
86-
if image.dtype == 'uint16': image = np.float32(image)
85+
## OpenCv matchTemplates only support 8 or 32-bit ie cast 16-bit to 32-bit
86+
if image.dtype == 'uint16':
87+
image = np.float32(image)
88+
89+
elif image.dtype == "float64":
90+
raise ValueError("64-bit not supported, max 32-bit")
8791

8892
listHit = []
8993
for templateName, template in listTemplates:
9094

9195
#print('\nSearch with template : ',templateName)
92-
## 16-bit image are converted to 32-bit for matchTemplate
93-
if template.dtype == 'uint16': template = np.float32(template)
96+
97+
if template.dtype == "float64": raise ValueError("64-bit not supported, max 32-bit")
98+
99+
## Make sure both images have same bittype and 8 or 32 bit
100+
if (template.dtype == "uint8" and image.dtype == "float32") or template.dtype == 'uint16':
101+
template = np.float32(template)
102+
103+
# Separate if
104+
if template.dtype == "float32" and image.dtype == "uint8":
105+
image = np.float32(image)
94106

95107
## Compute correlation map
96108
corrMap = cv2.matchTemplate(template, image, method)
@@ -155,10 +167,10 @@ def matchTemplates(listTemplates, image, method=cv2.TM_CCOEFF_NORMED, N_object=f
155167
156168
Returns
157169
-------
158-
Pandas DataFrame with 1 row per hit and column "TemplateName"(string), "BBox":(X, Y, Width, Height), "Score":float
159-
if N=1, return the best matches independently of the score_threshold
160-
if N<inf, returns up to N best matches that passed the score_threshold
161-
if N=inf, returns all matches that passed the score_threshold
170+
Pandas DataFrame with 1 row per hit and column "TemplateName"(string), "BBox":(X, Y, Width, Height), "Score":float
171+
if N=1, return the best matches independently of the score_threshold
172+
if N<inf, returns up to N best matches that passed the score_threshold
173+
if N=inf, returns all matches that passed the score_threshold
162174
'''
163175
if maxOverlap<0 or maxOverlap>1:
164176
raise ValueError("Maximal overlap between bounding box is in range [0-1]")

test.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
import numpy as np
55

66
#%% Get image and templates by cropping
7-
smallCoin = coins()[37:37+38, 80:80+41]
8-
bigCoin = coins()[14:14+59,302:302+65]
97
image = coins()
8+
smallCoin = image[37:37+38, 80:80+41]
9+
bigCoin = image[14:14+59,302:302+65]
10+
1011

1112
#%% Perform matching
1213
tableHit = MTM.matchTemplates([('small', smallCoin), ('big', bigCoin)], image, score_threshold=0.6, method=cv2.TM_CCOEFF_NORMED, maxOverlap=0) # Correlation-score

0 commit comments

Comments
 (0)