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

来源:互联网 发布:淘宝草编坐垫 编辑:程序博客网 时间:2024/05/21 17:44
接下来,sift该怎么用?(肯定不是检测出来就结束了)
  所以,接下来是图像特征匹配,输入两幅图像,输出匹配结果,代码如下(需加上头文件#include<opencv2/legacy/legacy.hpp>才可使用BruteForceMatcher):
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);

结果如下:
我们来分析下代码:
1.先构造SiftDescriptorExtractor,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 );
2.BruteForceMatcher (使用蛮力法)
Brute-force descriptor matcher. For each descriptor in the first set, this matcher finds the closest descriptor in the
second set by trying each one. This descriptor matcher supports masking permissible matches of descriptor sets.
3.matchFinds 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 Mat& img1, const vector<KeyPoint>& keypoints1, const Mat& img2, const vector<KeyPoint>& keypoints2, const vector<DMatch>& matches1to2, Mat& outImg, const Scalar& matchColor=Scalar::all(-1), const Scalar& singlePointColor=Scalar::all(-1), const vector<char>& matchesMask=vector<char>(), int flags=DrawMatchesFlags::DEFAULT )
C++: void drawMatches(const Mat& img1, const vector<KeyPoint>& keypoints1, const Mat& img2, const vector<KeyPoint>& keypoints2, const vector<vector<DMatch>>& matches1to2, Mat& outImg, const Scalar& matchColor=Scalar::all(-1), const Scalar& singlePointColor=Scalar::all(-1), const vector<vector<char>>& matchesMask=vector<vector<char> >(), int flags=DrawMatchesFlags::DEFAULT )
Paramet ers:
  • img1 – First source image.
  • keypoints1 – Keypoints from the first source image.
  • img2 – Second source image.
  • keypoints2 – Keypoints from the second source image.
  • matches – Matches from the first image to the second one, which means that keypoints1[i] has a corresponding point inkeypoints2[matches[i]] .
  • outImg – Output image. Its content depends on the flags value defining what is drawn in the output image. See possibleflags 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. IfsinglePointColor==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.
    来源: <http://www.opencv.org.cn/opencvdoc/2.3.2/html/modules/features2d/doc/drawing_function_of_keypoints_and_matches.html>
     
5. DMatch
class DMatch
Class for matching keypoint descriptors: query descriptor index, train descriptor index, train image index, and distance
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 0
原创粉丝点击