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

时间:2016-02-26 17:42:47

标签: python opencv opencv3.0

OpenCV, as of version 3.0.0, added a mask feature到matchTemplate方法。它通过在模板上定义掩码来支持与透明模板的模板匹配。下面我的python程序工作正常,但如果我在mask调用中添加cv2.matchTemplate参数,则会抛出错误:

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`

我的源代码:

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)

我如何知道python绑定是否支持掩码功能?

2 个答案:

答案 0 :(得分:9)

有一个简单的答案:查看src code,您会发现它仅适用于method == CV_TM_SQDIFFmethod == CV_TM_CCORR_NORMED,换句话说,不适用于您想要的cv2.TM_CCOEFF_NORMED }}

答案 1 :(得分:0)

更新答案:请参见src code

the commit

templmatch:添加对所有方法的掩码的支持

最新的lambda <- df2 * PV / (1 - PV) # another definition of non-centrality parameter g <- (df1 + lambda)^2 / (df1 + 2 * lambda) # adjusted df1 k <- (df1 + lambda) / df1 # to adjust for non-centrality #estimated statistical power F_crit <- qf(1-alpha, df1, df2) 1 - pf(F_crit/k, g, df2) > 0.5521764 将包括所有方法的掩码(已在python3.7上测试(