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 :)
The main challenges in the template matching task are: occlusion, detection of non-rigid transformations, illumination and background changes, background clutter and scale changes.
Multi-Template-Matching is a python package to perform object-recognition in images using one or several smaller template images.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With