Opencv source code analysis (3): feature point check preface 1

original address: opencv source code analysis (3): feature point check preface 1

because recently prepared to see the source code check aspect of the source code, and The most famous algorithms are sift and surf. So this time I mainly learned how to use the sift and surf functions in opencv to detect feature points and describe feature points, and how to use their algorithms for feature point matching. Fortunately, although the sift algorithm is a patent, it has been approved by the author under the efforts of opencv, and it has been added to the new version of opencv.

使用环境:opencv2.3.1+vs2010

Function: Find the feature points in the two images and describe them, and match them in 2 frames. The two pictures have the same content, but have been exposed, rotated, and scaled.

First look at the use of the sift algorithm function.

工程码:

//sift_test.cpp : Defines the entry point for the console application. #include"stdafx.h"#include <stdio.h>#include <iostream>#include "opencv2/core/core.hpp"//Because the opencv and other directories have been configured in the properties, it is treated as a local directory. #include"opencv2/features2d/features2d.hpp"#include "opencv2/highgui/highgui.hpp"using namespace cv;using namespace std;void readme();int main(int argc,char* argv[]){    Mat img_1=imread("./image/query.png",CV_LOAD_IMAGE_GRAYSCALE);//C macro definition CV_LOAD_IMAGE_GRAYSCALE=0, that is, read grayscale ImageMat img_2=imread("./image/rs_query.png",CV_LOAD_IMAGE_GRAYSCALE);//Be sure to remember the slash direction of the path here, which is the opposite of Matlab     if(!img_1.data || !img_2.data)// if the data is empty { cout<<"opencv error"<<endl;return -1;    }    cout<<"open right"<<endl;// One step, use SIFT operator to detect key pointsSiftFeatureDetector detector; // constructor uses internal default std::vector<KeyPoint> keypoints_1,keypoints_2;// construct 2 special points The constructed point vector is used to store the feature pointsdetector.detect(img_1,keypoints_1); // stores the feature points detected in the img_1 image in keypoints_1detector.detect(img_2,keypoints_2 );//同理//Draw feature points in imagesMat img_keypoints_1, img_keypoints_2; drawKeypoints(img_1,keypoints_1,img_keypoints_1,Scalar::all(-1),DrawMatchesFlags::DEFAULT);//Draw in memory Feature PointsdrawKeypoints(img_2,keypoints_2,img_keypoints_2,Scalar::all(-1),DrawMatchesFlags::DEFAULT);    imshow("sift_keypoints_1",img_keypoints_1);//display feature pointsimshow("sift_keypoints_2",img_keypoints_2);//calculate feature vectorsSif tDescriptorExtractor extractor;//definition description sub-objectMat descriptors_1,descriptors_2;//store the matrix of feature vectorsextractor.compute(img_1,keypoints_1,descriptors_1);//calculate feature vector| ||extractor.compute(img_2,keypoints_2,descriptors_2);    extractor.compute(img_2,keypoints_2,descriptors_2);//matching feature vectors with burte forceBruteForceMatcher<L2<float>>matcher;//defining a burte force matcher objectvector<DMatch>matches; Matcher.match(descriptors_1,descriptors_2,matches);//Draw matching line segmentsMat img_matches; drawMatches(img_1,keypoints_1,img_2,keypoints_2,matches,img_matches);// put the matching results into Memory img_matches // shows matching line segmentsimshow("sift_Matches",img_matches);//displayed title is MatcheswaitKey(0);return 0;}

Run results as follows:

below to see the use of surf algorithm function (and The sift is basically the same, the function name is changed.):

工程码:

//surf_test.cpp : Define the entry point of the console application. //#include "stdafx.h"#include <stdio.h>#include <iostream>#include "opencv2/core/core.hpp"//Because the opencv and other directories have been configured in the properties, it is treated as a local directory. #include"opencv2/features2d/features2d.hpp"#include "opencv2/highgui/highgui.hpp"using namespace cv;using namespace std;void readme();int main(int argc,char* argv[]){    Mat img_1=imread("./image/query.png",CV_LOAD_IMAGE_GRAYSCALE);// Macro definition CV_LOAD_IMAGE_GRAYSCALE=0, that is, read grayscale imageMat Img_2=imread("./image/rs_query.png",CV_LOAD_IMAGE_GRAYSCALE);//Be sure to remember the slash direction of the path here, which is the opposite of Matlab     if(!img_1.data || !img_2.data)// if the data is empty { cout<<"opencv error"<<endl;return -1;    }    cout<<"open right"<<endl;//第一, using SURF Operator detection key point     int minHessian=400;    SurfFeatureDetector detector(minHessian);    std::vector<KeyPoint> keypoints_1,keypoints_2;// Construct 2 point vectors specially composed of points to store feature pointsdetector.detect(img_1,keypoints_1);// Store the feature points detected in the img_1 image Put it in keypoints_1detector.detect(img_2,keypoints_2);//同理//Draw feature points in imagesMat img_keypoints_1,img_keypoints_2; drawKeypoints(img_1,keypoints_1,img_keypoints_1, Scalar::all(-1),DrawMatchesFlags::DEFAULT);    drawKeypoints(img_2,keypoints_2,img_keypoints_2,Scalar::all(-1),DrawMatchesFlags::DEFAULT);    imshow("surf_keypoints_1",img_keypoints_1);    imshow("surf_keypoints_2",img_keypoints_2);//calculate feature vectorSurfDescriptorExtractor extractor;//definition description sub-objectMat descriptors_1,descriptors_2;//store the matrix of feature vectorsextractor.compute (img_1,keypoints_1,descriptors_1); extractor.compute(img_2,keypoints_2,descriptors_2);//with burte force Matching feature vectorBruteForceMatcher<L2<float>>matcher;//defining a burte force matcher objectvector<DMatch>matches; matcher.match(descriptors_1,descriptors_2,matches);//Draw matching line segments Mat img_matches; drawMatches(img_1,keypoints_1,img_2,keypoints_2,matches,img_matches);// put the matching results into memory img_matches //display matching line segmentsimshow("surf_Matches",img_matches);//display The title is MatcheswaitKey(0);return 0;}

and its running result is as follows:

From this experiment, you can know how easy it is to use these two algorithms in opencv! Only a few simple parameters are needed to achieve good results. But this is only a low-level application of opencv, we should be able to use opencv some internal functions to help implement their own algorithms and ideas. This is the main purpose of analyzing the opencv source code.

In addition, from the course of the experiment, it can be felt that the running time of the surf algorithm is much faster than that of sift, and the number of feature points is detected more, and other temporary differences are still not found. Welcome to exchange, thank you!

Reprint please indicate the address of this article: opencv source code analysis (3): feature point check preface 1