ruben1691
ruben1691

Reputation: 423

OpenCV 3.0 Segmentation Fault (Bag of visual words)

I am trying to set up a bag of visual words using openCV 3.0. I have looked a bit everywhere and all I seem to be able to find is code that is only compatible with versions in the 2.x domain. As of now this is what I have:

#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;
}

I have put this together by using a few tutorials and snippets, but I am running into an issue. When the program gets to

features->detect(img1, keypoints);

it exits with a segmentation fault 11, whatever that means. Could someone help me and point out what it is I am doing wrong?

Upvotes: 0

Views: 585

Answers (1)

berak
berak

Reputation: 39796

you have to create your FeatureDetector, DescriptorExtractor first. atm, you got null-pointer instances (that's your segfault).

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

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

note, that since you have to use SIFT or SURF, you will need the opencv_contrib repo installed for this

Upvotes: 2

Related Questions