Opencv--surf feature detection and matching

Source: Internet
Author: User

Surf principle: https://wenku.baidu.com/view/2f1e4d8ef705cc1754270945.html

How the SURF algorithm works

    1. Select Poi in image (Points of interest) Hessian Matrix

    2. Detection of key points in different scale spaces, non-maximum signal suppression

    3. Discover feature point methods, rotation invariance requirements

    4. Generating feature vectors

Introduction to surf constructor function

C + +: Surf::surf (

Double Hessianthreshold---threshold detector uses the key point of Hessian, the default value in

Between 300-500

int noctaves=4,--4 means in four scale space

int noctavelayers=2,--Indicates the number of layers per scale

BOOL Extended=false,

BOOL Upright=false--Indicates that the calculation rotation invariance, not calculated faster

)

Feature Point drawing

The feature points are plotted in order to draw out the detected surf feature points in the original image, this step is to show us the feature points intuitively, and it doesn't matter with the feature extraction and matching process of the whole surf operator.

To draw using the Drawkeypoints method:

void drawkeypoints (const mat& image,

Const vector<keypoint>& Keypoints,

Cv_out mat& Outimage,
Const scalar& Color=scalar::all (-1),

int flags=drawmatchesflags::D efault

);

The first parameter image: The original image, you can make a three-channel or single-channel image;

The second parameter keypoints: The characteristic point vector, each element inside the vector is a KeyPoint object, which contains the various attribute information of the feature point;

The third parameter outimage: the canvas image that the feature point draws, can be the original image;

The fourth argument color: The color information drawn by the feature points, the default is to draw a random color;

The fifth parameter flags: the drawing mode of the feature point, in fact, is to set the feature points of the information needs to be drawn, those do not need to draw, there are several modes to choose:

DEFAULT: Only the coordinate points of the feature points are plotted, which is a small dot on the image, and the center coordinates of each small dot are the coordinates of the feature points.
Draw_over_outimg: The function does not create the output image, but directly in the output image variable space, it is required to output the image variable is an initialization, the size and type are already initialized good variables
Not_draw_single_points: Feature points of a single point are not plotted
Draw_rich_keypoints: Drawing feature points when drawing a circle with a direction, this method simultaneously displays the image coordinates, size, and direction, is the most can display the characteristic information one kind of drawing way.

1#include <opencv2/opencv.hpp>2#include <opencv2/xfeatures2d.hpp>3#include <iostream>4 5 using namespaceCV;6 using namespacecv::xfeatures2d;7 using namespacestd;8 9 intMainintargcChar**argv) {TenMat src = imread ("test.jpg", Imread_grayscale); One     if(Src.empty ()) { Aprintf"could not load image...\n"); -         return-1; -     } theNamedwindow ("Input Image", cv_window_autosize); -Imshow ("Input Image", SRC); -  -     //surf feature point detection +     intMinhessian = -; -ptr<surf> detector = surf::create (Minhessian);//creates a surf class object and initializes +Vector<keypoint>keypoints; ADetector->detect (SRC, keypoints, Mat ());//find the key. at  -     //Draw key points - Mat keypoint_img; -Drawkeypoints (SRC, keypoints, keypoint_img, Scalar::all (-1), Drawmatchesflags::D efault); -Imshow ("keypoints Image", keypoint_img); -  inWaitkey (0); -     return 0; to}

Draw a match point

Drawmatches (Constmat& IMG1,Constvector<keypoint>&Keypoints1,Constmat& Img2,Constvector<keypoint>&Keypoints2,Constvector<dmatch>& Matches1to2, mat&outimg,Constscalar& Matchcolor=scalar::all (-1),Constscalar& Singlepointcolor=scalar::all (-1),                             Constvector<Char>& matchesmask=vector<Char> (),intFlags=drawmatchesflags::D efault);

The parameters are as follows:

* img1– Source image 1

* keypoints1– The feature point of the source image 1.

* img2– source image 2.

* keypoints2– feature points of source image 2

* The feature point of the matches1to2– source image 1 matches the feature point of the source image 2 [matches[i]].

* The outimg– output image is determined by the flags.

* matchcolor– matching colors (feature points and lines), if Matchcolor==scalar::all (-1), Color random.

* singlepointcolor– the color of a single point, that is, an unpaired feature point, if Matchcolor==scalar::all (-1), the color is random.

*matchesmask–mask determines which points will be drawn and, if empty, draws all the matching points.

*flags-it is the same as the meaning of flags in the Drawkeypoints method.

When a match is made using only the selected optimal match points, it means that there will be many non-optimal feature points that will not be matched, which can be set flags=drawmatchesflags::not_draw_single_points

1#include <opencv2/opencv.hpp>2#include <opencv2/xfeatures2d.hpp>3#include <iostream>4 5 using namespaceCV;6 using namespacecv::xfeatures2d;7 using namespacestd;8 9 intMainintargcChar**argv) {TenMat src = imread ("number. jpg", Imread_grayscale); OneMat temp = Imread ("2.png", Imread_grayscale); A     if(Src.empty () | |Temp.empty ()) { -printf"could not load image...\n"); -         return-1; the     } -Namedwindow ("Input Image", cv_window_autosize); -Imshow ("Input Image", SRC); -  +     //surf feature point detection -     intMinhessian = -; +ptr<surf> detector = surf::create (Minhessian,4,3,true,true);//creates a surf class detector object and initializes AVector<keypoint>keypoints1, keypoints2; atDetector->detect (SRC, keypoints1, Mat ());//find the key. -Detector->detect (temp, keypoints2, Mat ());//find the key. -  -Bfmatcher Matcher;//instantiate a brute force matching device -Mat Src_vector, Temp_vector;//a description vector for storing feature points -Vector<dmatch> matches;//Dmatch is a class used to describe a matching pair of feature points, containing information about the two points in                                //For example, there is a feature m on the left, which matches the feature point N of the right image, and the Dmatch records the two most matches, and also records the M and N -                                //The distance of the eigenvector and other information, the distance is used to filter the to  +Detector->detectandcompute (SRC, Mat (), Keypoints1, src_vector);//input image, input mask, input feature point, output mat, store description vector of all feature points -Detector->detectandcompute (temp, Mat (), keypoints2, temp_vector);//This mat is the number of feature points, the number of columns is the size of each eigenvector, Surf is 64 (dimension) the  *Matcher.match (Src_vector, temp_vector, matches);//match, data source is eigenvector, result stored in Dmatch type $ Panax Notoginseng                                               //sort function to sort data in ascending order -Sort (Matches.begin (), Matches.end ());//Filter matching points, according to match inside the distance from the characteristics of the order from small to large thevector< Dmatch >good_matches; +     intPtspairs = Std::min ( -, (int) (Matches.size () *0.15)); Acout << Ptspairs <<Endl; the      for(inti =0; i < Ptspairs; i++) +     { -Good_matches.push_back (Matches[i]);//50 of the minimum distance to press into the new Dmatch $     } $Mat outimg;//drawmatches This function to draw a graph together -Drawmatches (SRC, keypoints1, temp, keypoints2, good_matches, Outimg, Scalar::all (-1), Scalar::all (-1), vector<Char> (), drawmatchesflags::not_draw_single_points);//draw a match point -Imshow ("keypoints Image", outimg);  the  -Waitkey (0);Wuyi     return 0; the}

Opencv--surf feature detection and matching

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.