RANSAC算法的简单理解

  图像拼接中看到了特征匹配的部分,特征匹配主要是特征点的匹配。在特征点匹配的时候,首先进行粗匹配,粗匹配通常是进行一对匹配点进行对比,误差越小越可能是一对匹配点;精匹配方法中,我们可以用到RANSAC(Random Sample Consensus 随机抽样一致性)算法。

RANSAC可以用于图片的拼接技术。在多幅图像合成时,事先会在待合成的图片中提取一些关键的特征点。计算机视觉的研究表明,不同视角下物体往往可以通过一个透视矩阵(单应矩阵)(3X3或2X2)的变换而得到。RANSAC被用于拟合这个模型的参数(矩阵各行列的值),由此便可识别出不同照片中的同一物体。

(选自:https://blog.csdn.net/l297969586/article/details/52328884

Ransac算法中要用四对特征点对构建一个单应矩阵,关于单应矩阵的介绍参考博客:(https://blog.csdn.net/zhaocj/article/details/78799194

  我们可以理解为,粗匹配是从两幅图像所提取的特征集中,找到特征点之间相对应的特征点对;精匹配是在粗匹配的基础上,再剔除一些不正确的匹配点对。

RANSAC算法步骤:

1.随机选取四对匹配点,计算出一个临时模型参数(单应矩阵)。

2.用该模型参数去测试匹配点对集,统计误差在允许范围内的匹配点对数目(即内点数)。

3.当内点数目占到指定比例时,则认为所选取的匹配点对是合理的。

  第一步选取的匹配点对合理:  根据内点信息重新计算得到最终的模型参数。

  第一步选取的匹配点对不合理:重新选取匹配点对,重复进行模型参数计算,直到选取的特征点对合理。

 

---------------------------------------------------------------------------

 

 

代码来源以及Ransac算法介绍:http://blog.csdn.net/luoshixian099/article/details/50217655

 

实例代码如下:OpenCV中此功能通过调用findHomography函数调用

 

#include <iostream>  
#include "opencv2/opencv.hpp"  
#include "opencv2/core/core.hpp"  
#include "opencv2/features2d/features2d.hpp"  
#include "opencv2/highgui/highgui.hpp"  

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
	Mat obj = imread("obj.jpg");   //载入目标图像  
	Mat scene = imread("scene.jpg"); //载入场景图像  
	if (obj.empty() || scene.empty())
	{
		cout << "Can't open the picture!\n";
		return 0;
	}
	vector<KeyPoint> obj_keypoints, scene_keypoints;
	Mat obj_descriptors, scene_descriptors;
	ORB detector;     //采用ORB算法提取特征点  
	detector.detect(obj, obj_keypoints);
	detector.detect(scene, scene_keypoints);
	detector.compute(obj, obj_keypoints, obj_descriptors);
	detector.compute(scene, scene_keypoints, scene_descriptors);
	BFMatcher matcher(NORM_HAMMING, true); //汉明距离做为相似度度量  
	vector<DMatch> matches;
	matcher.match(obj_descriptors, scene_descriptors, matches);
	Mat match_img;
	drawMatches(obj, obj_keypoints, scene, scene_keypoints, matches, match_img);
	imshow("滤除误匹配前", match_img);

	//保存匹配对序号  
	vector<int> queryIdxs(matches.size()), trainIdxs(matches.size());
	for (size_t i = 0; i < matches.size(); i++)
	{
		queryIdxs[i] = matches[i].queryIdx;
		trainIdxs[i] = matches[i].trainIdx;
	}

	Mat H12;   //变换矩阵  

	vector<Point2f> points1; 
	KeyPoint::convert(obj_keypoints, points1, queryIdxs);
	vector<Point2f> points2; 
	KeyPoint::convert(scene_keypoints, points2, trainIdxs);
	int ransacReprojThreshold = 5;  //拒绝阈值  


	H12 = findHomography(Mat(points1), Mat(points2), CV_RANSAC, ransacReprojThreshold);
	vector<char> matchesMask(matches.size(), 0);
	Mat points1t;
	perspectiveTransform(Mat(points1), points1t, H12);
	for (size_t i1 = 0; i1 < points1.size(); i1++)  //保存‘内点’  
	{
		if (norm(points2[i1] - points1t.at<Point2f>((int)i1, 0)) <= ransacReprojThreshold) //给内点做标记  
		{
			matchesMask[i1] = 1;
		}
	}
	Mat match_img2;   //滤除‘外点’后  
	drawMatches(obj, obj_keypoints, scene, scene_keypoints, matches, match_img2, Scalar(0, 0, 255), Scalar::all(-1), matchesMask);

	//画出目标位置,场景图片矩形
	std::vector<Point2f> obj_corners(4);
	obj_corners[0] = cvPoint(0, 0); obj_corners[1] = cvPoint(obj.cols, 0);
	obj_corners[2] = cvPoint(obj.cols, obj.rows); obj_corners[3] = cvPoint(0, obj.rows);
	std::vector<Point2f> scene_corners(4);
	perspectiveTransform(obj_corners, scene_corners, H12);
	line(match_img2, scene_corners[0] + Point2f(static_cast<float>(obj.cols), 0),
		scene_corners[1] + Point2f(static_cast<float>(obj.cols), 0), Scalar(0, 0, 255), 2);
	line(match_img2, scene_corners[1] + Point2f(static_cast<float>(obj.cols), 0),
		scene_corners[2] + Point2f(static_cast<float>(obj.cols), 0), Scalar(0, 0, 255), 2);
	line(match_img2, scene_corners[2] + Point2f(static_cast<float>(obj.cols), 0),
		scene_corners[3] + Point2f(static_cast<float>(obj.cols), 0), Scalar(0, 0, 255), 2);
	line(match_img2, scene_corners[3] + Point2f(static_cast<float>(obj.cols), 0),
		scene_corners[0] + Point2f(static_cast<float>(obj.cols), 0), Scalar(0, 0, 255), 2);

	imshow("滤除误匹配后", match_img2);
	waitKey(0);

	return 0;
}

 

代码实现的效果:

 

---。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值