Bad argument on classification problem [closed]
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);
bowDE.setVocabulary(dictionary);
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
trainingData.push_back(bowDescriptor);
labels.push_back((float)1); //push labels in a matrix.. 1
}
//SVM Part
Ptr<SVM> svm = SVM::create();
svm->setType(SVM::C_SVC);
svm->setKernel(SVM::LINEAR);
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
svm->train(td);
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!
you need integer labels for classification (with an SVM)
Mmm i don't get it, I've already used
int label[1] = {1};
which in actually an intlabel[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)
Alright so basically I deleted the line
int label[1] = {1}
, and just definedMat 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