Search code examples
opencvimage-processingtemplate-matching

when using opencv cvMatchTemplate, how to choose a good template for more accurate matching?


I am using opencv cvMatchTemplate to find a trademark pattern in a bunch of images.

now what I did is looking at the picture, find some unique patch from the trademark, and use it as my template. I found using the whole trademark image is not necessarily better off than using part of it. My question is

is this normal? or I did it wrongly.

if it is normal, how can I choose a good template for more accurate matching?

or in other words, is there any mathematical theory behind selecting good template, which can help me select best template.

I am not using feature detection, as I found that is not accurate as template matching.


Solution

  • Why is template matching working as good as feature detection (when everyone seems to love feature detection)

    Template matching is different than feature detection, as assumes that what you are looking for is in the same plane (the image plane) as the template. A "warped" template will not work ( template matching with warped templates is called Digital Image Correlation).

    Thus, if you are looking for a logo in an image of a sheet of paper aligned to the camera, then template-matching is your thing, but if you are looking for a logo on a random image of a street, then feature detection is your thing.

    How does template matching work?

    Well openCV has a brilliant example of it: http://docs.opencv.org/doc/tutorials/imgproc/histograms/template_matching/template_matching.html

    Why does a piece of the template work as good as the whole?

    However you are wondering why just a piece of the template is as good as the whole thing (not always, but can happen).

    This is easy to understand: Your "part" of the template has enough information to be identified.

    Example*:

    http://www.senseimarketing.com/wp-content/uploads/2015/01/Logos.jpg

    If I ask you to find the following image, would you find it accurately?

    enter image description here

    I hope the answer is yes. Why didnt you need the whole image to find it?

    ->Because that part of the image has enough information to be identified accurately! You dont need the whole image!

    However, if I would have given you the following image:

    enter image description here

    You wouldn't be able to identify the logo, as there are at least 3 or 4 other logos that have yellow on them.

    How can I know if a piece of my template is good enough ?

    There is not way "for sure" to know if a template will be enough to be uniquely identified, but there is a way to know if there is information in the image or not.

    As the template matching relies correlation coefficients and sum of squares coefficients, it means that the more "different" amount of information the template has, the better. This can be approximated with the sum of the image gradients.

    Compute the gradients of the template in the X and Y directions and sum them, then sum the result. The bigger that number is, the better the template!**


    *Logos are a brilliant example, I have no association with any of these companies.

    ** This is mathematically proven in http://www.ncbi.nlm.nih.gov/pubmed/18545407