I am trying to train SVM to predict the traffic sign, but I always get the same predict result for difference input image.
Here is my code: http://pastebin.com/6y2MgTb8
Can anyone explain why for me?
1 | initial version |
I am trying to train SVM to predict the traffic sign, but I always get the same predict result for difference input image.
Here is my code: http://pastebin.com/6y2MgTb8
Can anyone explain why for me?
2 | No.2 Revision |
I am trying to train SVM to predict the traffic sign, but I always get the same predict result for difference input image.
Here is my code: http://pastebin.com/6y2MgTb8
Can anyone explain why for me?
Here is my code:
vector < Mat > storeInput;
vector < int > labels;
//load input image
char FullFileName[10000];
char FirstFileName[10000] = "./Prohibit_Blue/prohibit_blue";
int fileNum = 4;
for (int i = 0; i< fileNum; ++i)
{
//pos
sprintf_s(FullFileName, "%s_%01d.jpg", FirstFileName, i + 1);
printf("%s\n", FullFileName);
//read image file
Mat img, img_gray, dst, feature;
img = imread(FullFileName);
//resizing
resize(img, img, Size(64, 64));
//convert to grayscale
cvtColor(img, img_gray, CV_BGR2GRAY);
storeInput.push_back(img_gray);
labels.push_back(i);
imshow("prohibit_blue", img_gray);
waitKey(5);
}
//compute HOG
cout << "Start hog...";
Size size = Size(64, 64);
Size block_size = Size(size.width / 4, size.height / 4);
Size block_stride = Size(size.width / 8, size.height / 8);
Size cell_size = block_stride;
int num_bins = 9;
HOGDescriptor hog(size, block_size, block_stride, cell_size, num_bins);
Mat inputHOG;
vector< Point > location;
vector< float > descriptors;
for (int i = 0; i < storeInput.size(); i++)
{
inputHOG = storeInput[i];
hog.compute(inputHOG, descriptors, Size(0, 0), Size(0, 0), location);
//cout << "descriptors size: " << descriptors.size() << endl;
gradient_lst.push_back(Mat(descriptors).clone());
}
cout << "...[done]" << endl;
//trainning with SVM
Mat trainData;
//--Convert data before trainning
const int rows = (int)train_samples.size();
const int cols = (int)std::max(train_samples[0].cols, train_samples[0].rows);
cv::Mat tmp(1, cols, CV_32FC1); //< used for transposition if needed
trainData = cv::Mat(rows, cols, CV_32FC1);
vector< Mat >::const_iterator itr = train_samples.begin();
vector< Mat >::const_iterator end = train_samples.end();
for (int i = 0; itr != end; ++itr, ++i)
{
if (itr->cols == 1)
{
transpose(*(itr), tmp);
tmp.copyTo(trainData.row(i));
}
else if (itr->rows == 1)
{
itr->copyTo(trainData.row(i));
}
}
cout << "Start training...";
Ptr<SVM> svm = SVM::create();
svm->setType(SVM::C_SVC); // classify more than 2 class
svm->setKernel(SVM::LINEAR);
svm->setC(10);
svm->setTermCriteria(cvTermCriteria(CV_TERMCRIT_ITER + CV_TERMCRIT_EPS, 1000, 1e-6));
svm->train(trainData, ROW_SAMPLE, Mat(labels));
cout << "...[done]" << endl;
svm->save("./genfiles/trainSVM_Recog_blue.xml");
cout << "Save XML ..... done!!!" << endl;
//test image
Mat testImg = imread("./Prohibit_Blue/prohibit_blue_1.jpg");
//resizing
resize(img, img, Size(64, 64));
//gray
cvtColor(img, img_gray, COLOR_BGR2GRAY);
//Extract HogFeature
Size size = Size(64, 64);
Size block_size = Size(size.width / 4, size.height / 4);
Size block_stride = Size(size.width / 8, size.height / 8);
Size cell_size = block_stride;
int num_bins = 9;
HOGDescriptor hog(size, block_size, block_stride, cell_size, num_bins);
vector< float> descriptorsValues;
vector< Point> locations;
hog.compute(img_gray, descriptorsValues, Size(0, 0), Size(0, 0), locations);
//vector to Mat
Mat fm = Mat(descriptorsValues);
//Classification data
Ptr<SVM> svm2 = Algorithm::load<SVM>("./genfiles/trainSVM_Recog_blue.xml");
std::cout << "Model Loaded" << std::endl;
Mat image1d(1, fm.rows, CV_32FC1);
imshow("gradient 1", image1d);
float result = svm2->predict(image1d);
cout << "Predict value: " << result << endl;