您的位置:首页 > 其它

RANSAC算法的简单理解

2018-01-09 15:49 155 查看
图像拼接中看到了特征匹配的部分,特征匹配主要是特征点的匹配,通常利用特征点提取算法寻找出的特征点。在特征点匹配的时候,首先进行粗匹配,粗匹配通常是进行一对匹配点进行对比,误差越小越可能是一对匹配点;精匹配方法中,我们可以用到RANSAC算法。

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

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;
}


代码实现的效果:



---。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: