Describe SIFT with OpenCV without repeatedly creating image pyramid

317 Views Asked by At

I'm working with OpenCV 2.4.6 and I need to compute SIFT descriptors at different locations many times on one certain image. The code runs extremely slow. I realize that it's probably because every time I call sift(image, mask, locations, descriptors, true), OpenCV creates an image pyramid. What should I do? PS:The mask option doesn't help.

Part of the code is like this.

Mat image = imread ("test.jpg");
vector<KeyPoint> locations;
Mat descriptors;
for (int i=0 ; i<n ; ++i)
    locations.push_back(KeyPoint(x[i],y[i],sigma[i]));
SIFT sift;
sift (image, noArray(), locations, descriptors);
//do something...
locations.clear();
for (int j=n ; j<2*n; ++j)
    locations.push_back(KeyPoint(x[j],y[j],sigma[j]));
sift (image, noArray(), locations, descriptors);

I think OpenCV create the same image pyramid twice.

1

There are 1 best solutions below

1
On

A fast solution is to call sift(...) just once with all the locations you use:

for (int i=0 ; i<n ; ++i)
  locations.push_back(KeyPoint(x[i],y[i],sigma[i]));
//do something...
const size_t N = locations.size();

for (int j=n ; j<2*n; ++j)
  locations.push_back(KeyPoint(x[j],y[j],sigma[j]));

sift (image, noArray(), locations, descriptors);

// locations[0..N-1] are the first ones, locations[N..size()-1] are the others