计算机视觉
图像处理

【OpenCV】特征检测器 FeatureDetector

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

  1. class CV_EXPORTS FeatureDetector
  2. {
  3. public:
  4.     virtual ~FeatureDetector();
  5.     void detect( const Mat& image, vector<KeyPoint>& keypoints,
  6.         const Mat& mask=Mat() ) const;
  7.     void detect( const vector<Mat>& images,
  8.         vector<vector<KeyPoint> >& keypoints,
  9.         const vector<Mat>& masks=vector<Mat>() ) const;
  10.     virtual void read(const FileNode&);
  11.     virtual void write(FileStorage&) const;
  12.     static Ptr<FeatureDetector> create( const string& detectorType );
  13. protected:
  14.     …
  15. };

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

  1. 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,同时在代码中加入:
  1. initModule_nonfree();
至于其他几种算法,我就不太了解了 ^_^
一个简单的使用演示:
  1. int main()
  2. {
  3.     initModule_nonfree();//if use SIFT or SURF
  4.     Ptr<FeatureDetector> detector = FeatureDetector::create( “SIFT” );
  5.     Ptr<DescriptorExtractor> descriptor_extractor = DescriptorExtractor::create( “SIFT” );
  6.     Ptr<DescriptorMatcher> descriptor_matcher = DescriptorMatcher::create( “BruteForce” );
  7.     if( detector.empty() || descriptor_extractor.empty() )
  8.         throw runtime_error(“fail to create detector!”);
  9.     Mat img1 = imread(“images\box_in_scene.png”);
  10.     Mat img2 = imread(“images\box.png”);
  11.     //detect keypoints;
  12.     vector<KeyPoint> keypoints1,keypoints2;
  13.     detector->detect( img1, keypoints1 );
  14.     detector->detect( img2, keypoints2 );
  15.     cout <<“img1:”<< keypoints1.size() << ” points  img2:” <<keypoints2.size()
  16.         << ” points” << endl << “>” << endl;
  17.     //compute descriptors for keypoints;
  18.     cout << “< Computing descriptors for keypoints from images…” << endl;
  19.     Mat descriptors1,descriptors2;
  20.     descriptor_extractor->compute( img1, keypoints1, descriptors1 );
  21.     descriptor_extractor->compute( img2, keypoints2, descriptors2 );
  22.     cout<<endl<<“Descriptors Size: “<<descriptors2.size()<<” >”<<endl;
  23.     cout<<endl<<“Descriptor’s Column: “<<descriptors2.cols<<endl
  24.         <<“Descriptor’s Row: “<<descriptors2.rows<<endl;
  25.     cout << “>” << endl;
  26.     //Draw And Match img1,img2 keypoints
  27.     Mat img_keypoints1,img_keypoints2;
  28.     drawKeypoints(img1,keypoints1,img_keypoints1,Scalar::all(-1),0);
  29.     drawKeypoints(img2,keypoints2,img_keypoints2,Scalar::all(-1),0);
  30.     imshow(“Box_in_scene keyPoints”,img_keypoints1);
  31.     imshow(“Box keyPoints”,img_keypoints2);
  32.     descriptor_extractor->compute( img1, keypoints1, descriptors1 );
  33.     vector<DMatch> matches;
  34.     descriptor_matcher->match( descriptors1, descriptors2, matches );
  35.     Mat img_matches;
  36.     drawMatches(img1,keypoints1,img2,keypoints2,matches,img_matches,Scalar::all(-1),CV_RGB(255,255,255),Mat(),4);
  37.     imshow(“Mathc”,img_matches);
  38.     waitKey(10000);
  39.     return 0;
  40. }

特征检测结果如图:

Box_in_scene
Box
特征点匹配结果:
Match
另一点需要一提的是SimpleBlob的实现是有Bug的。不能直接通过 Ptr<FeatureDetector> detector = FeatureDetector::create(“SimpleBlob”);  语句来调用,而应该直接创建 SimpleBlobDetector的对象:
  1.        Mat image = imread(“images\features.jpg”);
  2. Mat descriptors;
  3. vector<KeyPoint> keypoints;
  4. SimpleBlobDetector::Params params;
  5. //params.minThreshold = 10;
  6. //params.maxThreshold = 100;
  7. //params.thresholdStep = 10;
  8. //params.minArea = 10; 
  9. //params.minConvexity = 0.3;
  10. //params.minInertiaRatio = 0.01;
  11. //params.maxArea = 8000;
  12. //params.maxConvexity = 10;
  13. //params.filterByColor = false;
  14. //params.filterByCircularity = false;
  15. SimpleBlobDetector blobDetector( params );
  16. blobDetector.create(“SimpleBlob”);
  17. blobDetector.detect( image, keypoints );
  18. drawKeypoints(image, keypoints, image, Scalar(255,0,0));

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

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

转载注明来源:CV视觉网 » 【OpenCV】特征检测器 FeatureDetector

分享到:更多 ()
扫描二维码,给作者 打赏
pay_weixinpay_weixin

请选择你看完该文章的感受:

0不错 0超赞 0无聊 0扯淡 0不解 0路过

评论 3

评论前必须登录!