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



我正在尝试一个简单的例子来学习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;
}

附加输出图像

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

支持向量结果为

 support Vectors : 0 0.0125

支持向量机应该没问题。我认为问题出在你的展示上。当你调用你的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);
}

相关内容

  • 没有找到相关文章

最新更新