//圖像配準(zhǔn)
void Match::image_match()
{
IplImage *image;
IplImage
*image_model,*image_result,*image_rotation,*model_rotation,*match_result;
image=cvLoadImage("oil.jpg",0);
CvSize size;
size.height=image->height/2;
size.width=image->width/2;
image_model=cvCreateImage(size,image->depth,image->nChannels);
//cvResize(image,image_model,CV_INTER_LINEAR);
cvSetImageROI(image,cvRect(100,110,size.width,size.height));//截圖
cvCopy(image,image_model,NULL);//復(fù)制圖片
cvResetImageROI(image);//還原截圖還原截圖
CvPoint pp1, pp2;
double minVal, maxVal;
int resultW,resultH; //匹配結(jié)果的長寬
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);
//lefttop
resultW=image->width-image_model->width+1;
resultH=image->height-image_model->height+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;
//righttop
Globle g;
g.ImageRotation(image,image_rotation,90); //把圖像逆時(shí)針旋轉(zhuǎn)90度
g.ImageRotation(image_model,model_rotation,90);
//把模板逆時(shí)針旋轉(zhuǎn)90度
resultW=image_rotation->width-model_rotation->width+1;
resultH=image_rotation->height-model_rotation->height+1;
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->width-pp2.y-45;
rightup.y=pp2.x-45;
//rightdown
g.ImageRotation(image,image_rotation,180); //把圖像逆時(shí)針旋轉(zhuǎn)90度
g.ImageRotation(image_model,model_rotation,180);
//把模板逆時(shí)針旋轉(zhuǎn)90度
resultW=image_rotation->width-model_rotation->width+1;
resultH=image_rotation->height-model_rotation->height+1;
match_result=cvCreateImage(cvSize(resultW,resultH),
IPL_DEPTH_32F, 1);
cvMatchTemplate(image_rotation, model_rotation,match_result,
CV_TM_CCOEFF);
cvMinMaxLoc(match_result,
rightdown.x=image_rotation->width-pp2.x;
rightdown.y=image_rotation->height-pp2.y;
//leftdown
g.ImageRotation(image,image_rotation,270); //把圖像逆時(shí)針旋轉(zhuǎn)90度
g.ImageRotation(image_model,model_rotation,270);
//把模板逆時(shí)針旋轉(zhuǎn)90度
resultW=image_rotation->width-model_rotation->width+1;
resultH=image_rotation->height-model_rotation->height+1;
match_result=cvCreateImage(cvSize(resultW,resultH),
IPL_DEPTH_32F, 1);
cvMatchTemplate(image_rotation, model_rotation,match_result,
CV_TM_CCOEFF);
cvMinMaxLoc(match_result,
leftdown.x=pp2.y+45;
leftdown.y=image_rotation->height-pp2.x+45;
cvLine(image,leftup,rightup,CV_RGB(0,0,0),2);
cvLine(image,rightup,rightdown,CV_RGB(0,0,0),2);
cvLine(image,rightdown,leftdown,CV_RGB(0,0,0),2);
cvLine(image,leftdown,leftup,CV_RGB(0,0,0),2);
//顯示
printf("width:%d,hight:%d\n",size.width,size.height);
printf("lefttop:%d,%d\n",leftup.x,leftup.y);
printf("rightup:%d,%d\n",rightup.x,rightup.y);
printf("rightdown:%d,%d\n",rightdown.x,rightdown.y);
printf("leftdown:%d,%d\n",leftdown.x,leftdown.y);
cvNamedWindow("window",1);
cvShowImage("window",image);
cvNamedWindow("window2",1);
cvShowImage("window2",image_model);
cvWaitKey(0);
cvDestroyWindow("window");
cvReleaseImage(&image);
cvDestroyWindow("window2");
cvReleaseImage(&image_model);
}
|
|