opencv ransac

	Mat img11=imread("/home/daizhou/Holidays/jpg/100000.jpg");
	Mat img12=imread("/home/daizhou/Holidays/jpg/100002.jpg");
	Mat img1,img2;
	cv::resize(img11,img1,cv::Size(256,256));
	cv::resize(img12,img2,cv::Size(256,256));
	
	Mat gray_image1,gray_image2;
	cvtColor(img1,gray_image1,CV_RGB2GRAY);
	cvtColor(img2,gray_image2,CV_RGB2GRAY);
	SurfFeatureDetector detector(400);
	std::vector< KeyPoint > keypoints_object, keypoints_scene;
	detector.detect( gray_image1, keypoints_object );
	detector.detect(gray_image2,keypoints_scene);                              
	SurfDescriptorExtractor extractor;
	Mat descriptors_object, descriptors_scene;
	extractor.compute( gray_image1, keypoints_object, descriptors_object );
    extractor.compute( gray_image2, keypoints_scene, descriptors_scene );
	
	Mat descriptors_object1,descriptors_scene1;
    if(descriptors_object.type()!=CV_32F){
       descriptors_object.convertTo(descriptors_object1,CV_32F);
	   descriptors_scene.convertTo(descriptors_scene1,CV_32F);
	}else{
       descriptors_object1=descriptors_object;
	   descriptors_scene1=descriptors_scene;
	}
    FlannBasedMatcher matcher;
    std::vector< DMatch > matches;
    matcher.match( descriptors_object1, descriptors_scene1, matches );
	cout<<"matches:"<<matches.size()<<endl;
	vector<cv::Point2f> matchPoint0,matchPoint1;
	
	for(int i=0;i<matches.size();++i){
        matchPoint0.push_back(keypoints_object[matches[i].queryIdx].pt);
		matchPoint1.push_back(keypoints_scene[matches[i].trainIdx].pt);
	}
	Mat H = findHomography(matchPoint0,matchPoint1,CV_RANSAC,3);
	cv::Point2f point0,point1;
	double scale;
		for (int i = 0;i < matches.size();i ++)
	{
		point0 = keypoints_object[matches[i].queryIdx].pt;
		point1 = keypoints_scene[matches[i].trainIdx].pt;

		scale     =  point0.x*H.at<double>(2,0)+point0.y*H.at<double>(2,1)+H.at<double>(2,2);
		point1.x -= (point0.x*H.at<double>(0,0)+point0.y*H.at<double>(0,1)+H.at<double>(0,2))/scale;
		point1.y -= (point0.x*H.at<double>(1,0)+point0.y*H.at<double>(1,1)+H.at<double>(1,2))/scale;

		if (point1.x*point1.x + point1.y*point1.y > 100)
		{
			matches.erase(matches.begin()+i);
			i --;
		}

	}
		cout<<matches.size()<<endl;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值