(转载自http://blog.csdn.net/xiaowei_cqu) opencv特征检测器 FeatureDetector

【OpenCV】特征检测器 FeatureDetector
分类: 【OpenCV】

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

[cpp]  view plain copy
  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()函数调用:

[cpp]  view plain copy
  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,同时在代码中加入:
[cpp]  view plain copy
  1. initModule_nonfree();  
至于其他几种算法,我就不太了解了 ^_^

一个简单的使用演示:
[cpp]  view plain copy
  1. int main()  
  2. {  
  3.   
  4.     initModule_nonfree();//if use SIFT or SURF  
  5.     Ptr<FeatureDetector> detector = FeatureDetector::create( "SIFT" );  
  6.     Ptr<DescriptorExtractor> descriptor_extractor = DescriptorExtractor::create( "SIFT" );  
  7.     Ptr<DescriptorMatcher> descriptor_matcher = DescriptorMatcher::create( "BruteForce" );  
  8.     if( detector.empty() || descriptor_extractor.empty() )  
  9.         throw runtime_error("fail to create detector!");  
  10.   
  11.     Mat img1 = imread("images\\box_in_scene.png");  
  12.     Mat img2 = imread("images\\box.png");  
  13.   
  14.     //detect keypoints;  
  15.     vector<KeyPoint> keypoints1,keypoints2;  
  16.     detector->detect( img1, keypoints1 );  
  17.     detector->detect( img2, keypoints2 );  
  18.     cout <<"img1:"<< keypoints1.size() << " points  img2:" <<keypoints2.size()   
  19.         << " points" << endl << ">" << endl;  
  20.   
  21.     //compute descriptors for keypoints;  
  22.     cout << "< Computing descriptors for keypoints from images..." << endl;  
  23.     Mat descriptors1,descriptors2;  
  24.     descriptor_extractor->compute( img1, keypoints1, descriptors1 );  
  25.     descriptor_extractor->compute( img2, keypoints2, descriptors2 );  
  26.   
  27.     cout<<endl<<"Descriptors Size: "<<descriptors2.size()<<" >"<<endl;  
  28.     cout<<endl<<"Descriptor's Column: "<<descriptors2.cols<<endl  
  29.         <<"Descriptor's Row: "<<descriptors2.rows<<endl;  
  30.     cout << ">" << endl;  
  31.   
  32.     //Draw And Match img1,img2 keypoints  
  33.     Mat img_keypoints1,img_keypoints2;  
  34.     drawKeypoints(img1,keypoints1,img_keypoints1,Scalar::all(-1),0);  
  35.     drawKeypoints(img2,keypoints2,img_keypoints2,Scalar::all(-1),0);  
  36.     imshow("Box_in_scene keyPoints",img_keypoints1);  
  37.     imshow("Box keyPoints",img_keypoints2);  
  38.   
  39.     descriptor_extractor->compute( img1, keypoints1, descriptors1 );    
  40.     vector<DMatch> matches;  
  41.     descriptor_matcher->match( descriptors1, descriptors2, matches );  
  42.   
  43.     Mat img_matches;  
  44.     drawMatches(img1,keypoints1,img2,keypoints2,matches,img_matches,Scalar::all(-1),CV_RGB(255,255,255),Mat(),4);  
  45.   
  46.     imshow("Mathc",img_matches);  
  47.     waitKey(10000);  
  48.     return 0;  
  49. }  

特征检测结果如图:

Box_in_scene


Box

特征点匹配结果:

Match

另一点需要一提的是 SimpleBlob的实现是有Bug的。不能直接通过 Ptr<FeatureDetector> detector = FeatureDetector::create("SimpleBlob");  语句来调用,而应该直接创建  SimpleBlobDetector的对象:
[cpp]  view plain copy
  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


(转载请注明作者和出处:http://blog.csdn.net/xiaowei_cqu 未经允许请勿用于商业用途)



更多 1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值