-3

I am trying to make a iris scanner using Python and open CV. While using the template matching function I get the following error:

import cv2
import numpy as np

img1 = cv2.imread('canny.jpg');
img2 = cv2.imread('frame1.jpg');
edges=cv2.Canny(img2,100,100)
w,h=edges.shape[::-1]

res = cv2.matchTemplate(img1 , edges, cv2.TM_CCOEFF_NORMED)
threshold = 0.8
loc = np.where(res >= threshold)
print loc

Following is the error:

Traceback (most recent call last):
  File "E:/OpenCV Programs/threshold2img1.py", line 9, in <module>
    res = cv2.matchTemplate(img1 , edges, cv2.TM_CCOEFF_NORMED)
error: OpenCV(3.4.1) C:\build\master_winpack-bindings-win32-vc14-static\opencv\modules\imgproc\src\templmatch.cpp:1102: error: (-215) (depth == 0 || depth == 5) && type == _templ.type() && _img.dims() <= 2 in function cv::matchTemplate
halfer
  • 19,824
  • 17
  • 99
  • 186
  • so urgent that you don't have time to enter the error message into websearch like google? there are plenty of solutions.... – Piglet Apr 06 '18 at 04:57
  • its because you are using images of different shapes. canny edge is has shape(m,n) while img1 has shape (m,n,3) – flamelite Apr 06 '18 at 04:59
  • I'm not sure how you even came up with the idea this could work btw. it is very unlikely that you will get any good results using this kind of template matching on canny images of eyes. do you even know what this function does? – Piglet Apr 06 '18 at 08:24

1 Answers1

1

imread reads image files as BGR colour images by default. So img1 is BGR colour image while edge is grayscale.

You cannot do template matching between a BRG colour image and a grayscale template. Mathematically that doesn't make sense.

Think of the colour pixels as points in 3d space. Now how similar is scalar 5 to the point (3,4,1)?

The OpenCV manual is actually pretty clear about that. They even give the formulas used to calculate the results...

https://docs.opencv.org/2.4/modules/imgproc/doc/object_detection.html?highlight=matchtemplate#matchtemplate

In case of a color image, template summation in the numerator and each sum in the denominator is done over all of the channels and separate mean values are used for each channel. That is, the function can take a color template and a color image.

halfer
  • 19,824
  • 17
  • 99
  • 186
Piglet
  • 27,501
  • 3
  • 20
  • 43