7

I am using opencv template matching in real time camera capture, and got the error message:

error: (-215) _img.size().height <= _templ.size().height && _img.size().width <= _templ.size().width in function matchTemplate

enviroment:

OpenCV: 3.4.1
Python3

code (is the example from Official website doc, I just replace the picture)

import cv2 as cv
import numpy as np
img_rgb = cv.imread('mybook.png')
img_gray = cv.cvtColor(img_rgb, cv.COLOR_BGR2GRAY)
template = cv.imread('book.jpg',0)
w, h = template.shape[::-1]
res = cv.matchTemplate(img_gray,template,cv.TM_CCOEFF_NORMED)
threshold = 0.8
loc = np.where( res >= threshold)
for pt in zip(*loc[::-1]):
    cv.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,0,255), 2)
cv.imwrite('res.png',img_rgb)

I wonder if I did something wrong? Thanks a lot!

JeffChen
  • 173
  • 2
  • 10
  • 1
    By reading the error you can tell that your template might be larger than your image. – dorverbin May 01 '18 at 03:10
  • Oh my! I took the picture from cellphone and template from mac screenshot. I didn't notice the size problem. Thanks a lot! – JeffChen May 01 '18 at 03:53
  • You should post your solution as an answer and then accept it as the correct answer after a few days - So that this question does not remain unanswered :) – Wool May 04 '18 at 16:53
  • OK, thanks for reminding me that – JeffChen May 07 '18 at 01:49
  • @dobervin I am having the same issue. But my template is smaller than the image. – katu Feb 11 '20 at 10:01

1 Answers1

6

The problem is because that the picture I want to used for template is bigger than my original picture.

So i just change the template and it fixed! Thanks for @dorverbin's comment!!

JeffChen
  • 173
  • 2
  • 10