Ask Your Question
0

OpenCV 3 and SVM on cpp

asked 2017-03-31 04:22:43 -0600

alexMm1 gravatar image

Hi guys,

I already searched for this type of error but I did not find anything similar...

My question is related to opencv3 and SVM. Basically, I have this:

int labels[2] = {1, -1};
Mat labelsMat(2, 1, CV_32S, labels);
float trainingData[2][2] = { { 501, 10 }, { 255, 10 } };
Mat training_samples(2, 2, CV_64FC1, trainingData);

training_samples.convertTo(training_samples, CV_32S); 
labelsMat.convertTo(labelsMat, CV_32F);
Ptr<ml::SVM> svm = ml::SVM::create();
svm->setType(ml::SVM::C_SVC);
svm->setKernel(ml::SVM::LINEAR);
svm->setC(1); 
svm->train(training_samples, ml::SampleTypes::ROW_SAMPLE, labelsMat);

When I compile, I get this error:

OpenCV Error: Bad argument (in the case of classification problem the responses must be categorical; either specify varType when creating TrainData, or pass integer responses) in train, file /home/develop/_Morpheos/opencv/opencv/modules/ml/src/svm.cpp, line 1618 terminate called after throwing an instance of 'cv::Exception' what(): /home/develop/_Morpheos/opencv/opencv/modules/ml/src/svm.cpp:1618: error: (-5) in the case of classification problem the responses must be categorical; either specify varType when creating TrainData, or pass integer responses in function train

I tried almost everything but I do not catch the error.

Could you help me?

thanks

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-03-31 04:35:00 -0600

berak gravatar image

if you are trying a classification , your labels have to be integer.

please remove the

labelsMat.convertTo(labelsMat, CV_32F);

line.

also, you need float data , not integer, so it should be:

float trainingData[2][2] = { { 501, 10 }, { 255, 10 } };
Mat training_samples(2, 2, CV_32FC1, trainingData);

(and no further conversion)

edit flag offensive delete link more

Comments

thanks....finally, it works....

alexMm1 gravatar imagealexMm1 ( 2017-03-31 04:43:27 -0600 )edit

Another question: In openCV there are not normalization rules for data before training and testing?

alexMm1 gravatar imagealexMm1 ( 2017-03-31 04:59:46 -0600 )edit

the ANN has options for normailzation, but not the SVM.

(and yes, it's usually a good idea to do so !)

berak gravatar imageberak ( 2017-03-31 06:26:40 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-03-31 04:22:43 -0600

Seen: 2,591 times

Last updated: Mar 31 '17