problem with cvMatchTemplate method [closed]
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);
cvZero(res);
cvMatchTemplate(i, t, res, CV_TM_CCORR_NORMED);
CvPoint minloc, maxloc;
double minval, maxval;
cvMinMaxLoc(res,&minval,&maxval,&minloc,&maxloc,NULL);
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:
and my source and template images are:
(in result image a bigger rectangle is matched with template...!!!).
Help me to use of template macthing in openCV, Special thanks to you.
Suggestion 1, move away from the old and depricated C API and use the new bugfree C++ API ...
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.
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)
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.
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.