用图像训练SVM并进行预测

Training an SVM with images and making predictions

本文关键字:SVM 图像      更新时间:2023-10-16

感谢您的帮助...

我不久前编写了一些代码,这些代码成功地检测到了动人的流量视频中的汽车。因此,让我们考虑该代码的输出,该代码的最终输入为150x200尺寸的车辆图像。

我试图实施的是一种SVM,它可以采用这些车辆并可以在轿车和SUV之间进行分类。(假设只有轿车和SUV)。

通过紧随此链接的信息:https://docs.opencv.org/3.0-beta/doc/tutorials/ml/introduction_to_svm/introduction_to_svm.html和此链接:使用OpenCV和SVM与图像

请注意,与这些链接相关的语法对于我拥有的最新版本的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);

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

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

代码构建正常,但不幸的是,当它到达SVM->火车时,它会失败,我会遇到一个中止错误,上面写着:" openCV错误:不良参数(在分类问题的情况下,响应必须是分类的;要么要么;在CV :: ML :: SvMimpl :: Train中指定vartype时指定vartype或通过整数响应)

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

谢谢

通过将labels_mat更改为cv_32s的整数类型来解决此问题。不幸的是,仍然存在一个新问题,即SVM->预测(test_image)返回一个不是-1或1的值。