Ask Your Question

Revision history [back]

Your program is OK. Ihave tested it . As you can see positive and negative values are not in same class..

        Mat groups;
        Mat samples(10,1,CV_32F);
        RNG r;
        r.fill(samples,RNG::NORMAL,0,2);
        cout<<samples<<endl;
        for (int j = 1; j <= 10; ++j) 
        {
            groups.push_back(0);
        }
        Ptr<ml::SVM> classifierSVM = ml::SVM::create();
            classifierSVM->setType(ml::SVM::ONE_CLASS);
            classifierSVM->setKernel(ml::SVM::LINEAR);
            classifierSVM->setDegree(3);
            classifierSVM->setGamma(1);
            classifierSVM->setCoef0(0);
            classifierSVM->setC(1);
            classifierSVM->setNu(0.5);
            classifierSVM->setP(0);
            classifierSVM->setTermCriteria(cvTermCriteria(CV_TERMCRIT_ITER,
                500, FLT_EPSILON));

            classifierSVM->train(samples, ml::ROW_SAMPLE, groups);
            classifierSVM->save("trainingData.yml");

        //Test for 10 image sample - 7 & 9 must be classified.
       for (int i = 1; i <= 10; ++i) {
            Mat test(1,1,CV_32F);
            test.at<float>(0,0)= float(r.gaussian(200));
            float result = classifierSVM->predict(test);
            cout << test.at<float>(0, 0) <<": class " << result << endl;
        }
        waitKey(0);

and results are

[-3.2061895e-09;
 0.31627011;
 -1.4018559;
 -0.85719198;
 2.4774842;
 -0.55601645;
 -1.2967975;
 0.61032659;
 1.0300292;
 -2.2900875]

153.969: class 0
-9.28624: class 1
261.181: class 0
-391.937: class 1
-379.843: class 1
-107.448: class 1
-149.658: class 1
379.299: class 0
54.8101: class 0
-276.041: class 1