python opencv matchTemplate是实现了掩码功能吗?

时间:2022-06-29 23:48:01

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,从3.0.0版开始,为matchTemplate方法添加了一个掩码功能。它通过在模板上定义掩码来支持与透明模板的模板匹配。下面我的python程序工作正常,但如果我向cv2.matchTemplate调用添加一个mask参数,它会抛出一个错误:

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?

我怎么知道python绑定是否支持掩码功能?

1 个解决方案

#1


7  

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

有一个简单的答案:查看src代码,你会发现,它只针对方法== CV_TM_SQDIFF和方法== CV_TM_CCORR_NORMED实现,换句话说,不适用于你想要的cv2.TM_CCOEFF_NORMED

#1


7  

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

有一个简单的答案:查看src代码,你会发现,它只针对方法== CV_TM_SQDIFF和方法== CV_TM_CCORR_NORMED实现,换句话说,不适用于你想要的cv2.TM_CCOEFF_NORMED