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

时间:2017-08-16 10:52:45

标签: c++ opencv feature-extraction feature-detection

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

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

有没有办法通过更改&#34; SURF&#34;来更改要素类型?部分通过程序参数?

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

3 个答案:

答案 0 :(得分:1)

您似乎希望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"

它返回一个指向FeatureDetector基类的指针,因此您可以使用多态来在运行时选择特定的实现:

//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);

答案 1 :(得分:0)

模板怎么样?

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"
    defaultConfig {
        applicationId "xx.xx.xx.seguros"
        minSdkVersion 9
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:26.+'
    compile 'com.android.support:design:26.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
}

然后你可以用:

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

替代方案可能是MACRO。

只有所有这些特征检测器共享一个公共接口才能工作。

答案 2 :(得分:0)

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的回答。