本文作者:longlongago 博客地址:http://blog.csdn.net/longlongago2000
在学习OpenCV,很辛苦才找到相关的例子,在这里共享给大家,这里是在加入到我的程序中提取出来的:
void CFollowDlg::MatchTemplate(CString path){
//double a=0.;
IplImage* imgSrc = cvLoadImage(path);
IplImage* imgTemp = cvLoadImage("C://t.bmp");
CvSize sizeSrc = cvGetSize(imgSrc);
CvSize sizeTemp = cvGetSize(imgTemp);
CvSize sizeResult = cvSize(sizeSrc.width-sizeTemp.width+1,sizeSrc.height-sizeTemp.height+1);
IplImage* imgResult = cvCreateImage(sizeResult,IPL_DEPTH_32F,1);
cvMatchTemplate(imgSrc,imgTemp,imgResult,CV_TM_CCORR_NORMED);
// a=cvMatchShapes(imgSrc,imgTemp,CV_CONTOURS_MATCH_I3);
float dMax = 0.;
CvPoint point = cvPoint(0,0);
//if(!a) MessageBox("Not Successful!",MB_OK);
for (int cx=0 ; cx<sizeResult.width ; cx++)
{
for (int cy=0 ; cy<sizeResult.height ; cy++)
{
float fTemp = CV_IMAGE_ELEM(imgResult,float,cy,cx);
if (dMax < fTemp)
{
dMax = fTemp;
point = cvPoint(cx,cy);
}
}
}
CvPoint point2 = cvPoint(point.x+sizeTemp.width,point.y+sizeTemp.height);
cvRectangle(imgSrc,point,point2,cvScalar(255));
cvNamedWindow( "Test", CV_WINDOW_AUTOSIZE );
cvShowImage("Test",imgSrc);
/* for(;;)
{
int k = cvWaitKey(5);
if( k == 27 ) break;
}
*/
}
void CFollowDlg::OnButton4()
{
// TODO: Add your control notification handler code here
CString path;
CFileDialog dlg(TRUE);
if(dlg.DoModal()==IDOK)
path=dlg.GetPathName();
MatchTemplate(path);
}
希望对大家有所帮助!