Hi there,
I have a weird issue concerning keypoint detection with OpenCV. It seems that the detector doesn't release its KeyPoint vector. The application crashes when the KeyPoint vector goes out of scope. When I declare the KeyPoint vector globally, the memory used by the application grows with every processed image (and with every created KeyPoint-vector) and crashes only when returning from main.
It is certainly not the code, I'm just using the basic examples. Few others also reported the same issue, but they didn't provide a solution. (See http://answers.opencv.org/question/18425/siftsurf-descriptors-image-crashes-when-out-of/ or http://answers.opencv.org/question/21469/opencv-crash-after-surf-detection/) To me it seems like an OpenCV bug, but on the other hand this is just a simple example which is working for most people.
I'm using OpenCV 2.4.1, on a Windows 7 machine with cygwin. The precompiled windows libraries (x86/mingw/lib) are used. I also tried OpenCV 2.4.6, no difference.
I'd very much appreciate any hint on what could be wrong here. Thanks!
Here's my code:
#include <opencv2/opencv.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/nonfree/nonfree.hpp>
#include <iostream>
int main(void) {
std::string image_root = "D:/VZE/Workspace/TestProject/";
std::vector<std::string> filenames;
filenames.push_back(image_root + "image.png");
filenames.push_back(image_root + "image2.png");
// declaring the keypoint vector here makes the application crash when returning from main
std::vector<cv::KeyPoint> keypoints;
for (unsigned int index=0; index < filenames.size(); ++index)
{
// read & show image
cv::Mat mat = cv::imread(filenames[index]);
cv::namedWindow(filenames[index], CV_WINDOW_AUTOSIZE);
cv::moveWindow(filenames[index], 0, 0);
cv::imshow(filenames[index], mat);
// declaring the keypoint vector here makes the application crash after the first iteration
// std::vector<cv::KeyPoint> keypoints;
std::cout << "detecting keypoints:" << std::endl;
cv::DenseFeatureDetector detector(1, 4, 1.5, 10); // I also tried the FastFeatureDetector, same here
detector.detect(mat, keypoints);
std::cout << "done. detected " << keypoints.size() << " keypoints" << std::endl;
// show keypoints in output image
cv::Mat output;
cv::drawKeypoints(mat, keypoints, output);
cv::namedWindow("Output", CV_WINDOW_AUTOSIZE);
cv::moveWindow("Output", 640, 0);
cv::imshow("Output", output);
cv::waitKey(0);
cv::destroyAllWindows();
}
std::cout << "Bye!" << std::endl;
return 0;
}
Built with:
g++ -g -Wall -I/cygdrive/D/VZE/Tools/OpenCV/build/include -c -o main.o main.cpp
g++ -o main.exe main.o -L/cygdrive/D/VZE/Tools/OpenCV/build/x86/mingw/lib -lopencv_core241 -lopencv_highgui241 -lopencv_features2d241 -lopencv_nonfree241
Leads to the following output:
detecting keypoints:
done. detected 5302 keypoints
detecting keypoints:
done. detected 5302 keypoints
Bye!
1787 [main] main 13616 open_stackdumpfile: Dumping stack trace to main.exe.stackdump