不是基于直方图的匹配,使用起来非常方便
函数原型:
CVAPI(void) cvMatchTemplate( const CvArr* image, const CvArr* templ,
CvArr* result, int method );
image,原图像
templ,需要查找的图块
result,输出目标
method ,匹配方法
其中result的图像大小为:( image.weight - templ.weight+1 , image.height - templ.height+1 )
method 有好几种方法:
enum
{
CV_TM_SQDIFF =0, //平方差匹配法
CV_TM_SQDIFF_NORMED =1, //
CV_TM_CCORR =2, //相关匹配法
CV_TM_CCORR_NORMED =3, //
CV_TM_CCOEFF =4, //相关匹配法
CV_TM_CCOEFF_NORMED =5 //
};
测试图像:
find.jpg
test.jpg
程序:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "cv.h"
#include "highgui.h"
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
double min_val,max_val;
CvPoint min_loc,max_loc;
IplImage *img_temp = cvLoadImage("find.jpg");
IplImage *img_in = cvLoadImage("test.jpg");
IplImage *ftmp = cvCreateImage(cvSize(img_in->width-img_temp->width+1,img_in->height-img_temp->height+1),IPL_DEPTH_32F,1);
cvMatchTemplate(img_in,img_temp,ftmp,CV_TM_SQDIFF);
cvMinMaxLoc(ftmp,&min_val,&max_val,&min_loc,&max_loc,NULL);
cvRectangle(img_in, cvPoint(min_loc.x,min_loc.y),cvPoint((min_loc.x+img_temp->width),(min_loc.y+img_temp->height)),CV_RGB(0,255,0),1);
cvNamedWindow("img_in");
cvShowImage("img_in",img_in);
printf("x_location=%d;y_location=%d",min_loc.x,min_loc.y);
printf("min_val=%d;max_val=%d",min_val,max_val);
cvWaitKey(0);
cvReleaseImage(&img_in);
cvReleaseImage(&img_temp);
cvReleaseImage(&ftmp);
cvDestroyAllWindows();
return 0;
}
运行结果: