cv2.matchTemplate给出错误:(-215:断言失败)

时间:2019-01-30 13:54:17

标签: python opencv matchtemplate

import cv2
import numpy as np


kamera = cv2.VideoCapture(0)

while True :
    ret,kare = kamera.read()

    gri_kare = cv2.cvtColor(kare,cv2.COLOR_BGR2GRAY)

    nesne = cv2.imread("nesn.jpg",0)

    w,h = nesne.shape

    res = cv2.matchTemplate(gri_kare,nesne,cv2.TM_CCOEFF_NORMED)

    esikdeger = 0.8

    loc = np.where(res > esikdeger)

    for n in zip(loc) :
        cv2.rectangle(nesne,n,(n[0]+h,n[1]+w),(255,255,255),2,)

    cv2.imshow("asd",kare)


    if cv2.waitKey(25) & 0xFF == ord("q"):
        break


kamera.release()
cv2.destroyAllWindows()

我得到这个错误:

Traceback (most recent call last):
  File "C:/Users/abrakadabra/Desktop/python/CV/nesnetanima1.py", line 16, in <module>
    res = cv2.matchTemplate(gri_kare,nesne,cv2.TM_CCOEFF_NORMED)
cv2.error: OpenCV(3.4.2) C:\projects\opencv-python\opencv\modules\imgproc\src\templmatch.cpp:1107: error: (-215:Assertion failed) _img.size().height <= _templ.size().height && _img.size().width <= _templ.size().width in function 'cv::matchTemplate'

1 个答案:

答案 0 :(得分:0)

cv2.matchTemplate 的输入图像都是灰度时,它起作用。

在阅读图像后添加以下行 nesne = cv2.cvtColor(nesne, cv2.COLOR_BGR2GRAY) 即可。