打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
opencv图像匹配
//图像配准
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);//复制图片
cvResetImageROI(image);//还原截图还原截图
CvPoint pp1, pp2;
    CvPointleftup,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);
//lefttop
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;
//righttop
Globle g;
g.ImageRotation(image,image_rotation,90); //把图像逆时针旋转90度
g.ImageRotation(image_model,model_rotation,90);//把模板逆时针旋转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);
rightup.x=image_rotation->width-pp2.y-45;
rightup.y=pp2.x-45;
//rightdown
g.ImageRotation(image,image_rotation,180); //把图像逆时针旋转90度
g.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->width-pp2.x;
rightdown.y=image_rotation->height-pp2.y;
//leftdown
g.ImageRotation(image,image_rotation,270); //把图像逆时针旋转90度
g.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+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);
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
ios 照片编辑的view封装
人工智能|利用keras和tensorflow探索数据增强
Image Viewer UserControl
48.Formatting Pictures, Screenshots, and Characters
OpenCV之摄像机标定
06423760
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服