c++ - OpenCV SVM 训练数据

标签 c++ opencv visual-studio-2013 opencv3.0

我想通过使用 C++ 和 Visual Studio 2013 中的 opencv 3.00 库来学习 svm 实现。 我的代码:

#include<stdio.h>
#include<math.h>
#include<opencv\cv.h>
#include<opencv\highgui.h>
#include<opencv2\objdetect\objdetect.hpp>
#include<opencv2\highgui\highgui.hpp>
#include<opencv2\imgproc\imgproc.hpp>
#include<vector>
#include <windows.h>
#include <atlstr.h>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <opencv2\imgproc\imgproc.hpp>
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv\cvaux.hpp>

using namespace cv;
using namespace std;

#include <opencv2\ml.hpp>

using namespace cv;

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<ml::SVM> svm = ml::SVM::create();
    // edit: the params struct got removed,
    // we use setter/getter now:
    svm->setType(ml::SVM::C_SVC);
    svm->setKernel(ml::SVM::LINEAR);
    svm->setGamma(3);

    svm->train(trainingDataMat, ml::ROW_SAMPLE, labelsMat);

    Mat res;   // output


    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, res);

        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);

} 

运行这段代码后,我得到了那个错误:

OpenCV Error: Bad argument < in the case of classification problem the responses must be categorical; 
either specify varType when creating TrainData, or pass integer responses > in cv::ml::SVMImpl::train, 
file C:\builds\master_PackSlave-win64-vc12-shared\opencv\modules\ml\src\svm.cpp, line 1610

我调试了那个代码。调试器在这一行停止:svm->train(trainingDataMat, ml::ROW_SAMPLE, labelsMat);

它说:

 First-chance exception at 0x000007FEFDA5AAAD in train.exe: Microsoft C++ exception: cv::Exception at memory location 0x00000000001CEE50.
    Unhandled exception at 0x000007FEFDA5AAAD in train.exe: Microsoft C++ exception: cv::Exception at memory location 0x00000000001CEE50.

此外,它说:

(Win32): Loaded 'C:\OpenCV3.0.0\opencv\build\x64\vc12\bin\opencv_world300d.dll'. Cannot find or open the PDB file.

其实我的理解是内存的问题。

最佳答案

responses 的类型不能是floatdouble

改变

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_32S, labels);

顺便说一句,如果你使用的是线性内核,唯一的参数是C,所以你不需要setGamma


另一个问题是获得预测响应的方式。由于每次只有一个样本进行预测,如果您想使用返回值作为响应,则不应将res传递给predict

你可以改变

float response = svm->predict(sampleMat, res);

float response = svm->predict(sampleMat);

否则,如果要使用res,那么返回值就不再是响应值了。但您可以从 res 获取响应。

你可以改变

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

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

关于c++ - OpenCV SVM 训练数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31287207/

相关文章:

python - OpenCV-Python 中的数字识别 OCR

c# - 在 Visual Studio 2013 C# 中更改项目元数据

visual-studio - TFS 2013自动构建: Is there a way to run the tests from the src folder

.net - Visual Studio 2013 : "No framework version is selected, you must select one before creating a project."

c++ - zmq socket可以同时在两个线程中读写吗?

c++ - 删除() vector 中的元素不起作用

opencv - 提取SIFT描述符

c++ - 静态摄像机视频的 OpenCV 噪声去除

c++ - try __finally block 中可能的堆栈损坏

c++ - 如何正确处理来自 MSFTEDIT_CLASS (RichEdit) 控件的 Windows 消息?