[英]OpenCV - How to implement FeatureDetector interface
我正在x86_64體系結構上使用適用於Ubuntu 12.10的OpenCV 2.4.6.1的C ++實現。 我正在將Agast Corner Detector的這段代碼包裝在從cv::FeatureDetector
繼承的類中。
檢查feature2d模塊頭代碼並觀察其他實現,我發現我應該強制實現detectImpl
方法:
virtual void detectImpl( const Mat& image, std::vector<KeyPoint>& keypoints, const Mat& mask=Mat() ) const = 0;
通常,還可以實現一個具有以下簽名的名為operator
的方法:
CV_WRAP_AS(detect) void operator()(const Mat& image, CV_OUT std::vector<KeyPoint>& keypoints) const;
看看其他實現,我不能確切地說出每種方法應該做什么。 我猜想第二個operator
在某種程度上與用於檢測關鍵點的detect方法有關:
cv::Ptr<cv::FeatureDetector> detector = cv::FeatureDetector::create("...");
detector->detect(img, keypoints);
根據您的經驗,這兩種方法有什么區別,每種方法應實現什么?
與使用工廠方法cv::FeatureDetector::create
的檢測器實例化有關,我有一些線索與通常作為檢測器類實現的公共屬性放入的AlgorithmInfo*
類型的屬性info
有關,並在features2d_init
源文件中使用了CV_INIT_ALGORITHM 。
為了能夠使用工廠方法實例化自定義FeatureDetector,我應該實現什么?
最后,經過幾天的工作,我成功實現了自己的承諾,並學習了一些有關實現cv::FeatureDetector
接口的課程:
將包裝類包括在cv名稱空間中。
唯一必須實施的方法是使用您使用的OpenCV版本上的方法簽名的detectImpl
。
實現operator方法是可選的,在使用該方法的其他實現中(例如MserFeatureDetector
和StarDetector
),該方法通過類實例從detectImpl
調用:
void ...::detectImpl( const Mat& image, std::vector<KeyPoint>& keypoints, const Mat& mask ) const { ... (*this)(grayImage, keypoints); ... } void ...::operator()(const Mat& img, std::vector<KeyPoint>& keypoints) const{ ... }
請注意, detectImpl
是const方法,因此它不能修改實例參數,因此像其他檢測器實現(例如FastFeatureDetector
或StarDetector
)中所做的那樣,在邊函數上定義檢測器的具體行為可能會很有用。
要使用工廠方法cv::FeatureDetector::create
實例化包裝器,應在類聲明中添加公共方法AlgorithmInfo* info() const;
然后使用CV_INIT_ALGORITHM
將類初始化為OpenCV中的CV_INIT_ALGORITHM
,如下所示:
namespace cv{ CV_INIT_ALGORITHM(AgastFeatureDetector, "Feature2D.AGAST", obj.info()->addParam(obj, "threshold", obj.threshold); obj.info()->addParam(obj, "nonmaxsuppression", obj.nonmaxsuppression); obj.info()->addParam(obj, "type", obj.type)); }
如果您的類不需要任何參數,則只需將所有參數部分替換為obj.info()
還要提醒您在聲明(.h)或定義(.cpp)包裝器的源文件之外執行此操作,並包括opencv2/core/internal.hpp
庫。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.