Opencv 3.0 SVM train classification issues

可紊 提交于 2019-12-12 05:25:17

问题


Im new in openCV SVM. Im running in Xcode 7.0, openCV 3.0, Below is my code

MatMat labels(0,1,CV_32FC1);
//Mat labels(0,1,CV_32S);  //I also try this when i saw some posting, But error too.
...
Mat samples_32f; samples.convertTo(samples_32f, CV_32F);
//Mat samples_32f; samples.convertTo(samples_32f, CV_32FC1); //tried!

Ptr<SVM> classifier = SVM::create();
classifier->SVM::train(samples_32f, 0, labels);  <- Here the Error

The 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.

When I search around some solutions, the error message seem was came from labels that define not integer value. So i had try to changed to Mat labels(0,1,CV_32S), but the issues error still the same.

So i have no idea what going wrong with the code..is anyone can help?


回答1:


The errors is because labels does not content any value as defined as 0 rows with 1 cols. Therefore, it is correct to make sure labels has holding numbers of rows records for SVM training.

My Solutions:

Mat labels(0,1,CV_32S); 

/*
for loop to use pushback added value into lables
{...}
*/
/*
or
define another Mat labeled(labels.rows, 1, CV_32S); after the for loop 
and use it in the SVM.train
*/

Mat samples_32f; samples.convertTo(samples_32f, CV_32F);

Ptr<SVM> classifier = SVM::create();
classifier->SVM::train(samples_32f, 0, labels);  <-change the labels names if you define new labels after the for loop.

Thanks, that's what i can share.



来源:https://stackoverflow.com/questions/33294281/opencv-3-0-svm-train-classification-issues

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!