我的目标是使用SVM和HOG功能对SUV和轿车进行分类。
首先,我阅读了86个训练图像,计算每个训练图像的HOG特征,然后将它们放入一个大小为86xdescriptorSize的训练垫中,称为HOGFeat_train。
Mat HOGFeat_train(num_train_images, derSize, CV_32FC1); //86xdescriptor size training Mat
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
HOGDescriptor hog_train;
vector<float> descriptors_train;
vector<Point> locations_train;
hog_train.compute(training_img, descriptors_train, Size(16, 16), Size(0, 0), locations_train); //not sure about these values
for (int i = 0; i < descriptors_train.size(); i++)
HOGFeat_train.at<float>(file_count-1, i) = descriptors_train.at(i);
}
接下来,我为SVM的监督学习部分创建了86个标签的labels_mat(我知道这种方式不切实际且耗时,我稍后会修复)。 1表示SUV,-1表示轿车。不确定这些SVM参数,但我尝试过不同的品种和价值,但所有结果都是一样的。
float labels[86] = { 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, -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, -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};
Mat labels_mat(num_train_images, 1, CV_32S);
cout << "Beginning Training..." << endl;
Ptr<SVM> svm = SVM::create();
svm->setType(SVM::C_SVC);
svm->setKernel(SVM::LINEAR);
//svm->setDegree(3);
//svm->setGamma(2);
//svm->setC(.5);
cout << "Parameters Set..." << endl;
svm->train(HOGFeat_train, ROW_SAMPLE, labels_mat);
cout << "Training Successful" << endl;
接下来,我以与训练图像相同的方式读取10个测试图像,并再次计算HOG特征。在计算HOG特征之后,将它们放入1行x descriptorSized HOGFeat_test Mat中,然后我在该HOGFeat_test Mat上使用svm-&gt;预测,其应该返回值-1以表示轿车或1以表示SUV。
Mat HOGFeat_test(1, derSize, CV_32FC1); //Creates a 1 x descriptorSize Mat to house the HoG features from the test image
for (int file_count = 1; file_count < (num_test_images + 1); file_count++)
{
ss2 << name2 << file_count << type2; //'Test_1.jpg' ... 'Test_2.jpg' ... etc ...
string filename2 = ss2.str();
ss2.str("");
Mat test_image = imread(filename2, 0); //Read the file folder
HOGDescriptor hog_test;
vector<float> descriptors_test;
vector<Point> locations_test;
hog_test.compute(test_image, descriptors_test, Size(16, 16), Size(0, 0), locations_test);
for (int i = 0; i < descriptors_test.size(); i++)
HOGFeat_test.at<float>(0, i) = descriptors_test.at(i);
namedWindow("Test Image", CV_WINDOW_NORMAL);
imshow("Test Image", test_image);
//Should return a 1 if its an SUV, or a -1 if its a sedan
float result = svm->predict(HOGFeat_test);
if (result <= 0)
cout << "Sedan" << endl;
else
cout << "SUV" << endl;
cout << "Result: " << result << endl;
下图显示了结果,测试图像和HOGFeat_train Mat,以防对任何人有用。无论我使用什么值或参数或图像,结果(Sedan,-8.412e08)总是相同的。结果不是-1或1而是-800000000000而我假设负值对应于-1,但最重要的是我想知道为什么结果不会改变。有没有人对此有任何见解?谢谢。
EDIT ----------------------------------------
我从浮动标签中删除了所有的[86]并简单地将其留作浮动标签[86]; // {1,1,-1等等......}
这表明SVM结果没有差异,仍然能够成功训练。这告诉我,我的标签不通过svm-&gt;列车功能或其他东西。我会继续调查。
答案 0 :(得分:0)
所以这个只是我对自己做的一个愚蠢的错误。我用浮动标签替换了标签[86];基本上我刚刚删除了SVM的监督学习部分,但我得到了完全相同的结果。经过检查,我意识到我没有将标签填入labels_mat !!!!!!
因此,以明确的解决方案执行以下结果。此外,结果变为1和-1而不是-8.412e-8。
Mat labels_mat(num_train_images,1,CV_32S,labels);