Bad argument on classification problem [closed]

asked 2020-06-25 05:32:03 -0500

albeh95 gravatar image

updated 2020-06-25 07:41:57 -0500

Hello guys, I'm trying to compile this code having ONLY 1 LABEL fro object classification (I know it may seems like it doesn't make sense, but one label is just what I need). While compiling this code:

    Ptr<SIFT> detector = SIFT::create(); //detector to detect SIFT features 

Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create(DescriptorMatcher::FLANNBASED);

BOWImgDescriptorExtractor bowDE(detector, matcher);

cout << "extracting histograms in the form of BOW for each Training image " << endl;

Mat labels(0, 1, CV_32S);
int dictSize = 1500;

Mat trainingData(0, dictSize, CV_32S);
int k = 0;
vector<KeyPoint> keypoint;
Mat bowDescriptor;
Mat img;

vector<String> fn;
vector<Mat> data;
glob("C:/Users/albma/Desktop/Università/Computer Vision/Final_Project/Nonmio/train/*.jpg", fn, true);

//extracting histogram in the form of bow for each image
    for (size_t i = 0; i < fn.size(); i++) { //for each image

        printf("Training Image = %s\n", fn[i]);
        img = imread(fn[i], 0);
        detector->detect(img, keypoint); //detect keypoints
        bowDE.compute(img, keypoint, bowDescriptor); //compute descriptors
        labels.push_back((float)1); //push labels in a matrix.. 1

//SVM Part

Ptr<SVM> svm = SVM::create();
svm->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 100, 1e-6));

printf("Training SVM\n");
Ptr<TrainData> td = TrainData::create(trainingData, ROW_SAMPLE, labels); //start training SVM

I get this error: Bad argument (in the case of classification problem the responses must be categorical; either specify varType when creating TrainData, or pass integer responses). What should I fix? Thanks everyone!

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by albeh95
close date 2020-06-25 09:19:23.749958


you need integer labels for classification (with an SVM)

berak gravatar imageberak ( 2020-06-25 06:00:21 -0500 )edit

Mmm i don't get it, I've already used int label[1] = {1}; which in actually an int

albeh95 gravatar imagealbeh95 ( 2020-06-25 06:08:32 -0500 )edit

label[1] = {1}; -- that looks weirdly wrong, too, don't try to init it with external data ;)

you need a CV_32S labels empty Mat, and push_back((int)something)

berak gravatar imageberak ( 2020-06-25 07:11:42 -0500 )edit

Alright so basically I deleted the line int label[1] = {1}, and just defined Mat labels(0, 1, CV_32S); but still I get the same error :( (I apply the push_back() inside the for loop). EDIT: actually I removed the (float) cast in the push_back and now it works! Thank you so much

albeh95 gravatar imagealbeh95 ( 2020-06-25 07:43:09 -0500 )edit