Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Template Matching on different sizes efficiently

Is there a more efficient way to use Template Matching with images of different sizes? Here is my current Script:

import cv2
import numpy as np

img_bgr = cv2.imread('./full.jpg')
img_gray = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2GRAY)

template = cv2.imread('./template.jpg', 0)

w, h = template.shape[::-1]

res = cv2.matchTemplate(img_gray, template, 
cv2.TM_CCOEFF_NORMED)
threshold = 0.8
loc = np.where(res >= threshold)

for pt in zip(*loc[::-1]):
  cv2.rectangle(img_bgr, pt, (pt[0]+w, pt[1]+h), (0,255,255), 2)

cv2.imshow('detected', img_bgr)
cv2.waitKey(0)
cv2.destroyAllWindows()

This is my Template: Template

And I have these images, the first one works and the second one does not because of the size:

Works!

Fails

At first thought, I'm thinking it's failing because of the size of the template vs image

So I tried using this tutorial: Multi Scale Matching

But this seems really slow and bulky especially since I intend to use this in videos when I get it working. Is there a better way to handle this

Also, eventually I would also only like to check the top right of the image, I know that's a completely different question, but if you have any ideas since we're talking about scaling :)

like image 966
Bilal Abdullah Avatar asked Apr 09 '18 19:04

Bilal Abdullah


People also ask

What is one problem with template matching?

The main challenges in the template matching task are: occlusion, detection of non-rigid transformations, illumination and background changes, background clutter and scale changes.

What is multi scale template matching?

Multi-Template-Matching is a python package to perform object-recognition in images using one or several smaller template images.

Which is the drawback of template matching?

Well, the first disadvantage is that you need to know what you're looking for. If you're looking for dynamic features, you'll be better off using some other techniques. Secondly, template matching provided by OpenCV doesn't let you check for rotations and scalings.

Are template matches slow?

In summery statistical template matching method is slow and takes ages whereas opencv FFT or cvMatchTemplate() is quick and highly optimised. Statistical template matching will not produce errors if an object is not there whereas opencv FFT can unless care is taken in its application.


1 Answers

The easiest way is to use feature matching instead of template matching. Feature matching is exactly meant for this kind of applications. It can also detect if the image is rotated .. etc

Have a lock at this https://docs.opencv.org/master/dc/dc3/tutorial_py_matcher.html

like image 182
Abdelsalam Hamdi Avatar answered Sep 22 '22 22:09

Abdelsalam Hamdi