效果:
代码:
#include "cv.h"#include "cxcore.h"#include "highgui.h"#includeint MatchTemplate(int argc,char** argv){IplImage* temp=cvLoadImage("e:\\picture\\tou.jpg");IplImage* src=cvLoadImage("e:\\picture\\4.jpg");IplImage* result[6];CvPoint MaxPt[6];for(int i=0;i<6;i++){result[i]=cvCreateImage(cvSize(src->width-temp->width+1,src->height-temp->height+1),IPL_DEPTH_32F,1);cvZero(result[i]);cvMatchTemplate(src,temp,result[i],i); //模板匹配,分别用6种方法cvNormalize(result[i],result[i],1,0,CV_MINMAX); //归一化,把结果缩放到0~1范围内cvPow(result[i],result[i],3); //结果的三次方,小的更小,大的基本不变(因为范围是0~1)cvMinMaxLoc(result[i],NULL,NULL,NULL,&MaxPt[i]); //获取最大值坐标MaxPt[i].x=MaxPt[i].x+cvRound(temp->width/2); //修正最大值在原图的坐标,即查找结果MaxPt[i].y=MaxPt[i].y+cvRound(temp->height/2);}for(int i=0;i<6;i++){cvCircle(src,MaxPt[i],2,cvScalar(0,0,255),2); //在原图上标明结果坐标}cvNamedWindow("src");cvNamedWindow("temp");cvNamedWindow("result1");cvNamedWindow("result2");cvNamedWindow("result3");cvNamedWindow("result4");cvNamedWindow("result5");cvNamedWindow("result6");cvShowImage("src",src);cvShowImage("temp",temp);cvShowImage("result1",result[0]);cvShowImage("result2",result[1]);cvShowImage("result3",result[2]);cvShowImage("result4",result[3]);cvShowImage("result5",result[4]);cvShowImage("result6",result[5]);cvWaitKey(0);return 0;}