python opencv matchTemplate是否实现了蒙版功能? [英] python opencv matchTemplate is mask feature implemented?

查看:604
本文介绍了python opencv matchTemplate是否实现了蒙版功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OpenCV从3.0.0版开始,添加了遮罩功能 matchTemplate方法.通过在模板上定义掩码,它支持与透明模板匹配的模板.我的下面的python程序运行良好,但是如果我在cv2.matchTemplate调用中添加mask参数,则会引发错误:

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`

我的源代码:

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绑定是否支持掩码功能?

推荐答案

有一个简单的答案:查看

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

这篇关于python opencv matchTemplate是否实现了蒙版功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆