Heap Corruption using cv::FlannBasedMatcher and std::vector

248 views Asked by At

I am developing a breast imaging features for object recognition, using FlannBasedMatcher to compute spatial histograms.

Mat ComputeSpatialHistogram(Mat features, Mat vocabulary, int* region_index, int level, Ptr<DescriptorMatcher> flann_matcher)
{
   int vocab_size = vocabulary.rows;
   Mat descriptor = Mat::zeros(1, vocab_size*my_pow(4, level), CV_32FC1);
   if (features.rows > 0)
   {
        vector<DMatch> matches;
        flann_matcher->match(features, matches);
        int word_idx, region_idx, descr_idx;
        for (int i = 0; i < matches.size(); i++){
            word_idx = matches[i].trainIdx;
            region_idx = region_index[i];
            descr_idx = vocab_size*region_idx + word_idx;
            descriptor.at<float>(0, descr_idx) = descriptor.at<float>(0, descr_idx) + 1.0f;
        }
    }
    return descriptor;
}

I get an error when ending the execution of the if(features.rows > 0) scope. Can you helpme?

1

There are 1 answers

0
fbr On BEST ANSWER

Try putting

matches.reserve(size) 

with the actually size of the vector, before insert any element. This is necessary if you're using the OpenCV 2.2, but not with the 2.9