生活随笔
收集整理的這篇文章主要介紹了
特征检测器 FeatureDetector
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
OpenCV提供FeatureDetector 實現特征檢測及匹配
[cpp] ?view plaincopy
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()函數調用:
[cpp] ?view plaincopy
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 plaincopy
initModule_nonfree();?? 至于其他幾種算法,我就不太了解了?^_^
一個簡單的使用演示:
[cpp] ?view plaincopy
int ?main()?? {?? ?? ????initModule_nonfree();?? ????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" );?? ?? ?????? ????vector<KeyPoint>?keypoints1,keypoints2;?? ????detector->detect(?img1,?keypoints1?);?? ????detector->detect(?img2,?keypoints2?);?? ????cout?<<"img1:" <<?keypoints1.size()?<<? "?points??img2:" ?<<keypoints2.size()??? ????????<<?"?points" ?<<?endl?<<? ">" ?<<?endl;?? ?? ?????? ????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;?? ?? ?????? ????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 的對象:
[cpp] ?view plaincopy
???????Mat?image?=?imread( "images\\features.jpg" );?? Mat?descriptors;?? vector<KeyPoint>?keypoints;?? SimpleBlobDetector::Params?params;?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? 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?未經允許請勿用于商業用途)
總結
以上是生活随笔 為你收集整理的特征检测器 FeatureDetector 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。