简单数据集的 SVM 训练问题 (Opencv 2.4.9)

SVM training issue for a simple dataset (Opencv 2.4.9)

我正在尝试一个简单的例子来学习 OpenCV 中的 SVM,训练后我没有得到正确的支持向量。需要一些帮助来理解这个问题。 我的代码是:

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

using namespace cv;
using namespace std;

int main() {

Mat frame(Size(640,360), CV_8UC3, Scalar::all(255));
float train[15][2] = { {296, 296}, {296, 312}, {312,   8}, {312,  56}, {312,  88}, {328,  88}, {328, 104}, {328, 264}, {344,   8}, {344,  40}, {360,   8}, {360,  56}, {376,   8}, {376,  40}, {376,  56} };
Mat trainingDataMat(15, 2, CV_32FC1, train);
float labels[15] = { -1, -1, 1, 1, 1, 1, 1, -1, 1, 1, 1, 1, 1, 1, 1 };
Mat labelsMat(15, 1, CV_32FC1, labels);

CvSVMParams param;
param.svm_type     = CvSVM::C_SVC;
param.C            = 0.1;
param.kernel_type  = SVM::LINEAR;
param.term_crit    = TermCriteria(CV_TERMCRIT_ITER, 1000, 1e-6);

CvSVM SVM;

SVM.train(trainingDataMat, labelsMat, Mat(), Mat(), param);
cout<< "Training Finished..." << endl;
for(int i = 0; i < frame.rows; ++i) {
    for(int j = 0; j < frame.cols; ++j) {
        Mat sampleMat = (Mat_<float>(1,2) << i,j);
        float response = SVM.predict(sampleMat);
        //cout << response << endl;
        if(response == 1) {
            frame.at<Vec3b>(i,j)[2] = 0;
        } else {
            frame.at<Vec3b>(i,j)[0] = 0;
        }
    }
}
for(int dis = 0; dis < trainingDataMat.rows; dis++) {
    if(labels[dis] == 1) {
        circle(frame, Point((int)train[dis][0], (int)train[dis][1]), 3, Scalar (0, 0, 0), -1);
    } else {
        circle(frame, Point((int)train[dis][0], (int)train[dis][1]), 3, Scalar (0, 255, 0), -1);
    }
}    
int n = SVM.get_support_vector_count();
for(int i = 0; i < n; i++) {
          const float* v = SVM.get_support_vector(i);
          cout << "support Vectors : " << v[0] << " " << v[1] <<endl;
          circle(frame,Point((int)v[0], (int)v[1]), 6, Scalar(128, 128, 128), 2, 8);
}
imwrite("frame.jpg",frame);
imshow("output", frame);
waitKey(0);

return 0;
}

Output image is attached

SVM 线没有像我预期的那样将两条 类 分开。

支持向量的结果是

 support Vectors : 0 0.0125

SVM应该没问题。我认为问题出在您的显示上。当您调用 circle(frame, Point((int)train[dis][0], (int)train[dis][1]), 3, Scalar (0, 0, 0), -1); 时,OpenCV 知道您需要行号 train[dis][1] 和列号 train[dis][0] 中的圆圈。这不是您想要的,因为 OpenCV 的一个特殊性是它对矩阵和点使用不同的坐标系。 image.at<float>(Point(i,j)) 等同于 image.at<float>(j,i)

尝试用此替换您的 circle 调用:

if(labels[dis] == 1) {
    circle(frame, Point((int)train[dis][1], (int)train[dis][0]), 3, Scalar (0, 0, 0), -1);
} else {
    circle(frame, Point((int)train[dis][1], (int)train[dis][0]), 3, Scalar (0, 255, 0), -1);
}