Mr.G
Mr.G

Reputation: 51

OpenCV Matcher - std::bad_alloc exception

I'm trying to stitch together some images to make a sort of panorama. I'm using OpenCV so first thing to do is detect keypoints and descriptors than matching them. To do that I'm following this tutorial: http://opencv.itseez.com/doc/user_guide/ug_features2d.html But during debug I get a std::bad_alloc exception relative to this line:

matcher.match(descriptors1, descriptors2, matches);

Somebody can help me with that? Because I cutted & pasted the tutorial and there are no compilation errors.

Thanks. G

Complete code:


Mat img1 = imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE);
Mat img2 = imread(argv[2], CV_LOAD_IMAGE_GRAYSCALE);
if(img1.empty() || img2.empty())
{
    printf("Can't read one of the images\n");
    return -1;
}

// detecting keypoints
SurfFeatureDetector detector(400);
vector<KeyPoint> keypoints1, keypoints2;
detector.detect(img1, keypoints1);
detector.detect(img2, keypoints2);

// computing descriptors
SurfDescriptorExtractor extractor;
Mat descriptors1, descriptors2;
extractor.compute(img1, keypoints1, descriptors1);
extractor.compute(img2, keypoints2, descriptors2);

// matching descriptors
BruteForceMatcher<L2<float> > matcher;
vector<DMatch> matches;
matcher.match(descriptors1, descriptors2, matches);

// drawing the results
namedWindow("matches", 1);
Mat img_matches;
drawMatches(img1, keypoints1, img2, keypoints2, matches, img_matches);
imshow("matches", img_matches);
waitKey(0);

Update:

if I run this code, I get a:

Run-Time Check Failure #2 - Stack around the variable 'keypoints1' was corrupted.

Code:

    #include "opencv\cv.h"
    #include "opencv\highgui.h"

    using namespace cv;
    using namespace std;

    int main()
    {
        Mat img1 = imread("Chessboard1.jpg", CV_LOAD_IMAGE_GRAYSCALE);
        Mat img2 = imread("Chessboard3.jpg", CV_LOAD_IMAGE_GRAYSCALE);
        if(img1.empty() || img2.empty())
        {
            printf("Can't read one of the images\n");
            return -1;
        }

        FastFeatureDetector detector(50);
        vector<KeyPoint> keypoints1;
        detector.detect(img1, keypoints1);

        return 0;
    }

Upvotes: 1

Views: 1523

Answers (1)

Said Al-Milli
Said Al-Milli

Reputation: 51

You need ensure that the following "Additional Dependencies" under the the Properties->Linker->Input are referring to the correct OpenCV libraries with debugger support.

i.e.

C:\OpenCV2.2\lib\opencv_calib3d220d.lib
C:\OpenCV2.2\lib\opencv_core220d.lib
C:\OpenCV2.2\lib\opencv_features2d220d.lib
C:\OpenCV2.2\lib\opencv_highgui220d.lib
C:\OpenCV2.2\lib\opencv_imgproc220d.lib

instead of

C:\OpenCV2.2\lib\opencv_calib3d220.lib
C:\OpenCV2.2\lib\opencv_core220.lib
C:\OpenCV2.2\lib\opencv_features2d220.lib
C:\OpenCV2.2\lib\opencv_highgui220.lib
C:\OpenCV2.2\lib\opencv_imgproc220.lib

Upvotes: 2

Related Questions