使用c ++的SVM教程

时间:2015-12-07 11:56:55

标签: c++ opencv opencv3.0

我正在尝试使用SVM进行c ++对象检测。我正在关注这个answer。我面临一个问题,即CvSVM目前尚未使用。所以我修改了训练代码如下。

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/ml/ml.hpp>

using namespace cv;
using namespace cv::ml;

int main()
{
    // Data for visual representation
        int width = 512, height = 512;
    Mat image = Mat::zeros(height, width, CV_8UC3); 

    // Set up training data
        float labels[4] = {1.0, -1.0, -1.0, -1.0};
        Mat labelsMat(4, 1, CV_32FC1, labels);  
    float trainingData[4][2] = { {501, 10}, {255, 10}, {501, 255}, {10, 501} };
    Mat trainingDataMat(4, 2, CV_32FC1, trainingData);

// Set up SVM's parameters
        Ptr<SVM> svm = SVM::create();
        svm->setType(SVM::C_SVC);
        svm->setKernel(SVM::LINEAR);
//svm.term_crit   = SVM::getTermCriteria(CV_TERMCRIT_ITER, 100, 1e-6);

// Train the SVM
//Ptr<SVM> svm1 = SVM::trainAuto();

        SVM->train(trainingDataMat, labelsMat, Mat(), Mat(), svm);

        Vec3b green(0,255,0), blue (255,0,0);
// Show the decision regions given by the SVM
        for (int i = 0; i < image.rows; ++i)
        for (int j = 0; j < image.cols; ++j)
        {
                Mat sampleMat = (Mat_<float>(1,2) << j,i);
                float response = svm->predict(sampleMat);

                if (response == 1)
                image.at<Vec3b>(i,j)  = green;
                else if (response == -1)
                image.at<Vec3b>(i,j)  = blue;
        }

// Show the training data
        int thickness = -1;
        int lineType = 8;
        circle( image, Point(501,  10), 5, Scalar(  0,   0,   0), thickness, lineType);
        circle( image, Point(255,  10), 5, Scalar(255, 255, 255), thickness, lineType);
        circle( image, Point(501, 255), 5, Scalar(255, 255, 255), thickness, lineType);
        circle( image, Point( 10, 501), 5, Scalar(255, 255, 255), thickness, lineType);

// Show support vectors
        thickness = 2;
        lineType  = 8;
        int c     = SVM.get_support_vector_count();

        for (int i = 0; i < c; ++i)
        {
            const float* v = SVM.get_support_vector(i);
            circle( image,  Point( (int) v[0], (int) v[1]),   6,   Scalar(128, 128, 128), thickness, lineType);
        }

        imwrite("result.png", image);        // save the image

        imshow("SVM Simple Example", image); // show it to the user
        waitKey(0);

}

我无法实现列车功能。它说没有找到该功能。请帮助我使用此代码的更新版本。

1 个答案:

答案 0 :(得分:2)

您的代码中有多处错误。有些是C ++语法错误,有些是因为您使用的是OpenCV 2.4.X api,这与OpenCV 3.0不同

1)当你引用svm实例时,你应该使用svm(变量名),而不是SVM类名。

2)在分类问题的情况下,回答必须是分类的。所以

float labels[4] = { 1.0, -1.0, -1.0, -1.0 };
Mat labelsMat(4, 1, CV_32FC1, labels);

应该是:

int labels[4] = { 1, -1, -1, -1 };
Mat labelsMat(4, 1, CV_32SC1, labels); 

3)train接受不同的参数。 SVM->train(trainingDataMat, labelsMat, Mat(), Mat(), svm);应该是:svm->train(trainingDataMat, ROW_SAMPLE, labelsMat);

4)OpenCV 3.0中不存在get_support_vector_countint c = SVM.get_support_vector_count();应该是:int c = svm->getSupportVectors().rows;

5)OpenCV 3.0中不存在get_support_vectorconst float* v = SVM.get_support_vector(i);应该是:const float* v = svm->getSupportVectors().ptr<float>(i);

this答案中的代码已按预期工作。如果你引入这样的错误,它显然不会起作用。