cvMatchTemplate
IplImage *src, *templ,*ftmp[6];
int i;
double minval , maxval;
CvPoint minloc ,maxloc;
src=cvLoadImage("test.jpg", 1);
templ=cvLoadImage("temp.jpg", 1);
int iwidth = src->width - templ->width + 1;
int iheight = src->height - templ->height + 1;
for(i=0; i<6; ++i){
ftmp[i] = cvCreateImage( cvSize(iwidth,iheight),32,1);
}
//進行匹配,取各圖的最高最低值並歸一畫
for(i=0; i<6; ++i){
cvMatchTemplate( src, templ, ftmp[i], i);
cvMinMaxLoc(ftmp[i] ,&minval ,&maxval ,&minloc ,&maxloc);
cvNormalize(ftmp[i],ftmp[i],1,0,CV_MINMAX);
//在原圖劃出偵測到的位置(在此試取最高值)
cvLine(src , maxloc , cvPoint(maxloc.x + templ->width ,maxloc.y) ,CV_RGB(255,0,0) , 1, 8,0);
cvLine(src , maxloc , cvPoint(maxloc.x ,maxloc.y + templ->height) ,CV_RGB(255,0,0) , 1, 8,0);
cvLine(src , cvPoint(maxloc.x + templ->width ,maxloc.y) , cvPoint(maxloc.x + templ->width ,maxloc.y + templ->height) ,CV_RGB(255,0,0) , 1, 8,0);
cvLine(src , cvPoint(maxloc.x ,maxloc.y + templ->height ) , cvPoint(maxloc.x + templ->width ,maxloc.y + templ->height) ,CV_RGB(255,0,0) , 1, 8,0);
}
cvNamedWindow( "Template", 1 );
cvShowImage( "Template", templ );
cvNamedWindow( "Image", 1 );
cvShowImage( "Image", src );
cvNamedWindow( "SQDIFF", 0 );
cvResizeWindow("SQDIFF" ,templ->width ,templ->height);
cvShowImage( "SQDIFF", ftmp[0] );
cvNamedWindow( "SQDIFF_NORMED", 0 );
cvResizeWindow("SQDIFF_NORMED" ,templ->width ,templ->height);
cvShowImage( "SQDIFF_NORMED", ftmp[1] );
cvNamedWindow( "CCORR", 0 );
cvResizeWindow("CCORR" ,templ->width ,templ->height);
cvShowImage( "CCORR", ftmp[2] );
cvNamedWindow( "CCORR_NORMED", 0 );
cvResizeWindow("CCORR_NORMED" ,templ->width ,templ->height);
cvShowImage( "CCORR_NORMED", ftmp[3] );
cvNamedWindow( "CCOEFF", 0 );
cvResizeWindow("CCOEFF" ,templ->width ,templ->height);
cvShowImage( "CCOEFF", ftmp[4] );
cvNamedWindow( "CCOEFF_NORMED", 0 );
cvResizeWindow("CCOEFF_NORMED" ,templ->width ,templ->height);
cvShowImage( "CCOEFF_NORMED", ftmp[5] );
cvWaitKey(0);
參考
http://mengtsan.blogspot.com/2010/12/opencv-cvmatchtemplate.html
http://blog.weisu.org/2008/10/opencv-match-template-cvmatchtemplate.html
http://www.opencv.org.cn/index.php/Cxcore%E6%95%B0%E7%BB%84%E6%93%8D%E4%BD%9C
int i;
double minval , maxval;
CvPoint minloc ,maxloc;
src=cvLoadImage("test.jpg", 1);
templ=cvLoadImage("temp.jpg", 1);
int iwidth = src->width - templ->width + 1;
int iheight = src->height - templ->height + 1;
for(i=0; i<6; ++i){
ftmp[i] = cvCreateImage( cvSize(iwidth,iheight),32,1);
}
//進行匹配,取各圖的最高最低值並歸一畫
for(i=0; i<6; ++i){
cvMatchTemplate( src, templ, ftmp[i], i);
cvMinMaxLoc(ftmp[i] ,&minval ,&maxval ,&minloc ,&maxloc);
cvNormalize(ftmp[i],ftmp[i],1,0,CV_MINMAX);
//在原圖劃出偵測到的位置(在此試取最高值)
cvLine(src , maxloc , cvPoint(maxloc.x + templ->width ,maxloc.y) ,CV_RGB(255,0,0) , 1, 8,0);
cvLine(src , maxloc , cvPoint(maxloc.x ,maxloc.y + templ->height) ,CV_RGB(255,0,0) , 1, 8,0);
cvLine(src , cvPoint(maxloc.x + templ->width ,maxloc.y) , cvPoint(maxloc.x + templ->width ,maxloc.y + templ->height) ,CV_RGB(255,0,0) , 1, 8,0);
cvLine(src , cvPoint(maxloc.x ,maxloc.y + templ->height ) , cvPoint(maxloc.x + templ->width ,maxloc.y + templ->height) ,CV_RGB(255,0,0) , 1, 8,0);
}
cvNamedWindow( "Template", 1 );
cvShowImage( "Template", templ );
cvNamedWindow( "Image", 1 );
cvShowImage( "Image", src );
cvNamedWindow( "SQDIFF", 0 );
cvResizeWindow("SQDIFF" ,templ->width ,templ->height);
cvShowImage( "SQDIFF", ftmp[0] );
cvNamedWindow( "SQDIFF_NORMED", 0 );
cvResizeWindow("SQDIFF_NORMED" ,templ->width ,templ->height);
cvShowImage( "SQDIFF_NORMED", ftmp[1] );
cvNamedWindow( "CCORR", 0 );
cvResizeWindow("CCORR" ,templ->width ,templ->height);
cvShowImage( "CCORR", ftmp[2] );
cvNamedWindow( "CCORR_NORMED", 0 );
cvResizeWindow("CCORR_NORMED" ,templ->width ,templ->height);
cvShowImage( "CCORR_NORMED", ftmp[3] );
cvNamedWindow( "CCOEFF", 0 );
cvResizeWindow("CCOEFF" ,templ->width ,templ->height);
cvShowImage( "CCOEFF", ftmp[4] );
cvNamedWindow( "CCOEFF_NORMED", 0 );
cvResizeWindow("CCOEFF_NORMED" ,templ->width ,templ->height);
cvShowImage( "CCOEFF_NORMED", ftmp[5] );
cvWaitKey(0);
參考
http://mengtsan.blogspot.com/2010/12/opencv-cvmatchtemplate.html
http://blog.weisu.org/2008/10/opencv-match-template-cvmatchtemplate.html
http://www.opencv.org.cn/index.php/Cxcore%E6%95%B0%E7%BB%84%E6%93%8D%E4%BD%9C
留言
張貼留言