OpenCV无法设置SVM参数

问题描述 投票:1回答:1

我刚刚开始学习使用C++ OpenCV的SVM,并参考了SVM的文档 此处. 我想先试试链接中的示例源码熟悉一下,但我无法运行示例源码。它返回的是错误。

错误 1 错误 C2065: 'CvSVMParams' : 未声明的标识符。

我使用的是Visual Studio 2012与OpenCV 3.0.0.0,设置过程应该是正确的,因为除了这个之外,其他代码都运行良好。

c++ qt opencv
1个回答
10
投票

很多东西都变了 从OpenCV 2.4到OpenCV 3.0。. 其中,机器学习模块,并不向后兼容。

这就是OpenCV SVM的教程代码,更新为OpenCV 3.0。

#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include "opencv2/imgcodecs.hpp"
#include <opencv2/highgui.hpp>
#include <opencv2/ml.hpp>

using namespace cv;
using namespace cv::ml;

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

    // Set up training data
    int labels[4] = { 1, -1, -1, -1 };
    Mat labelsMat(4, 1, CV_32SC1, 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->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 100, 1e-6));

    // Train the SVM with given parameters
    Ptr<TrainData> td = TrainData::create(trainingDataMat, ROW_SAMPLE, labelsMat);
    svm->train(td);

    // Or train the SVM with optimal parameters
    //svm->trainAuto(td);

    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;
    Mat sv = svm->getSupportVectors();

    for (int i = 0; i < sv.rows; ++i)
    {
        const float* v = sv.ptr<float>(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);

}

输出应该是这样的

enter image description here


0
投票

我发现上面的代码是可行的 但我需要做一个小的修改 来把标签转换成整数。修改的内容用粗体表示。

// Set up training data **Original**:

int labels[4] = { 1, -1, -1, -1 };

Mat labelsMat(4, 1, **CV_32SC1**, labels);

// Set up training data **Modified**:

int labels[4] = { 1, -1, -1, -1 };

Mat labelsMat(4, 1, **CV_32S**, labels);
© www.soinside.com 2019 - 2024. All rights reserved.