图像算法:模板匹配
SkySeraph Mar 29th 2011 HQU
Email:zgzhaobo@gmail.com QQ:452728574
Latest Modified Date:Mar 29th 2011 HQU
一 工具:VC6.0+OpenCV1.0
语言:CPP
二 原理:
1 函数 cvMatchTemplate
//功能:比较模板和重叠的图像区域
//原型:void cvMatchTemplate(
const CvArr* image, //欲搜索的图像。它应该是单通道、8-比特或32-比特 浮点数图像
const CvArr* templ, //搜索模板,不能大于输入图像,且与输入图像具有一样的数据类型
CvArr* result, //比较结果的映射图像。单通道、32-比特浮点数.
若图像是W×H而templ是w×h,则result一定是(W-w+1)×(H-h+1)
int method //CV_TM_SQDIFF、CV_TM_SQDIFF_NORMED、CV_TM_CCORR、
CV_TM_CCORR_NORMED、CV_TM_CCOEFF、CV_TM_CCOEFF_NORMED
);
//说明:
关于参数result[2]:
含义:依次计算模板与待测图片的重叠区域的相似度,result图像中的每一个点的值代表了一次相似度比较结果
尺寸:横向比较W-w+1次,纵向比较H-h+1次
获取最佳匹配区域:
使用函数cvMinMaxLoc提取最大值(相似度最高)以及最大值的位置
rect=cvRect(max_loc.x,max_loc.y,tmp->width,tmp->height);
rect表示的矩形区域即是最佳的匹配区域
关于参数 method:
CV_TM_SQDIFF 平方差匹配法:该方法采用平方差来进行匹配;最好的匹配值为0;匹配越差,匹配值越大。
CV_TM_CCORR 相关匹配法:该方法采用乘法操作;数值越大表明匹配程度越好。
CV_TM_CCOEFF 相关系数匹配法:1表示完美的匹配;-1表示最差的匹配。
CV_TM_SQDIFF_NORMED 归一化平方差匹配法
CV_TM_CCORR_NORMED 归一化相关匹配法
CV_TM_CCOEFF_NORMED 归一化相关系数匹配法
2 函数:cvMinMaxLoc
/*-----------------cvMinMaxLoc------------------//
//功能:查找数组和子数组的全局最小值和最大值
//函数:
void cvMinMaxLoc(
const CvArr* arr, //输入数组, 单通道或者设置了 COI 的多通道
double* min_val, //指向返回的最小值的指针
double* max_val,//指向返回的最大值的指针
CvPoint* min_loc=NULL, //指向返回的最小值的位置指针
CvPoint* max_loc=NULL, //指向返回的最大值的位置指针
const CvArr* mask=NULL//选择一个子数组的操作掩模
);
//----------------------------------------------*/
三 源码(完整):
/*===============================================// 功能:Template Matching with OpenCV 时间:3/22/2011 SkySeraph HQU //===============================================*/ #include "iostream" using namespace std; #include "cv.h" #include "highgui.h" #include "math.h" #pragma comment(lib,"highgui.lib") #pragma comment(lib,"cv.lib") #pragma comment(lib,"cvaux.lib") #pragma comment(lib,"cxcore.lib") const char* filename1 = "D:\\My Documents\\My Pictures\\Images\\归一化.bmp"; const char* filename2 = "D:\\My Documents\\My Pictures\\Images\\归一化模板.bmp"; /*=============================================*/ int main( int argc, char** argv ) { IplImage *img; IplImage *tpl; IplImage *res; CvPoint minloc, maxloc; double minval, maxval; int img_width, img_height; int tpl_width, tpl_height; int res_width, res_height; img = cvLoadImage( filename1, CV_LOAD_IMAGE_COLOR ); tpl = cvLoadImage( filename2, CV_LOAD_IMAGE_COLOR ); if( tpl == 0 ) { fprintf( stderr, "Cannot load file %s!\n", argv[2] ); return 1; } cvNamedWindow( "src", CV_WINDOW_AUTOSIZE ); cvShowImage( "src", img ); cvNamedWindow( "template", CV_WINDOW_AUTOSIZE ); cvShowImage( "template", tpl ); /* get image's properties */ img_width = img->width; img_height = img->height; tpl_width = tpl->width; tpl_height = tpl->height; res_width = img_width - tpl_width + 1; res_height = img_height - tpl_height + 1; /* create new image for template matching computation */ res = cvCreateImage( cvSize( res_width, res_height ), IPL_DEPTH_32F, 1 ); /* choose template matching method to be used */ cvMatchTemplate( img, tpl, res, CV_TM_CCORR_NORMED ); /*cvMatchTemplate( img, tpl, res, CV_TM_SQDIFF_NORMED ); cvMatchTemplate( img, tpl, res, CV_TM_CCORR ); cvMatchTemplate( img, tpl, res, CV_TM_SQDIFF ); cvMatchTemplate( img, tpl, res, CV_TM_CCOEFF ); cvMatchTemplate( img, tpl, res, CV_TM_CCOEFF_NORMED );*/ cvNamedWindow( "res", CV_WINDOW_AUTOSIZE ); cvShowImage( "res", res ); cvMinMaxLoc( res, &minval, &maxval, &minloc, &maxloc, 0 ); /* draw red rectangle */ /*cvRectangle( img, cvPoint( minloc.x, minloc.y ), cvPoint( minloc.x + tpl_width, minloc.y + tpl_height ), cvScalar( 0, 0, 255, 0 ), 1, 0, 0 );*/ CvPoint pt1; CvPoint pt2; CvRect rect; rect=cvRect(maxloc.x,maxloc.y,tpl->width,tpl->height);//最佳的匹配区域 pt1=cvPoint(rect.x,rect.y); pt2=cvPoint(rect.x+rect.width,rect.y+rect.height); cvRectangle( img,pt1, pt2, cvScalar(0,0,255),1, 8, 0 ); /* display images */ cvNamedWindow( "reference", CV_WINDOW_AUTOSIZE ); cvShowImage( "reference", img ); /* wait until user press a key to exit */ cvWaitKey( 0 ); /* free memory */ cvDestroyWindow( "reference" ); cvDestroyWindow( "template" ); cvReleaseImage( &img ); cvReleaseImage( &tpl ); cvReleaseImage( &res ); return 0; }
四 效果:
五 参考:
[1] nashruddin.com http://nashruddin.com/template-matching-in-opencv-with-example.html
[2] http://www.52its.cn/Article/show.asp?id=135
六 其它:
平台: http://www.cnblogs.com/skyseraph/archive/2011/03/29/1998492.html
http://www.cnblogs.com/skyseraph/archive/2011/03/29/1998536.html
Author: SKySeraph
Email/GTalk: zgzhaobo@gmail.com QQ:452728574
From: http://www.cnblogs.com/skyseraph/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,请尊重作者的劳动成果。