openCV中使用模板匹配函数时遇到如下错误

错误提示如下:
Traceback (most recent call last):
File "D:/学习办公盘/python学习代码/opencv/moban.py", line 12, in
res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)
error: ........\opencv\modules\imgproc\src\templmatch.cpp:249: error: (-215) (img.depth() == CV_8U || img.depth() == CV_32F) && img.type() == templ.type() in function cv::matchTemplate

代码如下:
import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread('d:/学习办公盘/python学习代码/opencv/捕获.png')
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

template = cv2.imread('d:/学习办公盘/python学习代码/opencv/text3.jpg')
w= template.shape[0]
w= template.shape[1]

res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)
threshold = 0.8

loc = np.where( res >= threshold)
for pt in zip(*loc[::-1]):
cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,0,255), 2)

#cv2.imwrite('res.png',img_rgb)
cv2.imshow('res',img)
cv2.waitKey(0)
cv2.destroyAllWindows()


2个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问