OpenCV 3.0分段错误(一袋视觉词)

时间:2015-07-07 09:46:38

标签: c++ opencv

我正在尝试使用openCV 3.0设置一袋视觉词。我看起来到处都有,我似乎能找到的代码只与2.x域中的版本兼容。截至目前,这就是我所拥有的:

#include <opencv2/core/core.hpp>
#include "opencv2/highgui/highgui.hpp"
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/features2d/features2d.hpp>

#include <iostream>
#include <stdio.h>


using namespace std;
using namespace cv;

int main(int argc, const char** argv) {

    Ptr<FeatureDetector> features;
    Ptr<DescriptorExtractor> descriptors;
    Ptr<DescriptorMatcher> matcher;

    int MAX_ITER = 100;
    int EPS = 2;

    TermCriteria tc(MAX_ITER + EPS,1,0.001);

    int dictSize = 1000;
    int retries = 1;
    int flags = KMEANS_PP_CENTERS;
    BOWKMeansTrainer bowTrainer(dictSize,tc,retries,flags);

    BOWImgDescriptorExtractor bowDE(descriptors,matcher);



    Mat img1 = imread("/Users/Lucas/Desktop/pic2.jpg");
    Mat img2 = imread("/Users/Lucas/Desktop/2.jpg");

    vector<KeyPoint> keypoints,keypoints2;
    features->detect(img1, keypoints);
    features->detect(img2, keypoints2);


    Mat myFeatures;
    Mat myFeatures2;

    descriptors->compute(img1, keypoints, myFeatures);
    descriptors->compute(img2, keypoints2, myFeatures2);
    bowTrainer.add(myFeatures);
    bowTrainer.add(myFeatures2);

    Mat dictionary = bowTrainer.cluster();
    bowDE.setVocabulary(dictionary);

    cout << dictionary << endl;


    return 0;
}

我已经通过使用一些教程和片段将它们放在一起,但我遇到了一个问题。程序到达时

features->detect(img1, keypoints);

它以分段错误11退出,无论这意味着什么。有人可以帮助我并指出我做错了什么吗?

1 个答案:

答案 0 :(得分:2)

您必须首先创建您的FeatureDetector,DescriptorExtractor。 atm,你有空指针实例(这是你的段错误)。

   #include <opencv2/xfeatures2d.hpp>
   ...

   Ptr<FeatureDetector> features = xfeatures2d::SIFT::create();
   Ptr<DescriptorExtractor> descriptors = xfeatures2d::SIFT::create();
   Ptr<DescriptorMatcher> matcher = makePtr<BFMatcher>(NORM_L2);

请注意,由于您必须使用SIFT或SURF,因此需要为此安装opencv_contrib repo