Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Let me show you some of your error's to make your code better for those who answer you better for this error :

Your resize your images to a very small size which is difficult for surf to collect keypoints

const int image_area = 40*30;
// Initialize your training set.
cv::Mat training_mat(num_img,image_area,CV_32FC1);

there is no need for

cv::Mat tmp_dst( 500, 450, CV_8UC1 );

because on one side your making size 40*30 and on the other side you are changing this size in loop , so you don't these steps too in both loops:

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

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

So the final code without removing error become something like this :

// 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
// Initialize your training set.
cv::Mat training_mat(num_img,image_area,CV_32FC1); // error
cv::Mat labels(num_img,1,CV_32FC1); 

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);

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

    detector.detect( row_img, keypoints);

    extractor->compute( row_img, keypoints, descriptors_1);

    bow.add(descriptors_1);

    ++count;
}

int count_2=0;
vector<string>::const_iterator k;
Mat vocabulary = bow.cluster();
dextract.setVocabulary(vocabulary);
for (k = all_names.begin(); k != all_names.end(); ++k)
{
    Dir=( (count_2 < files.size() ) ? YourImagesDirectory : YourImagesDirectory_2);

    Mat row_img = imread( Dir +*k, 0 );

    detector.detect( row_img, keypoints);

    dextract.compute( row_img, keypoints, descriptors_1);

    training_mat.push_back(descriptors_1);

    labels.at< float >(count_2, 0) = (count_2<nb_cars)?1:-1;
    ++count_2;
}