简体   繁体   中英

cv2.matchTemplate gives an error: (-215:Assertion failed)

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

and I get this error:

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'

It works when both the input image for cv2.matchTemplate is in gray scale.

Add the following line nesne = cv2.cvtColor(nesne, cv2.COLOR_BGR2GRAY) after reading image and it will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM