0046-OpenCV下的SIFT特征检测

一个物体,不管远近以及角度如何,我们人都能判断为同一物体,因为我们人脑是根据物体的特征来判断是不是同一物体的,计算机在处理图像时,我们也希望具备这样的能力,所以需要对图像进行特征提取

本文给出利用OpenCV的SIFT类来进行图像的特征提取与匹配的代码

由于OpenCV3.0没有编译nonfree,而SIFT类包含在这个模块中,所以我们使用OpenCV2.4.9。
OpenCV2.4.9的Windows版安装包下载链接见https://blog.csdn.net/opencv_source/article/details/83785987


代码如下
代码请加Q2034196302获取
代码请加Q2034196302获取
代码请加Q2034196302获取

代友中的相关绘制函数说明
drawKeypoints函数,原型如下
C++: void drawKeypoints(const Mat& image, const vector<KeyPoint>& keypoints, Mat& outImage, const Scalar& color=Scalar::all(-1), int flags=DrawMatchesFlags::DEFAULT )
参数意义如下
image:源图像
keypoints:存储特征点的矢量
outImage:输出图像
color:特征点颜色,如果设置为Scalar::all(-1),则颜色随机。
flags:绘图设置的标志,可能的值如下:
struct DrawMatchesFlags
{
    enum
    {
       DEFAULT = 0, // Output image matrix will be created (Mat::create),
                     // i.e. existing memory of output image may be reused.
                     // Two source images, matches, and single keypoints
                     // will be drawn.
                     // For each keypoint, only the center point will be
                     // drawn (without a circle around the keypoint with the
                     // keypoint size and orientation).
        DRAW_OVER_OUTIMG = 1, // Output image matrix will not be
                       // created (using Mat::create). Matches will be drawn
                       // on existing content of output image.
        NOT_DRAW_SINGLE_POINTS = 2, // Single keypoints(没有匹配到的点) will not be drawn.
       DRAW_RICH_KEYPOINTS = 4 // For each keypoint, the circle around
                       // keypoint with keypoint size and orientation will
                       // be drawn.
    };
};

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 )
参数意义如下
img1:第一张图像
keypoints1:来自第一张图像的特征点
img2:第二张图像
keypoints2:来自第二张图像的特征点
matches1to2:第一张图像向第二张图像的匹配特征点。
outImg:输出图像,它的内容取决于flags的设置。
matchColor:匹配图的颜色设置(特征点和线的颜色),如果设置为Scalar::all(-1),则颜色随机。
singlePointColor:没有匹配到的点的颜色。如果设置为Scalar::all(-1),则颜色随机。
matchesMask:匹配掩码,可以通过设置它来设置哪些特征点被绘制。
flags:绘图设置的标志,可能的值如下:
struct DrawMatchesFlags
{
    enum
    {
        DEFAULT = 0, // Output image matrix will be created (Mat::create),
                     // i.e. existing memory of output image may be reused.
                     // Two source images, matches, and single keypoints
                     // will be drawn.
                     // For each keypoint, only the center point will be
                     // drawn (without a circle around the keypoint with the
                     // keypoint size and orientation).
        DRAW_OVER_OUTIMG = 1, // Output image matrix will not be
                       // created (using Mat::create). Matches will be drawn
                       // on existing content of output image.
       NOT_DRAW_SINGLE_POINTS = 2, // Single keypoints(没有匹配到的点) will not be drawn.
        DRAW_RICH_KEYPOINTS = 4 // For each keypoint, the circle around
                       // keypoint with keypoint size and orientation will
                       // be drawn.
    };
};
运行结果如下图所示

猜你喜欢

转载自blog.csdn.net/opencv_source/article/details/83786091