Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Svm using Surf Features

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

image description

Here is the link for my data of cars and not_cars

Link of directories data

Svm using Surf Features

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

image description

Here is the link for my data of cars and not_cars

Link of directories data

Also tried this code in loop

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);
    extractor.compute( row_img, keypoints, descriptors_1);
    training_mat.push_back(descriptors_1);
    labels.at< float >(count, 0) = (count<nb_cars)?1:-1; // 1 for car, -1 otherwise*/
    ++count;
}

Svm using Surf Features

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

image description

Here is the link for my data of cars and not_cars

Link of directories data

Also tried this code in loop

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);
    extractor.compute( row_img, keypoints, descriptors_1);
    training_mat.push_back(descriptors_1);
    labels.at< float >(count, 0) = (count<nb_cars)?1:-1; // 1 for car, -1 otherwise*/
    ++count;
}

Svm using Surf Features

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
std::vector<string> all_names;

all_names.assign(files.begin(),files.end());
all_names.insert(all_names.end(), files_no.begin(), files_no.end());
// Load image and add them to the training set
int count = 0;
vector<string>::const_iterator i;
string Dir;
for (i = files.begin(); all_names.begin(); i != files.end(); all_names.end(); ++i)
{
    Dir=( (count < files.size() ) ? YourImagesDirectory : YourImagesDirectory_2);
    tmp_img = imread( YourImagesDirectory+*i, Dir +*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

image description

Here is the link for my data of cars and not_cars

Link of directories data

Also tried this code in loop

for (i = files.begin(); all_names.begin(); i != files.end(); all_names.end(); ++i)
{
    Dir=( (count < files.size() ) ? YourImagesDirectory : YourImagesDirectory_2);
    tmp_img = imread( YourImagesDirectory+*i, Dir +*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);
    extractor.compute( row_img, keypoints, descriptors_1);
    training_mat.push_back(descriptors_1);
    labels.at< float >(count, 0) = (count<nb_cars)?1:-1; // 1 for car, -1 otherwise*/
    ++count;
}

Svm using Surf Features

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
std::vector<string> all_names;

all_names.assign(files.begin(),files.end());
all_names.insert(all_names.end(), files_no.begin(), files_no.end());
// Load image and add them to the training set
int count = 0;
vector<string>::const_iterator i;
string Dir;
for (i = all_names.begin(); i != all_names.end(); ++i)
{
    Dir=( (count < files.size() ) ? YourImagesDirectory : YourImagesDirectory_2);
    tmp_img = imread( Dir +*i, 0 );
    resize( tmp_img, tmp_dst, tmp_dst.size() );
    Mat row_img = tmp_dst.reshape( 1, 1 ); tmp_dst; // 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

image description

Here is the link for my data of cars and not_cars

Link of directories data

Also tried this code in loop

for (i = all_names.begin(); i != all_names.end(); ++i)
{
    Dir=( (count < files.size() ) ? YourImagesDirectory : YourImagesDirectory_2);
    tmp_img = imread( Dir +*i, 0 );
    resize( tmp_img, tmp_dst, tmp_dst.size() );
    Mat row_img = tmp_dst.reshape( 1, 1 ); tmp_dst; // get a one line image.
    detector.detect( row_img, keypoints);
    extractor.compute( row_img, keypoints, descriptors_1);
    training_mat.push_back(descriptors_1);
    labels.at< float >(count, 0) = (count<nb_cars)?1:-1; // 1 for car, -1 otherwise*/
    ++count;
}

Svm using Surf Features

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
std::vector<string> all_names;

all_names.assign(files.begin(),files.end());
all_names.insert(all_names.end(), files_no.begin(), files_no.end());
// Load image and add them to the training set
int count = 0;
vector<string>::const_iterator i;
string Dir;
for (i = all_names.begin(); i != all_names.end(); ++i)
{
    Dir=( (count < files.size() ) ? YourImagesDirectory : YourImagesDirectory_2);
    tmp_img = imread( Dir +*i, 0 );
    resize( tmp_img, tmp_dst, tmp_dst.size() );
    Mat row_img = tmp_dst; // 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

image description

Here is the link for my data of cars and not_cars

Link of directories data

Also tried this code in loop

 for (i = all_names.begin(); i != all_names.end(); ++i)
 {
     Dir=( (count < files.size() ) ? YourImagesDirectory : YourImagesDirectory_2);
     tmp_img = imread( cv::imread( Dir +*i, 0 );
     resize( tmp_img, tmp_dst, tmp_dst.size() );
     Mat row_img = tmp_dst; // get a one line image.
     detector.detect( row_img, keypoints);
     extractor.compute( row_img, keypoints, descriptors_1);
    training_mat.push_back(descriptors_1);

      resize( row_img, tmp_dst, tmp_dst.size() );

      Mat train_img = tmp_dst.reshape( 1, 1 );

      train_img.convertTo( training_mat.row(count), CV_32FC1 );

      labels.at< float >(count, 0) = (count<nb_cars)?1:-1; // 1 for car, -1 otherwise*/
otherwise

      ++count;
 }

Svm using Surf Features

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
std::vector<string> all_names;

all_names.assign(files.begin(),files.end());
all_names.insert(all_names.end(), files_no.begin(), files_no.end());
// Load image and add them to the training set
int count = 0;
vector<string>::const_iterator i;
string Dir;
for (i = all_names.begin(); i != all_names.end(); ++i)
{
    Dir=( (count < files.size() ) ? YourImagesDirectory : YourImagesDirectory_2);
    tmp_img = imread( Dir +*i, 0 );
    resize( tmp_img, tmp_dst, tmp_dst.size() );
    Mat row_img = tmp_dst; // 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

image description

Here is the link for my data of cars and not_cars

Link of directories data

Also tried this loop in the code above , this is working fine , but as i am new to opencv and beginner i don't know whether my approach in loopthis loop is right or not , because my next step is to recognize the object from video

    for (i = all_names.begin(); i != all_names.end(); ++i)
    {
      Dir=( (count < files.size() ) ? YourImagesDirectory : YourImagesDirectory_2);

      tmp_img = cv::imread( Dir +*i, 0 );

      resize( tmp_img, tmp_dst, tmp_dst.size() );

      Mat row_img = tmp_dst; // get a one line image.

      detector.detect( row_img, keypoints);

      extractor.compute( row_img, keypoints, descriptors_1);

      resize( row_img, tmp_dst, tmp_dst.size() );

      Mat train_img = tmp_dst.reshape( 1, 1 );

      train_img.convertTo( training_mat.row(count), CV_32FC1 );

      labels.at< float >(count, 0) = (count<nb_cars)?1:-1; // 1 for car, -1 otherwise

      ++count;
  }

I also upload the result of above loop code which is working fine without error in the code , which i get from svm.save(result.txt) , upload in the link given

Svm using Surf Features

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
std::vector<string> all_names;

all_names.assign(files.begin(),files.end());
all_names.insert(all_names.end(), files_no.begin(), files_no.end());
// Load image and add them to the training set
int count = 0;
vector<string>::const_iterator i;
string Dir;
for (i = all_names.begin(); i != all_names.end(); ++i)
{
    Dir=( (count < files.size() ) ? YourImagesDirectory : YourImagesDirectory_2);
    tmp_img = imread( Dir +*i, 0 );
    resize( tmp_img, tmp_dst, tmp_dst.size() );
    Mat row_img = tmp_dst; // 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

image description

Here is the link for my data of cars and not_cars

Link of directories data

Edit

Also tried this loop in the code above , this is working fine , but as i am new to opencv and beginner i don't know whether my approach in this loop is right or not , because my next step is to recognize the object from video

 int dictionarySize = 1500;        
   int retries = 1;
   int flags = KMEANS_PP_CENTERS;
   BOWKMeansTrainer bowTrainer(dictionarySize, tc, retries, flags);
   BOWImgDescriptorExtractor bowDE(extractor, matcher);
    for (i = all_names.begin(); i != all_names.end(); ++i)
    {
      Dir=( (count < files.size() ) ? YourImagesDirectory : YourImagesDirectory_2);

      tmp_img = cv::imread( Dir +*i, 0 );

      resize( tmp_img, tmp_dst, tmp_dst.size() );

      Mat row_img = tmp_dst; // get a one line image.

      detector.detect( row_img, keypoints);

      extractor.compute( row_img, keypoints, descriptors_1);

      resize( row_img, tmp_dst, tmp_dst.size() );

      Mat train_img = tmp_dst.reshape( 1, 1 );

      train_img.convertTo( training_mat.row(count), CV_32FC1 );
bowTrainer.add(descriptors_1);

      labels.at< float >(count, 0) = (count<nb_cars)?1:-1; // 1 for car, -1 otherwise

      ++count;
  }

I also upload the result of above loop code which is working fine without error in the code , which i get from svm.save(result.txt) , upload in the link given

Svm using Surf Features

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
std::vector<string> all_names;

all_names.assign(files.begin(),files.end());
all_names.insert(all_names.end(), files_no.begin(), files_no.end());
// Load image and add them to the training set
int count = 0;
vector<string>::const_iterator i;
string Dir;
for (i = all_names.begin(); i != all_names.end(); ++i)
{
    Dir=( (count < files.size() ) ? YourImagesDirectory : YourImagesDirectory_2);
    tmp_img = imread( Dir +*i, 0 );
    resize( tmp_img, tmp_dst, tmp_dst.size() );
    Mat row_img = tmp_dst; // 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

image description

Here is the link for my data of cars and not_cars

Link of directories data

Edit

Also tried this loop in the code above , this is working fine , but as i am new to opencv and beginner i don't know whether my approach in this loop is right or not , because my next step is to recognize the object from video

   int dictionarySize = 1500;        
   int retries = 1;
   int flags = KMEANS_PP_CENTERS;
   BOWKMeansTrainer bowTrainer(dictionarySize, tc, retries, flags);
   BOWImgDescriptorExtractor bowDE(extractor, matcher);
    for (i = all_names.begin(); i != all_names.end(); ++i)
    {
      Dir=( (count < files.size() ) ? YourImagesDirectory : YourImagesDirectory_2);

      tmp_img = cv::imread( Dir +*i, 0 );

      resize( tmp_img, tmp_dst, tmp_dst.size() );

      Mat row_img = tmp_dst; // get a one line image.
tmp_dst;

      detector.detect( row_img, keypoints);

      extractor.compute( row_img, keypoints, descriptors_1);

      bowTrainer.add(descriptors_1);

      labels.at< float >(count, 0) = (count<nb_cars)?1:-1; // 1 for car, -1 otherwise

      ++count;
  }

I also upload the result of above loop code which is working fine without error in the code , which i get from svm.save(result.txt) , upload in the link given

Svm using Surf Features

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
std::vector<string> all_names;

all_names.assign(files.begin(),files.end());
all_names.insert(all_names.end(), files_no.begin(), files_no.end());
// Load image and add them to the training set
int count = 0;
vector<string>::const_iterator i;
string Dir;
for (i = all_names.begin(); i != all_names.end(); ++i)
{
    Dir=( (count < files.size() ) ? YourImagesDirectory : YourImagesDirectory_2);
    tmp_img = imread( Dir +*i, 0 );
    resize( tmp_img, tmp_dst, tmp_dst.size() );
    Mat row_img = tmp_dst; // 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;
}

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

image description

Edit

Also tried this loop in the code above , this is working fine , but as i am new to opencv and beginner i don't know whether my approach in this loop is right or not , because my next step is to recognize the object from video

   int dictionarySize = 1500;        
   int retries = 1;
   int flags = KMEANS_PP_CENTERS;
   BOWKMeansTrainer bowTrainer(dictionarySize, tc, retries, flags);
   BOWImgDescriptorExtractor bowDE(extractor, matcher);
    for (i = all_names.begin(); i != all_names.end(); ++i)
    {
      Dir=( (count < files.size() ) ? YourImagesDirectory : YourImagesDirectory_2);

      tmp_img = cv::imread( Dir +*i, 0 );

      resize( tmp_img, tmp_dst, tmp_dst.size() );

      Mat row_img = tmp_dst;

      detector.detect( row_img, keypoints);

      extractor.compute( row_img, keypoints, descriptors_1);

      bowTrainer.add(descriptors_1);

      labels.at< float >(count, 0) = (count<nb_cars)?1:-1; // 1 for car, -1 otherwise

      ++count;
  }