Template matching poor results cv2

42 Views Asked by At

I'm using template matching function from cv2 library in python to detect gandhi's potrait in indian currency note for fake currency detection. I'm taking the template by cropping the source image itself and then trying to match it however it is not able able to match if threshold is greater than 0.21.

Here is the source image:enter image description here

Here is the cropped image of grayscaled source image used as template: enter image description here

and here is the code that i'm using for template matching:

import cv2

import numpy as np
grayscale_image = cv2.imread('/content/drive/MyDrive/files/50.jpg')
grayscale_image = cv2.cvtColor(grayscale_image,cv2.COLOR_BGR2GRAY)
image_path = '/content/drive/MyDrive/template.PNG'
template_image = cv2.imread(image_path)
template_image = cv2.cvtColor(template_image,cv2.COLOR_BGR2GRAY)
from google.colab.patches import cv2_imshow
h,w = template_image.shape[::]
res = cv2.matchTemplate(grayscale_image,template_image,cv2.TM_CCOEFF_NORMED)
plt.imshow(res,cmap='gray')
threshold = 0.25
loc=np.where(res>=threshold)
for pt in zip(*loc[::-1]):
  cv2.rectangle(grayscale_image,pt, (pt[0] + w, pt[1] + h), (0,0,255), 2)
cv2_imshow(grayscale_image)
0

There are 0 best solutions below