cvMatchTemplate() 模板匹配

不是基于直方图的匹配,使用起来非常方便


函数原型:

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;
}

运行结果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值