Template Matching with OpenCV

Nov 4, 2008 | Tags: OpenCV | del.icio.us del.icio.us | digg Digg

Template matching is a technique for finding small parts of an image which match a template image. It slids the template from the top left to the bottom right of the image, and compare for the best match with template. The template dimension should be equal or smaller than the reference image.

Template matching is used in various applications. Some are:

  • Face Recognition.
  • Object Tracking.
  • Medical Image Processing.
  • etc.

OpenCV has functions to perform template matching with several methods. I have made an example on how to perform template matching with OpenCV. Download it by clicking the download link above. The program loads a reference image and a template, find subimage in reference image that match the template, then display both images.

Send suggestion, comments and bug reports to me [at] nashruddin.com

Related Articles

Recommended Book

The Downloads

39 Comments

dyana on Jan 28, 2009:

Hi Nash, I am a newbie in opencv . Anyway I had tried all your examples for opencv part 1 & 2. That are very good examples and I understand that. But when I wanna to try template matching example, from the zip files that u are given, there is a Makefile. I try to understand about this file because I never use it. I am using VC++. How I am gonna to use it with the template matching example? I really hope that u can give some explanation about it and is it i have to run it? How? Thanks

Nash on Jan 28, 2009:

Hi dyana,

Makefile is the file used to simplify the compilation. If you use gcc compiler, you could simply type: make to compile your project.

But since you are using VC++, you can ignore this file. Simply compile the code like the other examples.

Mahmoud on May 21, 2009:

Hello Nash, I would like to thank you for your support. I have 2 automobiles in defined area. One of them should detect the second and then follow it. I face 2 problems and I need your help please:

1. I need to get the angle between reference axis on the area (X-axis) and reference axis on the automobile? In other words, How can I know how much the rotation of the automobile in the image?

2. cvMatchTemplate can match shapes if they not rotated. How to detect objects even if they rotated?
Best

Nash on May 22, 2009:

Man, you have some good questions. Unfortunately, I don't have the answers yet.

Mahmoud on May 23, 2009:

Hello Nash,

Thank you most sincerely for your reply.

However, I trust that you will do your best to help me. Maybe you can guide me to some articles or examples and I will work to solve them.

Do you think using contours and matching 2 contours using OpenCV functions can give good results?

Thanks again.
Best regards,

Madan on Jun 2, 2009:

Hi Mahmoud,
You can do rotation invariant template matching if you can calculate the tilt /skew angle of the portion of the image you are interested in. This can be done by using PCA etc

Mahmoud on Jun 3, 2009:

Hello Madan,
Thanks for your reply. Recently, I could overcome this problem by using contour matching, so what I do is calculating the rotation of the object by using cvMinAreaRect2, I could get the angle & center of the object in the area. Then I apply cvMatchShapes to check the identical between reference object with one in the area. Now I am going to do object avoidance, and I am thinking of some ideas, if you have good ideas, please let us share them and I'm ready for any question.
Best,

Dan on Jun 15, 2009:

Greetings gents,

Very helpful examples to start me on my path of image matching. Very grateful.

Has any of you been able to construct any more advanced examples of image matching? I was thinking more along the lines of applying some image filters and from the image patch to be matched construct a feature that will be used for matching in multiple images?

I have an image patch/section that is to be featured in multiple images. The camera is on a car and the car is in movement.

Can anyone please suggest a good approach in making this object identification possible?

Looking forward to your responses.

Sincerely
Dan

Steve on Jun 23, 2009:

Hey guys,

I was wondering if anyone had luck with getting template matching that's rotation and scale invariant.

The template matching code works great until I rotate the image and the program here:
http://www.lps.usp.br/~hae/software/cirateg/index.html
almost never matches even unrotated images for me.

Could it be done with Phase Correlation maybe? or just the FFT?

thanks,
~Steve

ali on Jul 6, 2009:

Hi,
thanks for the examples, but I couldn't compile the files, since cvcam cannot be found. I've downloaded the latest version of openCV. All other files were found (cxcore cv cvaux highgui ml), whereas cvcam.lib is missing. what should i do?

thanks in advance,
ali

Nash on Jul 7, 2009:

You don't need cvcam. Compile it with:

gcc template_matching.c -o template_matching -I"C:\OpenCV\cxcore\include" -I"C:\OpenCV\cv\include" -I"C:\OpenCV\otherlibs\highgui" -L"C:\OpenCV\lib" -lcxcore -lcv -lhighgui

Replace C:\OpenCV with the directory where you installed OpenCV.

ali on Jul 7, 2009:

yes nash, i compiled successfully without cvcam.lib. thank you.

I have one more question, it's about matching methods. I've cut a small part of an image, then selected it as the template. Default method have found the exact place of the template in the big image, and showed it in a green window. Then, I used normalized cross correlation, which couldn't find the correct place of the template.

How is normalized cross correlation used ? What kind of relation is analyzed during ncc?

Ali

ali on Jul 7, 2009:

Actually, I want to use normalized cross correlation in object tracking. What do you advice?

Nash on Jul 9, 2009:

The methods have different behaviour. For a matching region, CV_TM_SQDIFF* will show it with minimum value while CV_TM_CCORR* and CV_TM_CCOEFF* will show it with maximum value.

If you want to use normalized cross correlation, use it like this:

cvMatchTemplate(img, tpl, res, CV_TM_CCORR_NORMED);
cvMinMaxLoc(res, &minval, &maxval, &minloc, &maxloc, 0);
printf("Found matches at (%d, %d)", maxloc.x, maxloc.y);

vimal on Jul 14, 2009:

Hi,
Please any one help me.I have a doubt in cvMatchTemplate() ,how we can find template is matched or not.Here some times it is giving result wrongly ie it is showing some other wrong portion of the image.I am looking for a method for finding is it matched or not.

Paddy on Aug 6, 2009:

Hi Nash, Wondering if you can help. I am trying to write an OpenCV program for my thesis that inputs a video and I have to find identify the road signs on it and subsequently identify the detail on the sign.

So far I have only managed to decode the video's codec so it loads with OpenCV, then in the while loop i applied HSV thresholding to identify items that are red. But the resultant video appears upside down. Not sure why.

but my main problem is identifying a circle and triangle after that. My Thesis is due next week and I am really struggling any advice would great!

Thanks,

Patrick

Nash on Aug 6, 2009:

To fix the upside down frames, use cvFlip(). There is piece of code to detect circles in opencv samples dir. But I don't have any idea to detect triangles.

Btw, why bother with circles and triangles detection? just use methods similar to template matching to match the road sign candidates with some predefined templates.

However, template matching like my post above is out of this. You'll need a more sophisticated algorithm like SIFT, which is invariant to image scale and rotation.

Tõnu Samuel on Aug 7, 2009:

Forget SIFT. This is slow. Look for "opensurf" which implements SURF algorithm. This is mostly same stuff but faster.

vinit on Aug 15, 2009:

Thanks mann....your short and concise code helped alot....

vinit

Onkar on Aug 23, 2009:

Hello Nash,
Thanx for the code...i am a beginner in OpenCV...and i am realy interested in Computer Vision...
In this program while executing from command prompt, i am getting an error saying-
OpenCV GUI Error Handler
Unsupported format or combination of formats ()
in function cvMatchTemplate, C:\UserVP\opencv\cvscrcv\templmatch.cpp

Nash on Aug 24, 2009:

Try to rename your file to templmatch.c. VC++ treats C and C++ code differently. For better compiler, use MinGW.

Mahmoud on Aug 28, 2009:

Dear Mr. Nash,

I am working now to build maze solving application using OpenCV. I fixed a camera on top of the maze area, and a robot should move from starting point to target point and must avoid obstacles.

So I have to isolate paths from obstacles in the image which is captured by the camera. The obstacles maybe of any shape or color.

In another meaning, I have to get information where the obstacles occupy the pixels in the image and where they are not.

Could you please tell me how to detect achieve this task?

Any advices or example will be helpful from you Mr. Nash

Looking forward to hearing from you!
Regards,

Nash on Aug 29, 2009:

Hello,

Is it a real robot or just a simulation program? if it is a real robot, I think it would be much simpler if you use sensors on it

However from the 'OpenCV point of view', this is quite interesting. The main problem is converting the image into 2D array. Once you've got the array, solving the maze would be easy (or so I thought).

Here's my suggestion:
1. Start with simple images. Say 3x3 maze made by any graphic design software (Photoshop/Gimp).
2. Set the ROI for each cell, to obtain the walls.
3. Convert the walls to 2D array.

If you have successfully done that, move to more complex images. And the last, post your project here so I can learn it from you.

Mahmoud on Aug 31, 2009:

Hello Mr. Nash,

Thank you for your valuable suggestions.

Yes, it is a real robot. I use Omni-Directional Robot to move in diagonal paths. I didn't use sensors like IR or Ultrasonic because the purpose of the project is to build sensorless vision system!

Regarding solving the maze, I use Dijkstra algorithm which can give shortest paths.

About the implementation, I think of using color detection that means the background (say the floor) has a specific color and other objects should have different colors.

But my question is: how can we for the first time tell that the color of the background should be treated as the walkable paths and the color of the objects as obstacles? Remember that all the operations must be done automatically without any information from the user!

I am ready to share my ideas here, and once I get good results I will post it.

Thanks again

Alfonso on Feb 4, 2010:

Just wanted to say thanks a lot!!!

your examples helped me out alot on a homework assignment. I appreciate this!!!

Amar Deo on Feb 26, 2010:

Hi Nash,

I have been following your website for my Artificial Intelligence class. I need to match two image to get affine transform. The feature matching point has to be extracted by using SIFT. I have tried other implementations but I am not able to make it work. Can you please send me the source code for simple SIFT implementation.

Thanks,
Amar

An on Mar 9, 2010:

@Amar:

you can find some SIFT implementations with Google:
http://people.csail.mit.edu/albert/ladypack/wiki/index.php/Known_implementations_of_SIFT

You may also try SUFT, which is faster than SIFT:
http://code.google.com/p/opensurf1

An

imgprocess on Apr 20, 2010:

Hi
i used your example but can you tell me how to create a loop to find all the possible matches in an image if there's more than one match?

Prashant on Apr 28, 2010:

Dear Nashruddin, Thanks a lot for the example. The beauty lies in the simplicity. I guess I am too late in asking this question since the article was written long time ago. But I will try my luck to extract an answer. Your example works fine if the size of the template and the object image to be found inside the image is of same size. But what if the template size differs? I would be extracting more or less than required match. Do you have any suggestion on implementing the template match independent of size?

Nash on Apr 29, 2010:

Actually both images don't have to be the same size. Only that the template has to be *smaller* than (or equal with) the input image. Template matching will scan the image from top-left to bottom-right, so no need to be worried about different size images.

Doug on May 11, 2010:

Hi Nash,

This page has been the best (and most easily understood) example of template matching on the internet. Thanks for putting it out there for those of us still learning. A quick question - given the example in your code, how do we know that a match occurred? If the program finds a match for the template in the source image it will draw a rectangle around it the area that matches. If there is no match the rectangle gets drawn around some other area. Is there a way that the code can determine that a match has occurred? I thought that cvMatchTemplate() might return a value based on whether a match was found, but I don't think that happens. Can the values in cvMinMaxLoc() be used to determine whether a match was found?

Thank you very much for your help.

Doug

Nash on May 13, 2010:

Yes, use cvMinMaxLoc() to find matches. It looks something like this:

cvMinMaxLoc(res, &minval, &maxval, &minloc, &maxloc, 0);

if (minval < THRESHOLD_VAL) {
  printf("Best match found at (%d, %d)", minloc.x, minloc.y);
} else {
  printf("Not found.");
}

Doug on May 18, 2010:

Ah, I see now. Thank you Nash.

forestman on May 21, 2010:

Hi Nash, well actually thank you very much for this interesting tutotial.
I have a problem that i'm trying to resolve. After I used the template matching with openCv, the algorithme can detect the metched. What i want to do is to copy the template on the region founded by the template matching? is it clear? I want to replace the region detected by the template matching by the template, you know what i mean? Hey guys if you have an idea i will be gratfull. Best regards

sneixum on Jun 3, 2010:

hi.. same problem like doug,

cvMinMaxLoc(res, &minval, &maxval, &minloc, &maxloc, 0);

if (minval < THRESHOLD_VAL) {
  printf("Best match found at (%d, %d)", minloc.x, minloc.y);
} else {
  printf("Not found.");
}


what is THRESHOLD_VAL?
its undeclare variable in the program..

Nash on Jun 4, 2010:

#define THRESHOLD_VAL 0.1

int main(int argc, char** argv)
{
  /* ... the rest of the code ... */
}

amir on Jun 5, 2010:

Thank you very much for the information.

ProstoHam on Jun 8, 2010:

I have been surfing online more than three hours today, yet I never found any interesting article like yours. It’s pretty worth enough for me. In my opinion, if all webmasters and bloggers made good content as you did, the internet will be much more useful than ever before.

Syam on Jul 2, 2010:

Hai
I am trying to develop a web application with face detection for login. If anybody do same project in any language please sent me the algorithm and other details. Thanks in advance

Leave a comment

Name (required)
Email (will not be published) (required)
Website

Characters left = 1000

Tags

Recent Posts

  1. OpenCV Utility: Reading Image Pixels Value
  2. OpenCV Circular ROI
  3. OpenCV 2.0 Installation on Windows XP and Visual Studio 2008
  4. Runtime ROI Selection using Mouse
  5. Real Time Eye Tracking and Blink Detection
View Archives

About the Author

avatar Cool PHP programmer writing cool PHP scripts. Feel free to contact
Tel. +62 31 8662872
+62 856 338 6017
ICQ 489571630
Skype dede_bl4ckheart
Yahoo dede_bl4ckheart
Google nashruddin.amin

Recommended Sites:

Hacker's HTTP Client
HTML and CSS Tutorials
Stop Dreaming Start Action
Online Quran and Translation