为了得到更多的匹配,可采用随机采样一致性(RANSAC)方法来进行异常过滤。若希望所使用图像是刚性的,只需要在模式图像和查询图像间找到单应性变换即可。
图像校正—透视变换
RANSAC算法原理与源码解析
以下代码使用了比率测试来删除离群值。使用单应细化来找到更多的匹配。
#include<opencv2/opencv.hpp>
#include<opencv2/features2d/features2d.hpp>
#include<opencv2/xfeatures2d/nonfree.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/core/core.hpp>
#include<iostream>
using namespace std;
using namespace cv;
using namespace cv::xfeatures2d;
bool refineMatchesWithHomography(const vector<KeyPoint>&query,
const vector<KeyPoint>&train,
float reprojectionThreashold,
vector<DMatch>&matches,
Mat& homography)
{
const int minNumMatchesAllowed = 8;
if (matches.size() < minNumMatchesAllowed)
return false;
vector<Point2f>srcPoints(matches.size());
vector<Point2f>dstPoints(matches.size());
for (int i = 0; i < matches.size(); i++)
{
srcPoints[i] = train[matches[i].trainIdx].pt;
dstPoints[i] = query[matches[i].queryIdx].pt;
}
vector<unsigned char>inlierMask(srcPoints.size());
homography = findHomography(srcPoints,dstPoints,CV_FM_RANSAC,reprojectionThreashold,inlierMask);
vector<DMatch> inliers;
for (int i = 0; i < inlierMask.size(); i++)
{
if (inlierMask[i])
{
inliers.push_back(matches[i]);
}
}
matches.swap(inliers);
return matches.size() > minNumMatchesAllowed;
}
int main()
{
Mat srcImage1, srcImage2;
srcImage1 = imread("2.jpg",1);
srcImage2 = imread("1.jpg",1);
if (!srcImage1.data || !srcImage2.data)
{
cout << "读取数据出错" << endl;
return false;
}
imshow("原图1",srcImage1);
imshow("原图2",srcImage2);
Mat srcImage1_gray, srcImage2_gray;
cvtColor(srcImage1,srcImage2_gray,CV_BGR2GRAY);
cvtColor(srcImage2,srcImage2_gray,CV_BGR2GRAY);
Ptr<SurfFeatureDetector> detector = SurfFeatureDetector::create(400);
vector<KeyPoint>keypoints1, keypoints2;
Mat dstImage1, dstImage2;
detector->detectAndCompute(srcImage1, Mat(), keypoints1, dstImage1);
detector->detectAndCompute(srcImage2, Mat(), keypoints2, dstImage2);
FlannBasedMatcher matcher;
vector<Mat>train_desc_collection(1,dstImage2);
matcher.add(train_desc_collection);
matcher.train();
vector<vector<DMatch>>knn_matches;
vector<DMatch>matches;
const float minRatio = 1.f / 1.5f;
matcher.knnMatch(dstImage1,knn_matches,2);
for (int i = 0; i < knn_matches.size(); i++)
{
const DMatch& bestmatch = knn_matches[i][0];
const DMatch& bettermatch = knn_matches[i][1];
float distanceRatio = bestmatch.distance / bettermatch.distance;
if (distanceRatio < minRatio)
matches.push_back(bestmatch);
}
Mat paterrn;
drawMatches(srcImage1,keypoints1,srcImage2,keypoints2,matches,paterrn);
imshow("无变换匹配",paterrn);
Mat homography;
bool homographyFound = refineMatchesWithHomography(keypoints1,keypoints2,3,matches,homography);
Mat warpedImage;
if (homographyFound)
{
warpPerspective(srcImage1, warpedImage, homography,Size(srcImage1.rows,srcImage1.cols),cv::WARP_INVERSE_MAP|cv::INTER_CUBIC);
imshow("投射变换后的图片", warpedImage);
}
vector<KeyPoint>warpedkeypoints;
Mat warpeddst;
detector->detectAndCompute(warpedImage,Mat(),warpedkeypoints,warpeddst);
vector<vector<DMatch>>knn_matches1;
vector<DMatch>matches1;
matcher.knnMatch(warpeddst, knn_matches1, 2);
for (int i = 0; i < knn_matches1.size(); i++)
{
const DMatch& bestmatch1 = knn_matches1[i][0];
const DMatch& bettermatch1 = knn_matches1[i][1];
float distanceRatio1 = bestmatch1.distance / bettermatch1.distance;
if (distanceRatio1 < minRatio)
matches1.push_back(bestmatch1);
}
Mat result;
drawMatches(warpedImage,warpedkeypoints,srcImage2,keypoints2,matches1,result);
imshow("结果",result);
waitKey(0);
return 0;
}