你好我想尝试使用matchTamplate func但是它给了thar eror
OpenCV错误:cv中的断言失败((深度== 0 ||深度== 5)&& type == _templ.type()&& _img.dims()< = 2): :matchTemplate,文件C:\ projects \ opencv-python \ opencv \ modules \ imgproc \ src \ templmatch.cpp,第1102行 Traceback(最近一次调用最后一次): 文件“templatematch.py”,第10行,in res = cv2.matchTemplate(img2gray,template,cv2.TM_CCOEFF_NORMED) cv2.error:C:\ projects \ opencv-python \ opencv \ modules \ imgproc \ src \ templmatch.cpp:1102:错误:(-215)(深度== 0 ||深度== 5)&& type == _templ.type()&&函数cv :: matchTemplate
中的_img.dims()< = 2
这是代码
import cv2
import numpy as np
img = cv2.imread("tempmatch1.jpg")
img2gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
template = cv2.imread("tempmatch2.jpg")
w, h,_ = template.shape[::-1]
res = cv2.matchTemplate(img2gray, template, cv2.TM_CCOEFF_NORMED)
threshold = 0.80
loc = np.where(res >= threshold)
for pt in zip(*loc[::-1]):
cv2.rectangle(img, pt, (pt[0]+w, pt[1]+h), (0,0,255), 2)
cv2.imshow("detected", img)
k= cv2.waitKey(5) & 0xFF
if k==27:
cv2.destroyAllWindows()
答案 0 :(得分:2)
我怀疑该错误告诉您该模板与其应用的图像不兼容。在这种情况下,颜色模板和灰度图像。
而不是
res = cv2.matchTemplate(img2gray, template, cv2.TM_CCOEFF_NORMED)
试
res = cv2.matchTemplate(img, template, cv2.TM_CCOEFF_NORMED)