1
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'
Georgy
  • 12,464
  • 7
  • 65
  • 73
J.Doe
  • 21
  • 1
  • 3
  • 1
    Are you sure that your nesne image is loaded? The error means that the size of a template is greater than the size of the frame from the camera. What is the resolution of nesne and kare? – Piotr Siekański Jan 30 '19 at 14:36
  • thx I solved that problem but there is a new one. Can you help me about this. w,h = nesne.shape AttributeError: 'NoneType' object has no attribute 'shape' [ WARN:1] terminating async callback I am sure about image's name and that's place – J.Doe Jan 30 '19 at 15:52
  • Check this link: https://stackoverflow.com/questions/39833796/opencv-nonetype-object-has-no-attribute-shape – Piotr Siekański Jan 30 '19 at 16:14
  • it did not work but I solved anyway there is new one can you look this : res = cv2.matchTemplate(gri_kare,nesne,cv2.TM_CCOEFF_NORMED) cv2.error: OpenCV(4.0.0) C:\projects\opencv-python\opencv\modules\imgproc\src\templmatch.cpp:1107: error: (-215:Assertion failed) (depth == CV_8U || depth == CV_32F) && type == _templ.type() && _img.dims() <= 2 in function 'cv::matchTemplate' [ WARN:0] terminating async callback – J.Doe Jan 30 '19 at 18:03
  • Please provide the data types in both template and mage from the camera. This error means that the type of the data has to be 8U (unsigned char) or 32F (float) and has to be the same in both image and the template. – Piotr Siekański Jan 30 '19 at 19:01

1 Answers1

2

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.

mcgusty
  • 1,354
  • 15
  • 21