"Master Opencv ..." Reading Notes "image feature points matching

Source: Internet
Author: User

This is the third chapter of this book, this paper mainly focuses on the feature point matching and the method of removing mismatch points.

Main function: Took two photos of the unified object, but the second picture has the choice and the scale change. Now we need to extract the feature points for each of the two images, and then match the feature points so that they correspond to each other as much as possible

In this paper, we use the Surf feature to match feature points using Brute-force Matcher and flann-based Matcher respectively:

The first snippet of code is from the OPENCV official website tutorial:

Test2.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <iostream> #include "opencv2/core/core.hpp" #include "opencv2/features2d/ Features2d.hpp "#include" opencv2/highgui/highgui.hpp "#include" opencv2/nonfree/features2d.hpp "#include" opencv2/
Calib3d/calib3d.hpp "#include" opencv2/imgproc/imgproc.hpp "using namespace CV;


using namespace Std;
	int _tmain (int argc, _tchar* argv[]) {Mat img_1 = Imread ("haha1.jpg", Cv_load_image_grayscale);

	Mat img_2 = Imread ("haha2.jpg", Cv_load_image_grayscale);

	if (!img_1.data | |!img_2.data) {return-1;} --Step 1:detect the keypoints using SURF Detector//threshold for Hessian keypoint Detector used in SURF int minhess

	Ian = 15000;

	Surffeaturedetector detector (Minhessian);

	Std::vector<keypoint> Keypoints_1, keypoints_2;
	Detector.detect (img_1, keypoints_1);

	Detector.detect (Img_2, keypoints_2);

	--Step 2:calculate descriptors (feature vectors) surfdescriptorextractor extractor; Mat DescriptorS_1, descriptors_2;
	Extractor.compute (img_1, Keypoints_1, descriptors_1);

	Extractor.compute (Img_2, keypoints_2, descriptors_2);
	--Step 3:matching descriptor vectors with a brute force Matcher bfmatcher matcher (norm_l2,false);
	vector< Dmatch > matches;
	
	Matcher.match (Descriptors_1, descriptors_2, matches);
	--Draw matches Mat img_matches;

	Drawmatches (img_1, Keypoints_1, Img_2, keypoints_2, matches, img_matches);

	--Show detected matches imshow ("matches", img_matches);

	Waitkey (0);
return 0;
 }


Brute-forcedescriptor Matcher. For each descriptor in the first set, this matcher findsthe closest descriptor in the second set by trying each one. This descriptormatcher supports masking permissible matches of descriptor sets.

The above is the introduction of the Bfmatcher, you understand. I above the code to set the threshold value of the surf is deliberately large, otherwise the picture is all line, can not see. The running result of the above code:

As shown, there are many matching errors. There are two definitions of matching errors in the book:

False-positivematches: The characteristic point is perfect, just correspondence relation error;

False-negativematches: The feature point disappears, resulting in a corresponding relationship error;

We only care about the first case, there are two solutions, one of which is to set the second parameter of the Bfmatcher constructor to true, as Cross-match filter.

Bfmatcher Matcher (norm_l2,true);

His thinking is: to match train descriptors with the query set and viceversa. Only common matches for these and matches is returned. Such techniquesusually produce best results with minimal number of outliers when there areenough matches

Effect Diagram:

You can see that the segment that matches the error is less than the first.

The second flann-based matcher:uses the fastapproximate nearest neighbor search algorithm to find correspondences (it usesfast thi Rd-party Library for approximate nearest neighbors, library for this).

Usage:

Flannbasedmatcher Matcher1;
Matcher1.match (Descriptors_1, descriptors_2, matches);

Effect Diagram:


The second method of removing the matching error point is described below, Knn-matching:we performknn-matching first with k=2. The nearest descriptors is returned for Eachmatch. The match is returned only if the distance ratio between the first andsecond matches are big enough (the ratio threshold is usually near).

Test2.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <iostream> #include "opencv2/core/core.hpp" #include "opencv2/features2d/ Features2d.hpp "#include" opencv2/highgui/highgui.hpp "#include" opencv2/nonfree/features2d.hpp "#include" opencv2/
Calib3d/calib3d.hpp "#include" opencv2/imgproc/imgproc.hpp "using namespace CV;


using namespace Std;
	int _tmain (int argc, _tchar* argv[]) {Mat img_1 = Imread ("test.jpg", Cv_load_image_grayscale);

	Mat img_2 = Imread ("test1.jpg", Cv_load_image_grayscale);

	if (!img_1.data | |!img_2.data) {return-1;} --Step 1:detect the keypoints using SURF Detector//threshold for Hessian keypoint Detector used in SURF int minhess

	Ian = 1500;

	Surffeaturedetector detector (Minhessian);

	Std::vector<keypoint> Keypoints_1, keypoints_2;
	Detector.detect (img_1, keypoints_1);

	Detector.detect (Img_2, keypoints_2);

	--Step 2:calculate descriptors (feature vectors) surfdescriptorextractor extractor; Mat Descriptors_1, descriptors_2;
	Extractor.compute (img_1, Keypoints_1, descriptors_1);

	Extractor.compute (Img_2, keypoints_2, descriptors_2);
	--Step 3:matching descriptor vectors with a brute force Matcher bfmatcher matcher (norm_l2,false);
	Flannbasedmatcher Matcher1;
	vector< Dmatch > matches;
	vector<vector< Dmatch >> Matches2;
	
	Matcher.match (Descriptors_1, descriptors_2, matches);

	Matcher1.match (Descriptors_1, descriptors_2, matches);
	const float Minratio = 1.f/1.5f;
	Matches.clear ();
	Matcher.knnmatch (Descriptors_1, descriptors_2,matches2,2);
		for (size_t i=0; i<matches2.size (); i++) {const CV::D match& bestmatch = matches2[i][0];
		CONST CV::D match& bettermatch = matches2[i][1];
		float distanceratio = bestmatch.distance/bettermatch.distance; Pass only matches where distance ratio between//nearest matches is greater than 1.5//(distinct criteria) if (
		Distanceratio < Minratio) {matches.push_back (bestmatch);
}
	}
	--Draw matches Mat img_matches;

	Drawmatches (img_1, Keypoints_1, Img_2, keypoints_2, matches, img_matches);

	--Show detected matches imshow ("matches", img_matches);

	Waitkey (0);
return 0;
 }

Here, I set the surf threshold value to 1500, the effect diagram:


Finally, the Foreigner book also mentions the use of the single-response matrix transformation to further refine the results:

        Refine
	const int minnumbermatchesallowed = 8;

	if (Matches.size () < minnumbermatchesallowed)
		return false;
	Prepare data for cv::findhomography
	STD::VECTOR<CV::P oint2f> srcpoints (Matches.size ());
	STD::VECTOR<CV::P oint2f> dstpoints (Matches.size ());

	for (size_t i = 0; i < matches.size (); i++)
	{
		//cout<<i<< "+matches[i].trainidx<<" + matches[i].queryidx<<endl;
		Srcpoints[i] = keypoints_1[matches[i].trainidx].pt;
		Dstpoints[i] = keypoints_2[matches[i].queryidx].pt;
		
	}

	Find homography Matrix and get inliers mask
	std::vector<unsigned char> inliersmask (Srcpoints.size ());
	Mat homography = findhomography (srcpoints, dstpoints, Cv_fm_ransac, 3.0f, inliersmask);

	STD::VECTOR<CV::D match> inliers;
	for (size_t i=0; i<inliersmask.size (); i++)
	{
		if (Inliersmask[i])
			inliers.push_back (Matches[i]);
	}

	Matches.swap (inliers);

This code directly takes the last piece of code. Effect Diagram:


This article is too boring, the main foreigner book this chapter itself is not interesting, with the book code every time a lot, the third chapter also use OpenGL, do not play.

After a period of time, focus on the implementation of the basic features of the image and the implementation of Android basic functions. OpenCV put it aside first.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.