Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV match template

I am trying to match a logo (template) I have with some images. My images are all colored in nature and the way I am doing it is using the cvMatchTemplate() from openCV and running the template (logo) over my source image. I scale the template to multiple levels to find the best match. Please note that I am just finding the digitally imprinted logos on the image and not the logos present in the scene. Eg: Detecting sky sports from this image http://i56.tinypic.com/2v3j3wx.jpg (The image is purely for representation and clarity of my task and is not images I am working with

My images are not of fixed resolution so I scale it up to a standard 800x600. Now when my source image resolution is very bad say 300x300, the results are very ordinary. I am using the method=CV_TM_CCOEFF_NORMED in the template parameter and the scores even for accurate matches is quite low (as low as 0.4 on a scale of 0 to 1.0) making it difficult for me to confidently say if the logo is present or not. I have two questions on this:

1 - In opencv template matching, how does it handle colored images. I tried to comprehend from the document and my inference was it computes the score for each channel separately and the best is taken. If that is the case wont I would be better off taking all the three channels into account for better results

2 - Any alternative approach !! :)

Let me know if any thing is not clear !

EDIT (Additional information): As discussed in the comments I am attaching my current matching technique which is scaled template matching. Please note the attached images are purely for test purposes and is not my actual set of images I am working with (cannot post as the images are proprieotry) Source ImageA screen shot from youtube Logo Image Got from wikipedia Output Image using Template matching Red block indicating the best match wrt highest score

Although template is matched the score obtained here is 0.59 for this best match. Although relatively its a good score for a match, but still not good enough for me to tell for sure that the desired logo exists or not. In my test images when the logo on screen is transparent, it would still detect the logo but with a poor score of 0.3-0.4. Can a better result be obtained using SURF/SIFT?

EDIT (Attempt with SURF) I tried to run a SURF code already given as an example in the opencv offical documentation (minHessian = 2000).link here here is the output. I am not sure how to interpret it (2,3 points seem to be within the expected boundary. Is this considered good? Any further suggestions? enter image description here Thanks

like image 944
Ajay Nair Avatar asked Jan 15 '13 05:01

Ajay Nair


People also ask

What is OpenCV template matching?

Template Matching is a method for searching and finding the location of a template image in a larger image. OpenCV comes with a function cv. matchTemplate() for this purpose.

What does cv2 match template return?

It returns a grayscale image, where each pixel denotes how much does the neighbourhood of that pixel match with template. If input image is of size (WxH) and template image is of size (wxh) , output image will have a size of (W-w+1, H-h+1) . Once you got the result, you can use cv2.

What is template matching in image processing?

Template matching is a technique in digital image processing for finding small parts of an image which match a template image. It can be used in manufacturing as a part of quality control, a way to navigate a mobile robot, or as a way to detect edges in images.

What is mask in template matching?

Template matching is a technique for finding areas of an image that match (are similar) to a template image (patch). While the patch must be a rectangle it may be that not all of the rectangle is relevant. In such a case, a mask can be used to isolate the portion of the patch that should be used to find the match.


1 Answers

Did you try using a Gaussian Blur on your Source Image before performing template matching? This may give you more accurate results as I think it's the quality of the source image that's giving a worse match

Link to Gaussian Blur in OpenCV Docs :

OpenCV Python Gaussian Blur

Alternatively you could try a histogram comparison technique on the area template matching suggests, for an extra confirmation that the Template Match correlation returned, even if small, is the correct value :

Drawing Histograms

Drawing the histograms is optional, it might be useful for your own application

Comparing the Histograms

^ This method calculates the histograms of your images (source and template) and the correlation between them... However, you don't want a Histogram of the entire source, just where your Template Matching thinks the best correlation is, or some other location in the image, so you want to get a Histogram of a Region of Interest (ROI) instead, see the following C++ code :

Mat OriginalImage = imread("source.jpg", 0);
Rect RegionOfInterest = Rect(150, 150, 250, 250);
Mat ROIImage = OriginalImage(RegionOfInterest);

This lets you calculate the histogram of a region of interest. You should get a Histogram of your template and a histogram for the region where Template Matching thinks your template is in the source and compare them to confirm or refute the Template Matching output

like image 54
MattTheHack Avatar answered Sep 17 '22 12:09

MattTheHack