autobuffer too large in calcSIFTDescriptor (sift.cpp) L552 (Bug #3282)
Description
Hi,
I've run in a bug in the SIFT descriptor implementation. The code to recreate the error is at the end of this message. I would gladly edit the code myself and submit the correction if someone can walk me through it.
So the bug happens whenever a keypoint with a size too large is given. If this is the case, a std::bad_alloc nomem exception is raised during the descriptor extraction. This happens in the file sift.cpp, at the calcSIFTDescriptor function, line 552. The size requested for the autobuffer is too large. It correspond to the width of the histogram created to accumulate the sift gradient vectors. I would suggest to clip the radius to prevent the creation of a buffer too large: clip the radius to the diagonal of the image. For example add a line after L545 :
int radius = cvRound(hist_width * 1.4142135623730951f * (d + 1) * 0.5f); //L545
radius = radius>sqrt(img.cols*img.cols + img.rows*img.rows) ? sqrt(img.cols*img.cols + img.rows*img.rows):radius; //new line
This code will create the error. If the error dont happen, you have more memory than me, increase the size of the keypoint.
cv::initModule_nonfree();
vector<KeyPoint> kp;
Mat descriptors;
kp.push_back(KeyPoint(10,10,650,-1)); //650 is the size
Mat img(1280,720,CV_8U);
Ptr<DescriptorExtractor> descriptor = DescriptorExtractor::create("SIFT");
descriptor->compute(img, kp, descriptors);
Associated revisions
Merge pull request #3282 from asmorkalov:android_exclude_ocl
History
Updated by Steven Puttemans over 11 years ago
You can find more information on how to push a change on the how to contribute page following this link: http://code.opencv.org/projects/opencv/wiki/How_to_contribute. There is already a complete guide on how to do it step by step in Windows 7 with TortoiseGIT(http://code.opencv.org/projects/opencv/wiki/Windows_7_and_TortoiseGit_contribution_guide).
Go dive into the matter and get the hang of it! It is rather easy after some trying.
I have made the quick fix for you, see https://github.com/Itseez/opencv/pull/1495! Feel free to follow up on any comments made at the pullrequests and help out where you can if you want to.
Cheerz! And thanks for the solution provided!
- % Done changed from 0 to 70
- Target version set to 2.4.7
- Status changed from New to Open
Updated by Vladislav Vinogradov over 11 years ago
- Pull request set to https://github.com/Itseez/opencv/pull/1495
Updated by Jean-Nicolas Ouellet over 11 years ago
Thanks for the pointers, next time I will be able to make the pull request directly. I see that the fix was approved so I changed the status of this request to done.
- % Done changed from 70 to 100
- Status changed from Open to Done