分享

opencv图像匹配

 昵称20590214 2014-12-30
//图像配准
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;
    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);
//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);
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约