//FlannBasedMatcher matcher;
std::vector< DMatch > matches;
double max_dist = 0;
double min_dist = 100;
Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("FlannBased");
Ptr<DescriptorExtractor> extractor = new SurfDescriptorExtractor();
//SurfDescriptorExtractor extractor;
SurfFeatureDetector detector(500);
std::vector<KeyPoint> keypoints;
int dictionarySize = 1500;
TermCriteria tc(CV_TERMCRIT_ITER, 10, 0.001);
int retries = 1;
int flags = KMEANS_PP_CENTERS;
BOWKMeansTrainer bow(dictionarySize, tc, retries, flags);
BOWImgDescriptorExtractor dextract(extractor,matcher);
Mat img_keypoints_1;
Mat descriptors_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
const 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 = 40*30;
// Initialize your training set.
cv::Mat training_mat(num_img,image_area,CV_32FC1);
cv::Mat labels(num_img,1,CV_32FC1);
cv::Mat tmp_dst( 500, 450, CV_8UC1 ); // to the right size for resize
// Set temp matrices
cv::Mat tmp_img;
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 = 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);
bow.add(descriptors_1);
++count;
}
int count_2=0;
Mat vocabulary = bow.cluster();
dextract.setVocabulary(vocabulary);
for (i = all_names.begin(); i != all_names.end(); ++i)
{
Dir=( (count_2 < 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);
dextract.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_2;
}
// 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,cv::Mat(),cv::Mat(),Params);
svm.save("trainsvm.txt");
This is my code , which i try to summarize from the psuedo code from here , my code is running and its not showing me the error , but how do i know that now my data is trained according to my need and now i can implement on my real time app to detect objects from video