problem with cvMatchTemplate method [closed]

asked 2014-05-20 04:28:15 -0500

updated 2014-05-20 08:41:40 -0500

Witek gravatar image

hi there, I'm new in openCV and want to using of cvMatchTemplate but the result of this method is not desirable. My program is:

IplImage *t = cvLoadImage("template.jpg",1);
IplImage *i = cvLoadImage("source.jpg",1);
IplImage *res = cvCreateImage(cvSize(i->width - t->width+1,i->height - t->height +1),IPL_DEPTH_32F, 1);
cvMatchTemplate(i, t, res, CV_TM_CCORR_NORMED);
CvPoint minloc, maxloc;
double minval, maxval;
cvRectangle(i, cvPoint(maxloc.x, maxloc.y), cvPoint(maxloc.x + 50, maxloc.y + 50), cvScalar(250,250,250), 2, 0, 0);
cvShowImage("Matched area",i);

but after running it the result image is:

image description

and my source and template images are:

image description image description

(in result image a bigger rectangle is matched with template...!!!).

Help me to use of template macthing in openCV, Special thanks to you.

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by sturkmen
close date 2020-10-06 07:34:21.669437


Suggestion 1, move away from the old and depricated C API and use the new bugfree C++ API ...

StevenPuttemans gravatar imageStevenPuttemans ( 2014-05-20 04:45:01 -0500 )edit

Generally, template matching is very sensitive to size and orientation. If your template is not of similar size and/or is rotated with respect to the real object in the source image, it will usually not be found. In your case, it looks like the template is much bigger than the object (face) in the source image. Try to cut out the face from the source image and use it as the template. This should work.

Witek gravatar imageWitek ( 2014-05-20 08:36:30 -0500 )edit

thank you, but it is important for me that i don't want to cut my template from source. (my template could be a few defferenc with source)

user.opencv gravatar imageuser.opencv ( 2014-05-21 00:26:03 -0500 )edit

That is perfectly understandable that you don't want to cut your template from source. I suggested that so you could verify that the code works correctly. And as I mentioned, template matching will not work for templates that are clearly different from the target that you are looking for. If you want to identify faces of football players, template matching is not the way to go. Read on face detection and face recognition. But it's not going to be easy.

Witek gravatar imageWitek ( 2014-05-21 14:26:48 -0500 )edit

This is just an example for my purpose an not my final goal.I want generally to find a template in a source image and I'm not following face detection or anything like that.

user.opencv gravatar imageuser.opencv ( 2014-05-24 01:26:16 -0500 )edit