cv2与视频匹配的模板

时间:2018-04-02 09:09:28

标签: python-3.x templates cv2

我正在尝试将模板与计算机中的视频进行匹配。

它会抛出错误:

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()                        

0 个答案:

没有答案