I am getting an exception error when running the code at the train_samples, is there something missing in the code or any error?
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <math.h>
#include "opencv2/highgui/highgui.hpp"
#include <vector>
#include "opencv2/objdetect/objdetect.hpp"
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <opencv2/opencv.hpp>
#include <fstream>
#include <time.h>
using namespace cv;
using namespace cv::ml;
using namespace std;
int main(int argc, char** argv)
{
vector < Mat > storeInput;
vector < int > labels;
//load input image
char FullFileName[10000];
char FirstFileName[10000] = "./Proj/Dissertation/train_samples/image218.jpg";
//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);
imshow("trainsample", 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;
Mat gradient_lst;
gradient_lst.push_back(Mat(descriptors).clone());
}
cout << "...[done]" << endl;
//training with SVM
Mat trainData;
//--Convert data before training
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(".train_SVM.xml");
cout << "Save XML ..... done!!!" << endl;
//test image
Mat testImg = imread("./Proj/Dissertation/train_samples/image218.jpg");
//resizing
Mat img, img_gray, dst, feature;
resize(img, img, Size(64, 64));
//gray
Mat img, img_gray, dst, feature;
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>(".train_SVM.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;
return 0;
}