openCV模板匹配错误:(215)

2024-04-28 05:15:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用opencv模板匹配进行实时摄像头捕获,并收到错误消息:

error: (-215) _img.size().height <= _templ.size().height && _img.size().width <= _templ.size().width in function matchTemplate

环境:

^{pr2}$

代码(是Official website doc中的示例,我只是替换图片)

import cv2 as cv
import numpy as np
img_rgb = cv.imread('mybook.png')
img_gray = cv.cvtColor(img_rgb, cv.COLOR_BGR2GRAY)
template = cv.imread('book.jpg',0)
w, h = template.shape[::-1]
res = cv.matchTemplate(img_gray,template,cv.TM_CCOEFF_NORMED)
threshold = 0.8
loc = np.where( res >= threshold)
for pt in zip(*loc[::-1]):
    cv.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,0,255), 2)
cv.imwrite('res.png',img_rgb)

我想知道我是不是做错了什么? 谢谢!在


Tags: inimportptimgsizeasnpres