Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

SVM train error

I tried to train SVM with some data. but an exception is thrown at svm.train line.

#include "opencv2/opencv.hpp"
#include<iostream>
#include<stdio.h>

using namespace cv;
using namespace std;

int main()
{
    //int image_regions=4;//number of samples;
    //Mat training_mat(image_regions,7,CV_32FC1);
    //Mat labels(image_regions,1,CV_32FC1);
    double hu[4][7];


    hu[0][0]=0.00120898;
    hu[0][1]=5.11818e-08;
    hu[0][2]=3.55067e-10;
    hu[0][3]=6.66231e-11;
    hu[0][4]=8.45525e-21;
    hu[0][5]=3.247e-15;
    hu[0][6]=-5.78858e-21;


    hu[1][0]=0.000857154;
    hu[1][1]=1.52204e-07;
    hu[1][2]=1.95253e-10;
    hu[1][3]=2.13237e-11;
    hu[1][4]=7.62358e-22;
    hu[1][5]=5.54739e-15;
    hu[1][6]=-1.14541e-21;



    hu[2][0]=0.000880923;
    hu[2][1]=3.07455e-07;
    hu[2][2]=1.11048e-11;
    hu[2][3]=5.6902e-14;
    hu[2][4]=-1.53374e-26;
    hu[2][5]=8.70844e-18;
    hu[2][6]=4.25523e-26;
    // negative data
    hu[3][0]=0.00180891;
    hu[3][1]=2.42715e-006;
    hu[3][2]=2.77416e-010;
    hu[3][3]=5.19146e-011;
    hu[3][4]=3.15921e-022;
    hu[3][5]=6.22216e-021;
    hu[3][6]=2.29176e-313;


    for(int i=0;i<4;i++)
        {
            for(int j=0;j<7;j++)
                cout<<hu[i,j]<<'\n';
            cout<<"--------------------------------------------------- "<<'\n';
    }

Mat training_mat(4,7,CV_32FC1);
Mat labels(4,7,CV_32FC1);



    for(int l=0;l<4;l++)
    {
        for(int k=0;k<7;k++)
            training_mat.at<float>(l,k) = hu[l][k];
    }


    for(int l=0;l<4;l++)
    {
        for(int k=0;k<7;k++)
            if(l==3)
                labels.at<float>(l,k)=-1.0;
            else 
                labels.at<float>(l,k)=1.0;
    }





CvSVM svm;
CvSVMParams params;
params.svm_type = CvSVM::C_SVC;
params.kernel_type=CvSVM::LINEAR;
params.term_crit=cvTermCriteria(CV_TERMCRIT_ITER,1000,1e-6);
svm.train(training_mat,labels,Mat(),Mat(),params);

waitKey(0);

}