This is the code i did for train svm with surf features , it didn't show me the any syntax error but i think there is something logical wrong in it
int main( int argc, char** argv )
{
FlannBasedMatcher matcher;
std::vector< DMatch > matches;
double max_dist = 0;
double min_dist = 100;
SurfDescriptorExtractor extractor;
Mat descriptors_1;
SurfFeatureDetector detector(500);
std::vector<KeyPoint> keypoints;
Mat img_keypoints_1;
string YourImagesDirectory="D:\\Cars\\";
vector<string> files=listFilesInDirectory(YourImagesDirectory+"*.jpg");
//Load NOT cars!
string YourImagesDirectory_2="D:\\not_cars\\";
vector<string> files_no=listFilesInDirectory(YourImagesDirectory_2+"*.jpg");
// Initialize constant values
int nb_cars = files.size();
const int not_cars = files_no.size();
const int num_img = nb_cars + not_cars; // Get the number of images
const int image_area = 30*40;
// Initialize your training set.
Mat training_mat(num_img,image_area,CV_32FC1);
Mat labels(num_img,1,CV_32FC1);
// Set temp matrices
Mat tmp_img;
Mat tmp_dst( 30, 40, CV_8UC1 ); // to the right size for resize
// Load image and add them to the training set
int count = 0;
vector<string>::const_iterator i;
for (i = files.begin(); i != files.end(); ++i)
{
tmp_img = imread( YourImagesDirectory+*i, 0 ); // load in grayscale.
resize( tmp_img, tmp_dst, tmp_dst.size() );
Mat row_img = tmp_dst.reshape( 1, 1 ); // get a one line image.
detector.detect( row_img, keypoints);
drawKeypoints( row_img, keypoints, img_keypoints_1, Scalar::all(-1), DrawMatchesFlags::DEFAULT );
extractor.compute( row_img, keypoints, descriptors_1);
row_img.convertTo( training_mat.row(count), CV_32FC1 );
labels.at< float >(count, 0) = (count<nb_cars)?1:-1; // 1 for car, -1 otherwise*/
++count;
}
// Train your SVM
CvSVMParams Params;
Params.svm_type=CvSVM::C_SVC;
Params.kernel_type=CvSVM::LINEAR;
Params.term_crit = cvTermCriteria(CV_TERMCRIT_ITER, 100, 1e-6);
Params.gamma=3;
CvSVM svm;
svm.train(training_mat,labels,Mat(),Mat(),Params);
svm.save("trainsvm.txt");
Mat descriptors_2;
vector<KeyPoint> keypoint2;
Mat predict_img;
Mat img_keypoints_2;
predict_img = cvLoadImage("images.jpg",0);
resize( predict_img, tmp_dst, tmp_dst.size() );
Mat row_img_pred = tmp_dst.reshape( 1, 1 ); // get a one line image.
detector.detect(row_img_pred, keypoint2);
drawKeypoints( row_img_pred, keypoint2, img_keypoints_2, Scalar::all(-1), DrawMatchesFlags::DEFAULT );
extractor.compute( row_img_pred, keypoint2, descriptors_2);
float response = svm.predict(descriptors_2);
cout<<response;
}
and When i am going to predict the image for result , it give me the runtime error in it and didn't give me the prediction result
Here is the link for my data of cars and not_cars