如何通过opencv中的程序参数定义要素类型?

How to define feature type through program argument in opencv?

目前更改功能类型是通过在代码中手动更改名称来完成的。例如,对于 SURF 检测器和描述符,我必须在以下代码示例中将单词 "SURF" 更改为其他名称:

Ptr<SURF> detector = SURF::create();
Ptr<SURF> descriptor = SURF::create();

是否可以通过程序参数更改 "SURF" 部分来更改要素类型?

我希望能够批量评估多个特征类型,而不是每次都手动输入特征类型。

模板呢?

template<class T> cv::Ptr<T> FeatureDetectorCreator()
{
    return T::create();
};

然后你可以调用它:

cv::Ptr<SURF> detector = FeatureDetectorCreator<SURF>();

备选方案可以是 MACRO。

仅当所有这些特征检测器共享一个公共接口时才会起作用。

您似乎想要 Ptr<FeatureDetector> cv::FeatureDetector::create(const string& detectorType) 函数提供的功能:

"FAST" – FastFeatureDetector 
"STAR" – StarFeatureDetector 
"SIFT" – SIFT (nonfree module) 
"SURF" – SURF (nonfree module) 
"ORB" – ORB
"BRISK" – BRISK 
"MSER" – MSER 
"GFTT" – GoodFeaturesToTrackDetector
"HARRIS" – GoodFeaturesToTrackDetector with Harris detector enabled
"Dense" – DenseFeatureDetector 
"SimpleBlob" – SimpleBlobDetector

Also a combined format is supported: 
  feature detector adapter name ( "Grid" – GridAdaptedFeatureDetector, "Pyramid" – PyramidAdaptedFeatureDetector )
   + feature detector name (see above), for example: "GridFAST", "PyramidSTAR"

它 returns 指向 FeatureDetector 基 class 的指针,因此您可以在运行时使用多态性来 select 具体实现:

//assuming image path is the first command line parameter and detector type is the second parameter
auto image = cv::imread(argv[1]);
auto ptr = cv::FeatureDetector::create(argv[2]); //auto evaulates to cv::Ptr<cv::FeatureDetector> here
std::vector<cv::KeyPoint> keypoints;
ptr->detect(image, keypoints);
for (auto kp : keypoints)
    cv::circle(image, kp.pt, 2, cv::Scalar(0, 255, 255), -1);
cv::imwrite(argv[1], image);

OpenCV 3.x

没有内置函数。使用 Ptr<FeatureDetector> 定义一个检测器(等效的描述符是 Ptr<DescriptorExtractor>)。示例代码如下:

string det = argv[4]; //assuming detector definition is fourth argument
Ptr<FeatureDetector> detector;
if (det == "SURF") {
    detector = SURF::create();
} else if (det == "SIFT") {
    detector = SIFT::create();
}

OpenCV 2.4.x

使用提供的函数Ptr<FeatureDetector> cv::FeatureDetector::create(const string& detectorType)。详情请参考slawekwin的回答。