【OpenCV】特征检测器 FeatureDetector

《SIFT原理与源码分析》系列文章索引:http://www.cnblogs.com/tianyalu/p/5467813.html

OpenCV提供FeatureDetector实现特征检测及匹配

class CV_EXPORTS FeatureDetector
{
public:
    virtual ~FeatureDetector();
    void detect( const Mat& image, vector<KeyPoint>& keypoints,
        const Mat& mask=Mat() ) const;
    void detect( const vector<Mat>& images,
        vector<vector<KeyPoint> >& keypoints,
        const vector<Mat>& masks=vector<Mat>() ) const;
    virtual void read(const FileNode&);
    virtual void write(FileStorage&) const;
    static Ptr<FeatureDetector> create( const string& detectorType );
protected:
    ...
};

FeatureDetetor是虚类,通过定义FeatureDetector的对象可以使用多种特征检测方法。通过create()函数调用:

Ptr<FeatureDetector> FeatureDetector::create(const string& detectorType);

OpenCV 2.4.3提供了10种特征检测方法:

  • "FAST" – FastFeatureDetector
  • "STAR" – StarFeatureDetector
  • "SIFT" – SIFT (nonfree module)
  • "SURF" – SURF (nonfree module)
  • "ORB" – ORB
  • "MSER" – MSER
  • "GFTT" – GoodFeaturesToTrackDetector
  • "HARRIS" – GoodFeaturesToTrackDetector with Harris detector enabled
  • "Dense" – DenseFeatureDetector
  • "SimpleBlob" – SimpleBlobDetector

图片中的特征大体可分为三种:点特征、线特征、块特征。

FAST算法是Rosten提出的一种快速提取的点特征[1],Harris与GFTT也是点特征,更具体来说是角点特征(参考这里)。

SimpleBlob是简单块特征,可以通过设置SimpleBlobDetector的参数决定提取图像块的主要性质,提供5种:

颜色 By color、面积 By area、圆形度 By circularity、最大inertia (不知道怎么翻译)与最小inertia的比例 By ratio of the minimum inertia to maximum inertia、以及凸性 By convexity.

最常用的当属SIFT,尺度不变特征匹配算法(参考这里);以及后来发展起来的SURF,都可以看做较为复杂的块特征。这两个算法在OpenCV nonfree的模块里面,需要在附件引用项中添加opencv_nonfree243.lib,同时在代码中加入:

initModule_nonfree();

至于其他几种算法,我就不太了解了 ^_^

一个简单的使用演示:

int main()
{

    initModule_nonfree();//if use SIFT or SURF
    Ptr<FeatureDetector> detector = FeatureDetector::create( "SIFT" );
    Ptr<DescriptorExtractor> descriptor_extractor = DescriptorExtractor::create( "SIFT" );
    Ptr<DescriptorMatcher> descriptor_matcher = DescriptorMatcher::create( "BruteForce" );
    if( detector.empty() || descriptor_extractor.empty() )
        throw runtime_error("fail to create detector!");

    Mat img1 = imread("images\\box_in_scene.png");
    Mat img2 = imread("images\\box.png");

    //detect keypoints;
    vector<KeyPoint> keypoints1,keypoints2;
    detector->detect( img1, keypoints1 );
    detector->detect( img2, keypoints2 );
    cout <<"img1:"<< keypoints1.size() << " points  img2:" <<keypoints2.size()
        << " points" << endl << ">" << endl;

    //compute descriptors for keypoints;
    cout << "< Computing descriptors for keypoints from images..." << endl;
    Mat descriptors1,descriptors2;
    descriptor_extractor->compute( img1, keypoints1, descriptors1 );
    descriptor_extractor->compute( img2, keypoints2, descriptors2 );

    cout<<endl<<"Descriptors Size: "<<descriptors2.size()<<" >"<<endl;
    cout<<endl<<"Descriptor‘s Column: "<<descriptors2.cols<<endl
        <<"Descriptor‘s Row: "<<descriptors2.rows<<endl;
    cout << ">" << endl;

    //Draw And Match img1,img2 keypoints
    Mat img_keypoints1,img_keypoints2;
    drawKeypoints(img1,keypoints1,img_keypoints1,Scalar::all(-1),0);
    drawKeypoints(img2,keypoints2,img_keypoints2,Scalar::all(-1),0);
    imshow("Box_in_scene keyPoints",img_keypoints1);
    imshow("Box keyPoints",img_keypoints2);

    descriptor_extractor->compute( img1, keypoints1, descriptors1 );
    vector<DMatch> matches;
    descriptor_matcher->match( descriptors1, descriptors2, matches );

    Mat img_matches;
    drawMatches(img1,keypoints1,img2,keypoints2,matches,img_matches,Scalar::all(-1),CV_RGB(255,255,255),Mat(),4);

    imshow("Mathc",img_matches);
    waitKey(10000);
    return 0;
}

特征检测结果如图:

Box_in_scene

Box

特征点匹配结果:

Match

另一点需要一提的是SimpleBlob的实现是有Bug的。不能直接通过 Ptr<FeatureDetector> detector = FeatureDetector::create("SimpleBlob");  语句来调用,而应该直接创建 SimpleBlobDetector的对象:

        Mat image = imread("images\\features.jpg");
    Mat descriptors;
    vector<KeyPoint> keypoints;
    SimpleBlobDetector::Params params;
    //params.minThreshold = 10;
    //params.maxThreshold = 100;
    //params.thresholdStep = 10;
    //params.minArea = 10;
    //params.minConvexity = 0.3;
    //params.minInertiaRatio = 0.01;
    //params.maxArea = 8000;
    //params.maxConvexity = 10;
    //params.filterByColor = false;
    //params.filterByCircularity = false;
    SimpleBlobDetector blobDetector( params );
    blobDetector.create("SimpleBlob");
    blobDetector.detect( image, keypoints );
    drawKeypoints(image, keypoints, image, Scalar(255,0,0));

以下是SimpleBlobDetector按颜色检测的图像特征:

[1] Rosten. Machine Learning for High-speed Corner Detection, 2006

本文转自:http://blog.csdn.net/xiaowei_cqu/article/details/8652096

时间: 2024-11-09 09:47:18

【OpenCV】特征检测器 FeatureDetector的相关文章

OpenCV2.4.4中调用SIFT特征检测器进行图像匹配

OpenCV中一些相关结构说明: 特征点类: class KeyPoint { Point2f pt; //坐标 float size; //特征点邻域直径 float angle; //特征点的方向,值为[0,360),负值表示不使用 float response; // int octave; //特征点所在的图像金字塔的组 int class_id; //用于聚类的id } 存放匹配结果的结构: 1 struct DMatch 2 { 3 //三个构造函数 4 DMatch(): quer

OpenCV特征点检测------ORB特征

OpenCV特征点检测------ORB特征 ORB是是ORiented Brief的简称.ORB的描述在下面文章中: Ethan Rublee and Vincent Rabaud and Kurt Konolige and Gary Bradski, ORB: an efcient alternative to SIFT or SURF, ICCV 2011 没有加上链接是因为作者确实还没有放出论文,不过OpenCV2.3RC中已经有了实现,WillowGarage有一个talk也提到了这个

OpenCV之特征检测器(Feature Detector),描述子提取器(Descriptor Extractor)和描述子匹配器(Descriptor Matcher)

1.特征检测子 -Harris cv::cornerHarris(image,strength,3,3,0.01); -Fast cv::Ptr<cv::FastFeatureDetector> fast = cv::FastFeatureDetector::create(); //或 cv::FAST(InputArray image, std::vector<KeyPoint> &keypoints, int threshold) //或 cv::FAST(InputA

OpenCV特征点检测

特征点检测 目标 在本教程中,我们将涉及: 使用 FeatureDetector 接口来发现感兴趣点.特别地: 使用 SurfFeatureDetector 以及它的函数 detect 来实现检测过程 使用函数 drawKeypoints 来绘制检测到的关键点 理论 代码 这个教程的代码如下所示.你还可以从 这个链接下载到源代码 #include <stdio.h> #include <iostream> #include "opencv2/core/core.hpp&q

OpenCV特征点检测匹配图像-----添加包围盒

最终效果: 其实这个小功能非常有用,甚至加上只有给人感觉好像人脸检测,目标检测直接成了demo了,主要代码如下: // localize the object std::vector<Point2f> obj; std::vector<Point2f> scene; for (size_t i = 0; i < good_matches.size(); ++i) { // get the keypoints from the good matches obj.push_bac

OpenCV特征点检測------Surf(特征点篇)

Surf(Speed Up Robust Feature) Surf算法的原理                                                                           1.构建Hessian矩阵构造高斯金字塔尺度空间 事实上surf构造的金字塔图像与sift有非常大不同,就是由于这些不同才加快了其检測的速度. Sift採用的是DOG图像.而surf採用的是Hessian矩阵行列式近似值图像.Hessian矩阵是Surf算法的核心,为了方

OpenCV特征点检测------Surf(特征点篇)

Surf(Speed Up Robust Feature) Surf算法的原理                                                                           1.构建Hessian矩阵构造高斯金字塔尺度空间 其实surf构造的金字塔图像与sift有很大不同,就是因为这些不同才加快了其检测的速度.Sift采用的是DOG图像,而surf采用的是Hessian矩阵行列式近似值图像.Hessian矩阵是Surf算法的核心,为了方便运算

OpenCV SIFT原理与源码分析

http://blog.csdn.net/xiaowei_cqu/article/details/8069548 SIFT简介 Scale Invariant Feature Transform,尺度不变特征变换匹配算法,是由David G. Lowe在1999年(<Object Recognition from Local Scale-Invariant Features>)提出的高效区域检测算法,在2004年(<Distinctive Image Features from Scal

【OpenCV】SIFT原理与源码分析

SIFT简介 Scale Invariant Feature Transform,尺度不变特征变换匹配算法,是由David G. Lowe在1999年(<Object Recognition from Local Scale-Invariant Features>)提出的高效区域检测算法,在2004年(<Distinctive Image Features from Scale-Invariant Keypoints>)得以完善. SIFT特征对旋转.尺度缩放.亮度变化等保持不变性