Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python opencv matchTemplate is mask feature implemented?

OpenCV, as of version 3.0.0, added a mask feature to the matchTemplate method. It supports template matching with transparent templates by defining a mask on the template. My python program below works fine, but if I add a mask parameter to the cv2.matchTemplate call, it throws an error:

OpenCV Error: The function/feature is not implemented () in matchTemplateMask, file /Users/jared.rada/dev/opencv/modules/imgproc/src/templmatch.cpp, line 894
Traceback (most recent call last):
File "masked.py", line 13, in <module>
res = cv2.matchTemplate(img, tmpl, cv2.TM_CCOEFF_NORMED, data, mask)
cv2.error: /Users/jared.rada/dev/opencv/modules/imgproc/src/templmatch.cpp:894: error: (-213)  in function matchTemplateMask`

My source code:

import sys
import numpy as np
import cv2


img = cv2.imread('./image.jpg')
tmpl = cv2.imread('./tmpl.png')
mask = cv2.imread('./mask.png')
w, h = tmpl.shape[:-1]
data = np.zeros((h, w, 3), dtype=np.uint8)

res = cv2.matchTemplate(img, tmpl, cv2.TM_CCOEFF_NORMED, data, mask)

min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)

top_left = max_loc
bottom_right = (top_left[0] + w, top_left[1] + h)
cv2.rectangle(img, top_left, bottom_right, (0, 0, 255), 2)

cv2.imshow("images", np.hstack([img]))
cv2.waitKey(0)

How do I know if the python bindings support the mask feature?

like image 871
jaredrada Avatar asked Feb 26 '16 17:02

jaredrada


People also ask

What does the cv2 matchTemplate function do?

OpenCV comes with a function cv. matchTemplate() for this purpose. It simply slides the template image over the input image (as in 2D convolution) and compares the template and patch of input image under the template image.

What is the output of cv2 matchTemplate?

The output result from cv2. matchTemplate is a matrix with spatial dimensions: Width: image. shape[1] - template.

What is cv2 minMaxLoc?

Once you got the result, you can use cv2. minMaxLoc() function to find where is the maximum/minimum value. Take it as the top-left corner of rectangle and take (w,h) as width and height of the rectangle. That rectangle is your region of template. If you are using cv2.

What is Sqdiff?

SQDIFF is a difference based calculation that gives a 0 at a perfect match. The other two (CCORR and CCOEFF) are correlation based, and return a 1.0 for a perfect match. To determine the maximum point in the correlation, we use another OpenCV function: cvMinMaxLoc.


2 Answers

there is an easy answer: looking at the src code , you will find, that it's only implemented for method == CV_TM_SQDIFF and method == CV_TM_CCORR_NORMED , in other words, not for your desired cv2.TM_CCOEFF_NORMED

like image 179
berak Avatar answered Sep 25 '22 01:09

berak


update for the answer: see src code

and the commit:

templmatch: Add support for mask for all methods

now latest pip install opencv-contrib-python will including mask for all methods (testd on python3.7(

like image 41
Felix Liu Avatar answered Sep 25 '22 01:09

Felix Liu