Advertisement

OpenCV SIFT特征学习:(二)SIFT匹配

阅读量:

接下来,sift该怎么用?(肯定不是检测出来就结束了)

在此之后 是进行图像特征配对的过程 接受两副图像作为输入 生成相应的匹配结果 输出 至于具体的实现细节 可以参考相关技术文档

复制代码
       
       
    
    
    
    	cv::Mat image1=cv::imread("D:\ sift1.bmp");
       
       
    
    
       
       
    
    
    
    	cv::Mat image2=cv::imread("D:\ sift2.bmp");
       
       
    
    
       
       
    
    
    
    	cv::Mat out;
       
       
    
    
       
       
    
    
    
    	cv::SiftFeatureDetector  siftdtc; 
       
       
    
    
       
       
    
    
    
        std::vector<cv::KeyPoint> kp1,kp2;
       
       
    
    
       
       
    
    
    
        siftdtc.detect(image1,kp1);
       
       
    
    
       
       
    
    
    
    	siftdtc.detect(image2,kp2);
       
       
    
    
       
       
    
    
    
        drawKeypoints(image1,kp1,out,Scalar::all(-1),4);
       
       
    
    
       
       
    
    
    
    	cv::imshow("",out);
       
       
    
    
       
       
    
    
    
     
       
       
    
    
       
       
    
    
    
    	//---------------------------------------------------------
       
       
    
    
       
       
    
    
    
    	SiftDescriptorExtractor extractor;
       
       
    
    
       
       
    
    
    
    	Mat descriptor1,descriptor2;
       
       
    
    
       
       
    
    
    
    	BruteForceMatcher<L2<float>> matcher;
       
       
    
    
       
       
    
    
    
    	vector<DMatch> matches;
       
       
    
    
       
       
    
    
    
    	Mat img_matches;
       
       
    
    
       
       
    
    
    
    	extractor.compute(image1,kp1,descriptor1);
       
       
    
    
       
       
    
    
    
    	extractor.compute(image2,kp2,descriptor2);
       
       
    
    
       
       
    
    
    
    	matcher.match(descriptor1,descriptor2,matches);
       
       
    
    
       
       
    
    
    
     
       
       
    
    
       
       
    
    
    
    	drawMatches(image1,kp1,image2,kp2,matches,img_matches);
       
       
    
    
       
       
    
    
    
    	imshow("matches",img_matches);

结果如下:

我们来分析下代码:

为了构建SiftDescriptorExtractor这一模块,在其构造过程中同样包含两个主要组件:

复制代码
    
    
    
    
     
     SiftDescriptorExtractor(
    
    
    
    
    
    
    
    
     
     const SIFT::DescriptorParams& descriptorParams=SIFT::DescriptorParams(),
    
    
    
    
    
    
    
    
     
     const SIFT::CommonParams& commonParams=SIFT::CommonParams() );
    
    
    
    
    
    
    
    
     
      
    
    
    
    
    
    
    
    
     
     SiftDescriptorExtractor( double magnification, bool isNormalize=true,
    
    
    
    
    
    
    
    
     
     bool recalculateAngles=true, int nOctaves=SIFT::CommonParams::DEFAULT_NOCTAVES,
    
    
    
    
    
    
    
    
     
     int nOctaveLayers=SIFT::CommonParams::DEFAULT_NOCTAVE_LAYERS,
    
    
    
    
    
    
    
    
     
     int firstOctave=SIFT::CommonParams::DEFAULT_FIRST_OCTAVE,
    
    
    
    
    
    
    
    
     
     int angleMode=SIFT::CommonParams::FIRST_ANGLE );
  1. BruteForceMatcher (使用蛮力法)

This brute-force descriptor matcher identifies, for each descriptor in the first set, its closest counterpart within the

The second set is achieved by sequentially attempting each individual one. The described matcher is designed to mask permissible matches of descriptor sets.

The second set is achieved by sequentially attempting each individual one. The described matcher is designed to mask permissible matches of descriptor sets.

3.match : Finds the best match for each descriptor from a query set.

复制代码
    
    
    
    
     
     C++: void DescriptorMatcher::match(const Mat& queryDescriptors, const Mat& trainDescriptors, vector<
    
    
    
    
    
    
    
    
     
     DMatch>& matches, const Mat& mask=Mat() ) const
    
    
    
    
    
    
    
    
     
     C++: void DescriptorMatcher::match(const Mat& queryDescriptors, vector<DMatch>& matches, const
    
    
    
    
    
    
    
    
     
     vector<Mat>& masks=vector<Mat>() )
    
    
    
    
    
    
    
    
     
     Parameters
    
    
    
    
    
    
    
    
     
     queryDescriptors – Query set of descriptors.
    
    
    
    
    
    
    
    
     
     trainDescriptors – Train set of descriptors. This set is not added to the train descriptors
    
    
    
    
    
    
    
    
     
     collection stored in the class object.
    
    
    
    
    
    
    
    
     
     matches – Matches. If a query descriptor is masked out in mask , no match is added for this
    
    
    
    
    
    
    
    
     
     descriptor. So, matches size may be smaller than the query descriptors count.
    
    
    
    
    
    
    
    
     
     mask – Mask specifying permissible matches between an input query and train matrices of
    
    
    
    
    
    
    
    
     
     descriptors.

4.drawMatches

C++: void drawMatches (const Matrix& img1 , const vector& keypoints1 , const Matrix& img2 , const vector& keypoints2 , const vector& matches_1to2 , Mat& outImg , const Scalar& matchColor =Scalar::all(-1), const Scalar& singlePointColor =Scalar::all(-1), const vector& matchesMask =vector(), int flags =DrawMatchesFlags::DEFAULT ) [](http://www.opencv.org.cn/opencvdoc/2.3.2/html/modules/features2d/doc/drawing_function_of_keypoints_and_matches.html#void drawMatches�(C%20const%20Matrix%20&img1,%20C%20const%20vector%20&keypoints1,C%20const%20Matrix%20&img2,C%20const%20vector%20&keypoints2,C%20const%20vector%7b_7bmatches_7b7b1to7b7b,c7cMat&outImg,C% 95 Scalar&matchColor%C9F(C9FScalar::all(-76)),C99Scalar&singlePointColor%C9F(C9FScalar::all(-76)),C85vector&matchesMask=C85vector(),C83int*flags=DrawMatchesFlags::DEFAULT){}

该函数用于从输入图像中绘制关键点之间的匹配关系【

|Paramet ers:|

  • img1 – 第一个来源图像。
  • keypoints1 – 第一幅图像的关键点。
  • img2 – 第二个来源图像。
  • keypoints2 – 第二幅图像的关键点。
  • matches – 匹配结果表示从第一幅图像到第二幅图像的匹配关系。其中keypoints1中的第i个关键点与keypoints2中的第matches[i]个关键点相对应。
  • outImg – 输出图像。其内容取决于flags参数所定义的内容显示类型,请参阅DrawMatchesFlags类文档以获取详细信息。
  • matchColor – 匹配线及连接关键点的颜色设置。当match_color设为-1时会自动随机生成颜色。
  • single_point_color – 单个关键点(圆圈)的颜色设置。当single_point_color设为-1时会自动随机生成颜色。
  • matches_mask – 确定哪些匹配会被绘制显示的掩模图层。若mask为空,则所有匹配都会被绘制。
  • flags - 设置绘图特征使用的位掩码参数集合,请参阅DrawMatchesFlags类文档以获取详细信息。

5. DMatch

class DMatch

This category is used for matching keypoint descriptors: query descriptor indices, train descriptor indices, train image indices, and the distance measure.

between descriptors.

structure:

int queryIdx; // query descriptor index

int trainIdx; // train descriptor index

int imgIdx; // train image index

float distance; // 两个特征向量之间的欧氏距离,越小表明匹配度越高。

通过暴力枚举法计算得到匹配数目等于kp1的数量,在图形界面中绘制出的直线难以解读。建议仅展示距离当前最接近的25个匹配结果:如下代码实现

复制代码
       
       
    
    
    
    	std::nth_element(matches.begin(),matches.begin()+24,matches.end());
       
       
    
    
       
       
    
    
    
    	matches.erase(matches.begin()+25,matches.end());
       
       
    
    
       
       
    
    
    
    
    
       
       
    
    
       
       
    
    
    
    
    
       
       
    
    
       
       
    
    
    
    
    
       
       
    
    
       
       
    
    
    
    但是,现在的匹配线仍是比较杂乱的,如何挑选出最佳的匹配呢?请听下回讲解^^

全部评论 (0)

还没有任何评论哟~