Ask Your Question
0

SVM Training Data Error

asked 2016-04-17 09:24:40 -0600

sara1691362 gravatar image

i'm using Opencv 3.0 and i tried this code but i'm getting this error :

using namespace std;
using namespace cv;
using namespace cv::ml;
int main(int argc, char** argv){
 int labels[10] = { 0, 0, 1, 1, 0, 1, 1, 1, 1, 0};
 cv::Mat lablesMat(10, 1, CV_32SC1, labels);

 float trainingData[10][2] = { { 100, 10 }, { 150, 10 }, { 600, 200 }, { 600, 10 }, { 10, 100 }, { 455, 10 }, { 345, 255 }, { 10, 501`z` }, { 401, 255 }, { 30, 150 } };

 cv::Mat trainDataMat(10, 2, CV_32FC1, trainingData);
SVM::Params params;
    params.svmType    = SVM::C_SVC;
    params.kernelType = SVM::LINEAR;
    params.termCrit   = TermCriteria(TermCriteria::MAX_ITER, 100, 1e-6);
    // Train the SVM
    Ptr<SVM> svm = StatModel::train<SVM>(trainDataMat, ROW_SAMPLE, lablesMat, params);
 //Create test features
 float testData[2] = { 150, 15 };

 cv::Mat testDataMat(2, 1, CV_32FC1, testData);

 //Predict the class labele for test data sample
 float predictLable = svm->predict(testDataMat);

 std::cout << "Predicted label :" << predictLable << "\n";
    return(0);
}

OpenCV Error: Assertion failed (samples.cols == var_count && samples.type() == CV_32F) in predict, terminate called after throwing an instance of 'cv::Exception' what(): error: (-215) samples.cols == var_count && samples.type() == CV_32F in function predict

edit retag flag offensive close merge delete

Comments

do you really have that z in your trainData ? shouldn't even compile ;9

berak gravatar imageberak ( 2016-04-17 09:56:48 -0600 )edit

... and maybe you want to update to current opencv 3.1

(afaik, your version does not allow to save / read svm data)

berak gravatar imageberak ( 2016-04-17 10:05:00 -0600 )edit

so what should i do to solve the probleme? is there any solution beside the update?

sara1691362 gravatar imagesara1691362 ( 2016-04-17 10:11:14 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2016-04-17 09:53:35 -0600

berak gravatar image

updated 2016-04-17 10:40:59 -0600

your train set had 10 rows and 2 feature-cols, and your test vec should have 1 row, and 2 cols.

cv::Mat testDataMat(1, 2, CV_32FC1, testData);
edit flag offensive delete link more

Comments

thank you !

sara1691362 gravatar imagesara1691362 ( 2016-04-17 10:46:20 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-04-17 09:24:40 -0600

Seen: 4,326 times

Last updated: Apr 17 '16