opencv3.4.5实现GMS+ORB特征匹配

环境:opencv3.4.5+opencv_contrib,vs2015;

GMS链接:

GMS: Grid-based Motion Statistics for Fast, Ultra-robust Feature Correspondence CVPR2017

 c++ code: https://github.com/JiawangBian/GMS-Feature-Matcher

代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 


using namespace cv;
using namespace std;
using namespace cv::xfeatures2d;


int main() 
{
	Mat img_1 = imread("01.jpg");
	Mat img_2 = imread("02.jpg");
	//Ptr sift = xfeatures2d::SIFT::create();
	//Ptr surf = xfeatures2d::SURF::create();
	Ptr orb = ORB::create(5000);
	orb->setFastThreshold(0);

	vector keypoints_1, keypoints_2;
	Mat descriptors_1,descriptors_2;

	//sift->detect(img_1, keypoints_1);
	//sift->compute(img_1, keypoints_1, descriptors);

	orb->detectAndCompute(img_1, Mat(),keypoints_1, descriptors_1);
	orb->detectAndCompute(img_2, Mat(), keypoints_2, descriptors_2);

	//orb ->detect(img_1, keypoints_1);
	//orb ->compute(img_1, keypoints_1, descriptors_1);

	//orb->detect(img_2, keypoints_2);
	//orb->compute(img_2, keypoints_2, descriptors_2);

	Mat ShowKeypoints1, ShowKeypoints2;
	drawKeypoints(img_1, keypoints_1, ShowKeypoints1);
	drawKeypoints(img_2, keypoints_2, ShowKeypoints2);
	imshow("Result_1", ShowKeypoints1);
	imshow("Result_2", ShowKeypoints2);

    vector matchesAll, matchesGMS;
	BFMatcher matcher(NORM_HAMMING);
	
	matcher.match(descriptors_1, descriptors_2, matchesAll);
	cout << "matchesAll: " << matchesAll.size() << endl;
	matchGMS(img_1.size(), img_2.size(), keypoints_1, keypoints_2, matchesAll, matchesGMS);
	std::cout << "matchesGMS: " << matchesGMS.size() << std::endl;

	Mat finalMatches;
	drawMatches(img_1, keypoints_1, img_2, keypoints_2, matchesGMS, finalMatches, Scalar::all(-1), Scalar::all(-1),
		std::vector(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
	imshow("Matches GMS", finalMatches);
	imwrite("MatchesGMS.jpg", finalMatches);
	waitKey(0);

	return 0;
}

匹配结果:

opencv3.4.5实现GMS+ORB特征匹配_第1张图片

你可能感兴趣的:(opencv,GMS,特征匹配)