opencv3.XX OpenCV SURF feature extraction to achieve learning related changes

As we all know, since opencv3.0 version, endless variety of changes, there are still big differences compared with opencv2. You can find this book from learning "OpenCV3 programming entry" might better front to the back of relatively complex image processing, such as feature extraction, really big change. 3.0 feature extraction related functions integrated in the opencv_contrib, you need to configure your own, then even if you took 130 minutes to configure the book will find a good sample program still does not take. Because a lot of functions including header files have changed, the key author lazy dead chatter, did not reveal a clue version of the direct use of opencv2, prime real gas cry !!! No way, only hard on those of us with a version of the opencv3, the most evil smoke is also easy to find information online, so it took me 130 minutes of run time before successful vomit! Next gave you share it!

opencv_contrib configuration

The first and most important step is to opencv_contrib configuration, need to use cmake, I use VS2015 + OpenCV3.2.0, then configure the opencv_contrib also spent a lot of time, I have not introduced specific tutorials, online or with a lot of, of course, you can not be a successful chatter on your luck, after all, not everyone God's blessing poured hee hee. Given a configuration tutorial link below, openCV3.2.0 configure VS2013 (for personal use) + opencv_contrib installation , I suggest that you find a few tutorials compare, together success rate will be higher.

Changes in header files

I use a sample program 11.2.3 of the book: SURF feature extraction code, the code is not long, but really the place to change a lot of vomit. The first is the header file, many are integrated into the opencv_contrib, as originally put nonfree.hpp of SURF and legacy.hpp in BruteForce correlation function, are now placed xfeatures2d.hpp inside. So you only need to add the following header file, of course, the premise is you have to configure opencv_contrib.

#include<opencv2/xfeatures2d.hpp>
using namespace xfeatures2d;

This namespace is also very important, otherwise those correlation function you still impossible, so it's really very silent vomit, trouble is dead chatter!

Defined feature detection class object

The code book is

SurfFeatureDetector detector(minHessian);
detector.detect(srcImage1, keypoint1);

opencv3 version is changed as a pointer defined manner, it is necessary to call detect by an arrow () function, there are two methods A and direct use as defined by the original SURF SurfFeatureDetector defined class, is another method, which species appears to be universal feature extraction method ,, comprising also using this
method:

Ptr< SurfFeatureDetector >detector = SurfFeatureDetector::create(minHessian);
detector->detect(srcImage1, keypoint1);

Method Two:

Ptr< SURF> detector =SURF::create(minHessian);
detector->detect(srcImage1, keypoint1);

Calculating a descriptor (feature vector)

The code book is

SurfDescriptorExtractor extractor;
extractor.compute(srcImage1, keypoint1, descriptors1);

The following changes, can be obtained thereby, as long as the code class definition, can be defined in the following manner

//方法一
Ptr< SurfDescriptorExtractor> extractor = SurfDescriptorExtractor::create();
//方法二
//Ptr< SURF> extractor = SURF::create();
extractor->compute(srcImage1, keypoint1, descriptors1);

Use BruteForce match

The code book is

BruteForceMatcher< L2< float > > matcher;
matcher.match(descriptors1, descriptors2, matches);

Here a little different to the above, it still has two changes methods, one with the same method, the other is BFMatcher class defined header files directly, which is similar to the book, not the pointer class

//方法一
Ptr< DescriptorMatcher > matcher = DescriptorMatcher::create(“BruteForce”);
matcher->match(descriptors1, descriptors2, matches);
//方法二
//BFMatcher matcher(NORM_L2);
//matcher.match(descriptors1, descriptors2, matches);

The complete code

Other portions basically the same, still gives the complete code for all to use, I was so generous, I compare very noble, but the free hee hee!

#include<opencv2/opencv.hpp>
#include<opencv2/xfeatures2d.hpp>
#include<iostream>
using namespace cv;
using namespace xfeatures2d;
using namespace std;

int main()
{
	Mat srcImage1 = imread("linlin.jpg");
	Mat srcImage2 = imread("linlin2.jpg");
	//使用SURF算子检测关键点
	int minHessian = 700; //SURF算法中的Hessian阈值
	Ptr<SurfFeatureDetector>detector = SurfFeatureDetector::create(minHessian);// 定义一个特征检测类对象
	vector<KeyPoint> keypoint1, keypoint2;//vector模板类,存放任意类型的动态数组
	//调用detect函数检测出SURF特征关键点,保存在vector容器中
	detector->detect(srcImage1, keypoint1);
	detector->detect(srcImage2, keypoint2);
	//计算描述符(特征向量)
	//方法一
	Ptr<SurfDescriptorExtractor> extractor = SurfDescriptorExtractor::create();
	//方法二
	//Ptr<SURF> extractor = SURF::create();
	Mat descriptors1, descriptors2;
	extractor->compute(srcImage1, keypoint1, descriptors1);
	extractor->compute(srcImage2, keypoint2, descriptors2);
	//使用BruteForce进行匹配
	//实例化一个匹配器
	//方法一
	Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("BruteForce");
	//方法二
	//BFMatcher matcher(NORM_L2);
	vector<DMatch> matches;
	//匹配两幅图中的描述子
	matcher->match(descriptors1, descriptors2, matches);
	//绘制从两个图像中匹配出的关键点
	Mat imgMatches;
	drawMatches(srcImage1, keypoint1, srcImage2, keypoint2, matches, imgMatches); //进行绘制

	imshow("匹配图", imgMatches);
	waitKey(0);
	return 0;
}

Experimental results

Love is still the choice of prostitute universe days, but days later the situation is too rich bitch, so match results Well, me and Ms. Tsai, a prostitute shows Qingzheziqing boots boots! (Note that two pictures best tune into the same size Yo)
Here Insert Picture Description

Published 17 original articles · won praise 3 · Views 1809

Guess you like

Origin blog.csdn.net/weixin_43350361/article/details/88887304