Wednesday, October 29, 2008

[OpenCV]: match template (cvMatchTemplate)

#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include <stdio.h>
int main( int argc, char** argv )
{
IplImage *src, *templ,*ftmp[6]; //ftmp is what to display on
int i;

//Read in the template to be used for matching:
if((templ=cvLoadImage("template.jpg", 1))== 0) {
printf("Error on reading template %s\n","temp");
return(-1);
}

//Read in the source image to be searched:
if((src=cvLoadImage("source.jpg", 1))== 0) {
printf("Error on reading src image %s\n","source");
return(-1);
}

int patchx = templ->width;
int patchy = templ->height;
int iwidth = src->width - patchx + 1;
int iheight = src->height - patchy + 1;
for(i=0; i<6; ++i){
ftmp[i] = cvCreateImage( cvSize(iwidth,iheight),32,1);
}

//DO THE MATCHING OF THE TEMPLATE WITH THE IMAGE
for(i=0; i<6; ++i){
cvMatchTemplate( src, templ, ftmp[i], i);
cvNormalize(ftmp[i],ftmp[i],1,0,CV_MINMAX);
}
//DISPLAY
cvNamedWindow( "Template", 0 );
cvShowImage( "Template", templ );
cvNamedWindow( "Image", 0 );
cvShowImage( "Image", src );

cvNamedWindow( "SQDIFF", 0 );
cvShowImage( "SQDIFF", ftmp[0] );
cvNamedWindow( "SQDIFF_NORMED", 0 );
cvShowImage( "SQDIFF_NORMED", ftmp[1] );
cvNamedWindow( "CCORR", 0 );
cvShowImage( "CCORR", ftmp[2] );

cvNamedWindow( "CCORR_NORMED", 0 );
cvShowImage( "CCORR_NORMED", ftmp[3] );

cvNamedWindow( "CCOEFF", 0 );
cvShowImage( "CCOEFF", ftmp[4] );

cvNamedWindow( "CCOEFF_NORMED", 0 );
cvShowImage( "CCOEFF_NORMED", ftmp[5] );
cvWaitKey(0);
}

2 comments:

  1. Thanks! This is really helpful for an image stitching algorithm I'm implementing.

    ReplyDelete
  2. Anonymous12:29 AM

    I'm delighted that I have observed this weblog. Finally anything not a junk, which we go through incredibly frequently. The website is lovingly serviced and kept up to date. So it need to be, thank you for sharing this with us.
    windows 7

    ReplyDelete