Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

SVM Training Data Error

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