Scale Invariant Feature Transform matching algorithm SIFT (2)

Source: Internet
Author: User

Scale Invariant Feature Transform matching algorithm SIFT (2)
Scale Invariant Feature Transform matching algorithm SIFT (2)
E-mail: [email protected]
SIFT AlgorithmIn the early October S, Caocao learned SIFT (which can be checked here) and called the functions of the opencv function library for practice, however, I did not have a deep understanding of the principle of the SIFT sub-description and the usage and parameter descriptions of related functions in opencv. As a note of LZ, this blog will record the description of related functions in opencv. It will take time to continue understanding the principles of SIFT features.
C ++ codeEnvironment: vs2010 + opencv2.3.1 + win7 × 64 this part of code still uses the previous SIFT code. This article focuses on understanding some functions and data structures.

#include 
 
    #include 
  
       using namespace std;  using namespace cv;  int main()  {      //read the two input images      Mat image1 = imread(image1.jpg);      Mat image2 = imread(image2.jpg);      //if failed      if(image1.empty()||image2.empty())      {          cout<
   
     keypoint1,keypoint2;      //detect image with SIFT,get key points      siftDetector.detect(image1,keypoint1);      Mat outImage1;      //draw key points at the out image and show to the user      drawKeypoints(image1,keypoint1,outImage1,Scalar(255,0,0));        imshow(original_image1,image1);      imshow(sift_image1,outImage1);            Mat outImage2;            siftDetector.detect(image2,keypoint2);      drawKeypoints(image2,keypoint2,outImage2,Scalar(255,0,0));        imshow(sift_image2.jpg,outImage2);      //imwrite(sift_result2.jpg,outImage2);      //store 10 keypoints in order to watch the effect clearly      vector
    
      keypoint3,keypoint4;      for(int i=0;i<10;i++)      {          keypoint3.push_back(keypoint1[i]);          keypoint4.push_back(keypoint2[i]);      }      // difine a sift descriptor extractor      SiftDescriptorExtractor extractor;      //store the descriptor of each image      Mat descriptor1,descriptor2;      BruteForceMatcher
     
      > matcher;        vector
      
        matches; Mat img_matches; //compute the descriptor of each image extractor.compute(image1,keypoint3,descriptor1); extractor.compute(image2,keypoint4,descriptor2); //match matcher.match(descriptor1,descriptor2,matches); //show the result drawMatches(image1,keypoint3,image2,keypoint4,matches,img_matches,Scalar(255,0,0)); imshow(matches,img_matches); //store the match_image //imwrite(matches.jpg,img_matches); waitKey(0); return 0; } 
      
     
    
   
  
 

Opencv functions and data structures
1. drawMatcher ():Draws the found matches of keypoints from two images.
Reference: http://docs.opencv.org/2.4/modules/features2d/doc/drawing_function_of_keypoints_and_matches.html C ++: Void DrawMatches (Const Mat & Img1, Const vector & Keypoints1, Const Mat & Img2, Const vector & Keypoints2, Const vector > & Matches1to2, Mat & OutImg, Const Scalar & MatchColor= Scalar: all (-1), const Scalar & SinglePointColor= Scalar: all (-1), const vector > & MatchesMask= Vector > (), Int Flags= DrawMatchesFlags: DEFAULT )
 
  • Img1-First source image.
  • Keypoints1-Keypoints from the first source image.
  • Img2-Second source image.
  • Keypoints2-Keypoints from the second source image.
  • Matches1to2-Matches from the first image to the second one, which means that keypoints1 [I] has a corresponding point in keypoints2 [matches [I].
  • OutImg-Output image. Its content depends on the flags value defining what is drawn in the output image. See possible flags bit values below.
  • MatchColor-Color of matches (lines and connected keypoints). If matchColor = Scalar: all (-1), the color is generated randomly.
  • SinglePointColor-Color of single keypoints (circles), which means that keypoints do not have the matches. If singlePointColor = Scalar: all (-1), the color is generated randomly.
  • MatchesMask-Mask determining which matches are drawn. If the mask is empty, all matches are drawn.
  • Flags-Flags setting drawing features. Possible flags bit values are defined by DrawMatchesFlags.
2. DMatch: Class for matching keypoint descriptors: query descriptor index, train descriptor index, train image index, and distance between descriptors. See: http://docs.opencv.org/master/d4/de0/classcv_1_1DMatch.html
Struct DMatch {// three constructors DMatch (): queryIdx (-1), trainIdx (-1), imgIdx (-1), distance (std: numeric_limits
        
         
: Max () {} DMatch (int _ queryIdx, int _ trainIdx, float _ distance): queryIdx (_ queryIdx), trainIdx (_ trainIdx ), imgIdx (-1), distance (_ distance) {} DMatch (int _ queryIdx, int _ trainIdx, int _ imgIdx, float _ distance): queryIdx (_ queryIdx ), trainIdx (_ trainIdx), imgIdx (_ imgIdx), distance (_ distance) {} intqueryIdx; // The feature description sub-index of the matched query image inttrainIdx; // This match corresponds to the feature description sub-index intimgIdx of the training (Template) image; // the index of the Training image (if there are multiple) f Loat distance; // The Euclidean distance between two feature vectors. The smaller the distance, the higher the matching degree. Booloperator <(const DMatch & m) const ;};
        

Generally, the Brute-force descriptor matcher is used for matching, and the result is not readable (Here we look at the figure). Therefore, please note that the matching results are saved in the vector In the defined dynamic array matches, this means that we can perform a series of operations on the matching results, such as adding matches before the drawMatches () function. erase (matches. begin () + 25, matches. end (); you can select the latest 25 matching results.


 

Related Article

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.