使用SVM在SUV和轿车之间进行分类

时间:2017-12-21 01:06:36

标签: opencv machine-learning svm

我正在尝试使用OpenCV实现一个SVM,它可以对轿车和SUV的图像进行分类。我大量引用了这篇文章:using OpenCV and SVM with images

我有29辆轿车和SUV的训练图像,我将每张图像拉伸成1个非常长的行,从而使我的训练垫大小为29ximage_area。下图显示training_mat全部为白色,我不确定是否正确,可能会影响我的结果。

这可能是由于training_mat是浮点类型。例如,如果training_mat被更改为CV_8UC1,我可以清楚地看到每个图像在training_mat中展开,但是svm-> train函数不接受training_mat。

我使用labels_mat作为实现的监督版本。 A 1表示SUV,-1表示轿车。在下图中,当我尝试使用SVM模型预测SUV时,我得到的值类似于-800000000000。无论我做什么(更改参数,使用全白测试图像,所有黑色测试图像,将标签更改为仅1或-1)我总是得到相同的-80000000000值。现在任何负面结果可能只意味着-1(轿车),但我不能确定,因为它永远不会改变。如果有人对此有所了解,那将不胜感激

这是我的代码,结果和所有白色training_mat。 Result Image

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, 0);     //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++; 
        }
    }
}

imshow("Training Mat", training_mat);
waitKey(0);

//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. 
int 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_32S);

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::RBF);
svm->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 100, 1e-6));
svm->setGamma(1);
svm->setDegree(3);

cout << "Parameters Set..." << endl;

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 1200 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
float result = svm->predict(test_image);

if (result < 0)
    cout << "Sedan" << endl;
else
    cout << "SUV" << endl;

cout << "Result: " << result << endl;

namedWindow("Test Image", CV_WINDOW_NORMAL);
imshow("Test Image", SUV_image);
waitKey(0);

1 个答案:

答案 0 :(得分:0)

请参阅这篇文章,了解我遇到的这个问题。 Using SVM with HOG Features to Classify Vehicles

在此,我使用HOG功能而不仅仅是图像的纯像素值。 training_mat不再是白色,分类器运行良好。此外,输出结果为1或-1。