【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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM