Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9c9825a

Browse files
author
Laurent Thomas
committed
check that image and template are not 0-width or height
1 parent 3d8a105 commit 9c9825a

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

‎MTM/__init__.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,14 @@ def findMatches(listTemplates, image, method=cv2.TM_CCOEFF_NORMED, N_object=floa
121121
"""
122122
if N_object != float("inf") and not isinstance(N_object, int):
123123
raise TypeError("N_object must be an integer")
124-
124+
125+
## Check image has not 0-width or height
126+
if image.shape[0] == 0:
127+
raise ValueError("Image has a height of 0.")
128+
129+
if image.shape[1] == 0:
130+
raise ValueError("Image has a width of 0.")
131+
125132
## Crop image to search region if provided
126133
if searchBox is not None:
127134
xOffset, yOffset, searchWidth, searchHeight = searchBox
@@ -132,14 +139,25 @@ def findMatches(listTemplates, image, method=cv2.TM_CCOEFF_NORMED, N_object=floa
132139
# Check that the template are all smaller are equal to the image (original, or cropped if there is a search region)
133140
for index, tempTuple in enumerate(listTemplates):
134141

135-
if not isinstance(tempTuple, tuple) or len(tempTuple)==1:
142+
if not isinstance(tempTuple, tuple) or len(tempTuple)<2:
136143
raise ValueError("listTemplates should be a list of tuples as ('name','array') or ('name', 'array', 'mask')")
137-
138-
templateSmallerThanImage = all(templateDim <= imageDim for templateDim, imageDim in zip(tempTuple[1].shape, image.shape))
144+
145+
tempName = tempTuple[0]
146+
tempImage = tempTuple[1]
147+
148+
# Check that template is not 0-width or height
149+
if tempImage.shape[0] == 0:
150+
raise ValueError(f"Template '{tempName}' has a height of 0.")
151+
152+
if tempImage.shape[1] == 0:
153+
raise ValueError(f"Template '{tempName}' has a width of 0.")
154+
155+
156+
templateSmallerThanImage = all(templateDim <= imageDim for templateDim, imageDim in zip(tempImage.shape, image.shape)) # check both width/height smaller than image or search region
139157

140158
if not templateSmallerThanImage :
141159
fitIn = "searchBox" if (searchBox is not None) else "image"
142-
raise ValueError("Template '{}' at index {} in the list of templates is larger than {}.".format(tempTuple[0], index, fitIn) )
160+
raise ValueError("Template '{}' at index {} in the list of templates is larger than {}.".format(tempName, index, fitIn) )
143161

144162
listHit = []
145163
## Use multi-threading to iterate through all templates, using half the number of cpu cores available.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /