I am using sift to detect keypoints of two images of 3264x2466. Here follows my code. However, I got an error saying that opencv error: insufficient memory
. Is there anything wrong?
Here is the image http://img42.imageshack.us/img42/6963/v839.jpg and I am running the program on win7x86
, opencv 2.4.7
#include <opencv\cv.h>
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <string>
#include <vector>
#include <cmath>
#include <opencv2/nonfree/features2d.hpp>
#include <opencv2/nonfree//nonfree.hpp>
using namespace std;
using namespace cv;
int main(int argc, char **argv)
{
cv::initModule_nonfree();
Mat image1 = imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE );
Mat image2 = imread(argv[2], CV_LOAD_IMAGE_GRAYSCALE );
//1. compute the keypoints
int minHessian = 400;
Ptr<FeatureDetector> detector = FeatureDetector::create("SIFT");
vector<KeyPoint> keypoints1,keypoints2;
detector->detect(image1, keypoints1);
detector->detect(image2, keypoints2);
//2. compute the descriptor
Ptr<DescriptorExtractor> extractor = DescriptorExtractor::create("SIFT");
Mat descriptors1, descriptors2;
extractor->compute( image1, keypoints1, descriptors1);
extractor->compute( image2, keypoints2, descriptors2);
//3. match
Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("FlannBased");
std::vector< DMatch > matches;
matcher->match( descriptors1, descriptors2, matches );
double max_dist = 0; double min_dist = 100;
//-- Quick calculation of max and min distances between keypoints
for( int i = 0; i < descriptors1.rows; i++ )
{
double dist = matches[i].distance;
if( dist < min_dist ) min_dist = dist;
if( dist > max_dist ) max_dist = dist;
}
//-- Draw only "good" matches (i.e. whose distance is less than 2*min_dist )
//-- PS.- radiusMatch can also be used here.
std::vector< DMatch > good_matches;
for( int i = 0; i < descriptors1.rows; i++ )
{
if( matches[i].distance <= 2*min_dist ) {
good_matches.push_back( matches[i]);
}
}
//-- Draw only "good" matches
Mat img_matches;
drawMatches( image1, keypoints1, image2, keypoints2,
good_matches, img_matches, Scalar::all(-1), Scalar::all(-1),
vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );
//-- Show detected matches
imwrite("C:\\Users\\flex\\Desktop\\output2.jpg", img_matches);
//imshow( "Good Matches", img_matches );
//waitKey(0);
return 0;
}