使用图像训练SVM并进行预测

时间:2017-12-15 05:55:36

标签: c++ opencv machine-learning svm

感谢您的帮助......

前段时间我写了一些代码,成功检测到移动视频中的汽车。因此,让我们考虑该代码的输出以及此代码的最终输入是150x200大小的车辆图像。

我想要实现的是一个SVM,可以接收这些车辆,并可以在轿车和SUV之间进行分类。 (假设只有轿车和SUV)。

通过密切关注此链接上的信息来实施以下代码:https://docs.opencv.org/3.0-beta/doc/tutorials/ml/introduction_to_svm/introduction_to_svm.html 这个链接:using OpenCV and SVM with images

请注意,与我最新版本的SVM上的SVM实现相关联的这些链接的语法略显过时。

    //Used to read multiple files from folder
stringstream ss;
string name = "Vehicle_";
string type = ".jpg";

int num_train_images = 29;      //29 images will be used to train the SVM
int image_area = 150 * 200;
Mat training_mat(num_train_images, image_area, CV_32FC1);   // Creates a 29 rows by 30000 columns... 29 150x200 images will be put into 1 row per image

//Converts 29 2D images into a really long row per image
for (int file_count = 1; file_count < (num_train_images + 1); file_count++) 
{
    ss << name << file_count << type;       //'Vehicle_1.jpg' ... 'Vehicle_2.jpg' ... etc ...
    string filename = ss.str();
    ss.str("");

    Mat training_img = imread(filename, 1);     //Reads the training images from the folder
    int ii = 0;                                 //Scans each column
    for (int i = 0; i < training_img.rows; i++) 
    {
        for (int j = 0; j < training_img.cols; j++)
        {
            training_mat.at<float>(file_count - 1, ii) = training_img.at<uchar>(i, j);  //Fills the training_mat with the read image
            ii++; 
        }
    }
}

//Labels are used as the supervised learning portion of the SVM. If it is a 1, its an SUV test image. -1 means a sedan. 
float labels[29] = { 1, 1, -1, -1, 1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, 1, 1, 1, -1, -1, -1, -1, 1, 1, 1, -1, 1 };

//Place the labels into into a 29 row by 1 column matrix. 
Mat labels_mat(num_train_images, 1, CV_32FC1, labels);

cout << "Beginning Training..." << endl;

//Set SVM Parameters (not sure about these values)
Ptr<SVM> svm = SVM::create();
svm->setType(SVM::C_SVC);
svm->setKernel(SVM::LINEAR);
svm->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 100, 1e-6));

svm->train(training_mat, ROW_SAMPLE, labels_mat);

cout << "End Training" << endl;

waitKey(0);

Mat test_image(1, image_area, CV_32FC1);        //Creates a 1 x 30000 matrix to house the test image. 

Mat SUV_image = imread("SUV_1.jpg", 0);         //Read the file folder
int jj = 0;
for (int i = 0; i < SUV_image.rows; i++)
{
    for (int j = 0; j < SUV_image.cols; j++)
    {
        test_image.at<float>(0, jj) = SUV_image.at<uchar>(i, j);    //Fills the training_mat
        jj++;
    }
}

//Should return a 1 if its an SUV, or a -1 if its a sedan
svm->predict(test_image);

waitKey(0);

所以我在这里做的是我拍摄测试图像,然后将每个150乘200的图像转换为training_mat中的1行乘30,000列行。

labels_mat是SVM的监督学习部分,它告诉训练图像是SUV还是轿车。

代码构建正常,但不幸的是,当它到达svm-&gt;训练它失败并且我得到一个中止错误,说:“OpenCV错误:错误的参数(在分类问题的情况下,响应必须是分类的;要么在创建TrainData时指定varType,要么在cv :: ml :: SVMImpl :: train中传递整数响应“

不太清楚这意味着什么,我的参数可能有问题。一位朋友建议我可能需要在将图像输入SVM之前提取图像的功能,我不确定它是否必要。

由于

1 个答案:

答案 0 :(得分:0)

通过将labels_mat更改为CV_32S,将此问题解决为整数类型。不幸的是,仍然存在一个新问题,即svm-&gt; predict(test_image)返回一个不是-1或1的大值。

相关问题