Either segmentation fault 11 or (-215) N >= K

asked 2015-07-08 03:10:08 -0600

Hello everyone, I am trying to create a Bag of visual Words program, but I am running into an issue. Every time I run the program, I either get a segmentation fault: 11 error, or if I change the dictSize variable I get a error: (-215) N >= K in function kmeans. Since I am a complete novice, could someone please help me?

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

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

using namespace std;
using namespace cv;

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

    //=================================== LEARN ===================================

    struct dirent *de = NULL;
    DIR *d = NULL;
    d = opendir(argv[1]);
    if(d == NULL)
    {
        perror("Couldn't open directory");
        return(2);
    }

    Mat input;
    vector<KeyPoint> keypoints;
    Mat descriptor;
    Mat featuresUnclustered;
    Ptr<DescriptorExtractor> detector = xfeatures2d::SIFT::create();


    while((de = readdir(d))){
          if ((strcmp(de->d_name,".") != 0) && (strcmp(de->d_name,"..") != 0)  && (strcmp(de->d_name,".DS_Store") != 0)) {

            char fullPath[] = "./";
            strcat(fullPath, argv[1]);
            strcat(fullPath, de->d_name);
            printf("Current File: %s\n",fullPath);

            input = imread(fullPath,CV_LOAD_IMAGE_GRAYSCALE);
            cout << "Img size => x: " << input.size().width << ", y: " << input.size().height << endl;
            // If the incoming frame is too big, resize it
            if (input.size().width > 3000) {
                double ratio = (3000.0)/(double)input.size().width;
                resize(input, input, cvSize(0, 0), ratio, ratio); 
                cout << "New size => x: " << input.size().width << ", y: " << input.size().height << endl;
            }

            detector->detect(input, keypoints);
            detector->compute(input, keypoints, descriptor);
            featuresUnclustered.push_back(descriptor);
        }
    }

    closedir(d);

    int dictSize = 200;
    TermCriteria tc(CV_TERMCRIT_ITER,100,0.001);
    int retries = 1;
    int flags = KMEANS_PP_CENTERS;
    BOWKMeansTrainer bowTrainer(dictSize,tc,retries,flags);
    Mat dictionary = bowTrainer.cluster(featuresUnclustered);
    FileStorage fs("dict.yml",FileStorage::WRITE);
    fs << "vocabulary" << dictionary;
    fs.release();

    return 0;
}
edit retag flag offensive close merge delete

Comments

1

N >= K means, that you need to have more features (N) than desired clusters (K).

you don't have enough features to make 200 clusters.

berak gravatar imageberak ( 2015-07-08 04:29:12 -0600 )edit

So how should I change my code? Sorry but I am completely lost

ruben1691 gravatar imageruben1691 ( 2015-07-08 04:37:48 -0600 )edit

either use more images, or reduce dictSize, i guess ?

try: cerr << featuresUnclustered.size() << endl; to see, how many you got

berak gravatar imageberak ( 2015-07-08 05:04:06 -0600 )edit

returns [128 x 1999], so I tried with both 128 and 1999 as a dictSize, still crashes

ruben1691 gravatar imageruben1691 ( 2015-07-08 07:18:28 -0600 )edit

I have the same problem. Have you fixed it ??

Giorgos_ts gravatar imageGiorgos_ts ( 2016-03-06 17:12:06 -0600 )edit