Fork me on GitHub

【My Project】图像匹配平台二

图像匹配 平台二

SkySeraph Mar 29th 2011  HQU

Email:zgzhaobo@gmail.com    QQ:452728574

Latest Modified Date:Mar 29th 2011 HQU

一 工具:VS2010+OpenCV2.0

  语言:CPP

原理: 平台一采用的匹配算法相同,但是是采用OpenCV自带函数cvMatchTemplate实现的,相对来说较为简单

三 核心源码

说明:下面匹配源码里包含两种方法,其中是旋转的方法来源于网上,稍微复杂;后面一种直接用cvRectangle描绘出匹配出,比较简单

//匹配
void CTestDlgDlg::OnBnClickedTemplatematch()
{
		if(!image)
		return;
	if(!image_model)
		return;

    CvPoint pp1, pp2;
    CvPoint leftup,rightup,leftdown,rightdown; //定义匹配结果的4个点
	double minVal, maxVal;
	int resultW,resultH; //匹配结果的长宽
 
	image_result=cvCloneImage( image);
	image_rotation=cvCloneImage(image);
	//image_rotation=cvCreateImage(cvSize(image->height,image->width), IPL_DEPTH_32F, 1);
	model_rotation=cvCloneImage(image_model);
    //model_rotation=cvCreateImage(cvSize(image_model->height,image_model->width), IPL_DEPTH_32F, 1);

	///* 第一种方法	
	//第一次匹配,求左上角的点坐标
	resultW=image->width-image_model->width+1;
	resultH=image->height-image_model->height+1;
    match_result=cvCreateImage(cvSize(resultW,resultH), IPL_DEPTH_32F, 1);
	cvMatchTemplate(image, image_model,match_result, CV_TM_CCOEFF); 
	cvMinMaxLoc(match_result,  &minVal, &maxVal, &pp1, &pp2);
	leftup.x=pp2.x;
	leftup.y=pp2.y;

	//第二次匹配,求右上角的点坐标
	ImageRotation(image,image_rotation,90); //把图像逆时针旋转90度
	ImageRotation(image_model,model_rotation,90); //把模板逆时针旋转90度
	//cvNamedWindow ("旋转图像", 1);
    //cvShowImage ("旋转图像", image_rotation);
	//cvNamedWindow ("旋转模板", 1);
    //cvShowImage ("旋转模板", model_rotation);

	resultW=image_rotation->width-model_rotation->width+1;
	resultH=image_rotation->height-model_rotation->height+1;
    cvReleaseImage(&match_result);  //释放第一次匹配结果
	match_result=cvCreateImage(cvSize(resultW,resultH), IPL_DEPTH_32F, 1);
	cvMatchTemplate(image_rotation, model_rotation,match_result, CV_TM_CCOEFF); 
	cvMinMaxLoc(match_result,  &minVal, &maxVal, &pp1, &pp2);
	rightup.x=image_rotation->height-pp2.y;
	rightup.y=pp2.x;

	//第三次匹配,求右下角的点坐标
    
	ImageRotation(image,image_rotation,180); //把图像逆时针旋转90度
	ImageRotation(image_model,model_rotation,180); //把模板逆时针旋转90度

	resultW=image_rotation->width-model_rotation->width+1;
	resultH=image_rotation->height-model_rotation->height+1;
    cvReleaseImage(&match_result);  //释放第二次匹配结果
	match_result=cvCreateImage(cvSize(resultW,resultH), IPL_DEPTH_32F, 1);
	cvMatchTemplate(image_rotation, model_rotation,match_result, CV_TM_CCOEFF); 
	cvMinMaxLoc(match_result,  &minVal, &maxVal, &pp1, &pp2);
	rightdown.x=image_rotation->height-pp2.x;
	rightdown.y=image_rotation->height-pp2.y;

	//第四次匹配,求左下角坐标
	ImageRotation(image,image_rotation,270); //把图像逆时针旋转90度
	ImageRotation(image_model,model_rotation,270); //把模板逆时针旋转90度

	resultW=image_rotation->width-model_rotation->width+1;
	resultH=image_rotation->height-model_rotation->height+1;
    cvReleaseImage(&match_result);  //释放第二次匹配结果
	match_result=cvCreateImage(cvSize(resultW,resultH), IPL_DEPTH_32F, 1);
	cvMatchTemplate(image_rotation, model_rotation,match_result, CV_TM_CCOEFF); 
	cvMinMaxLoc(match_result,  &minVal, &maxVal, &pp1, &pp2);
	leftdown.x=pp2.y;
	leftdown.y=image_rotation->height-pp2.x;

	//cvCircle( image_rotation, rightup, 10,CV_RGB(255,0,0), 2 );
	cvLine(image_result,leftup,rightup,CV_RGB(255,0,0),2);
	cvLine(image_result,rightup,rightdown,CV_RGB(255,0,0),2);
	cvLine(image_result,rightdown,leftdown,CV_RGB(255,0,0),2);
	cvLine(image_result,leftdown,leftup,CV_RGB(255,0,0),2);
	//cvRectangle(image_result,leftup,rightdown,CV_RGB(255,0,0),2);
	DrawPicToHDC(image_result, IDC_PICSHOW3);
	//*/

	/*  第二种方法
	resultW=image->width-image_model->width+1;
	resultH=image->height-image_model->height+1;
    match_result=cvCreateImage(cvSize(resultW,resultH), IPL_DEPTH_32F, 1);
	cvMatchTemplate(image, image_model,match_result, CV_TM_CCOEFF); 
	cvMinMaxLoc(match_result,  &minVal, &maxVal, &pp1, &pp2);

    CvPoint pt1;
    CvPoint pt2;
    CvRect rect;
    rect=cvRect(pp2.x,pp2.y,image_model->width,image_model->height);//最佳的匹配区域
    pt1=cvPoint(rect.x,rect.y);
    pt2=cvPoint(rect.x+rect.width,rect.y+rect.height);
    cvRectangle( image,pt1, pt2, cvScalar(0,0,255),1, 8, 0 );
	
	DrawPicToHDC(image, IDC_PICSHOW3);
	*/
}

四 效果

 

说明:第二幅模板图进行了放大,所以有失真效果,为了美观,可以另外再定义一个显示函数,根据图像大小显示而不是根据控件大小显示,此处略

 

Author:         SKySeraph

Email/GTalk: zgzhaobo@gmail.com    QQ:452728574

From:         http://www.cnblogs.com/skyseraph/

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,请尊重作者的

posted @ 2011-03-29 10:57  SkySeraph  阅读(2587)  评论(0编辑  收藏  举报