我正在尝试将模板与计算机中的视频进行匹配。
它会抛出错误:
res = cv2.matchTemplate(frame_gray, template ,cv2.TM_CCOEFF_NORMED)
cv2.error: /io/opencv/modules/imgproc/src/templmatch.cpp:1107: error: (-215) _img.size().height <= _templ.size().height && _img.size().width <= _templ.size().width in function matchTemplate
你能帮忙告诉我,我的代码有什么不对。
import cv2
import numpy as np
cap = cv2.VideoCapture('ankhein.avi')
template = cv2.imread('srk_face.png',cv2.IMREAD_GRAYSCALE)
w,h = template.shape[::-1]
while True:
_,frame = cap.read()
frame_gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
res = cv2.matchTemplate(frame_gray, template ,cv2.TM_CCOEFF_NORMED)
threshold = 0.8
loc = np.where(res>=threshold)
for pts in zip(*loc[::-1]):
cv2.rectangle(img_bgr,pts,(pts[0]+w,pts[1]+h),(0,0,255),2)
cv2.imshow('detected',frame)
if cv2.waitKey(5) & 0xFF == 27:
break
cv2.destroyAllWindows()
cap.release()