I'm got a "free(): invalid pointer" error when try using detectAndCompute

asked 2019-03-27 13:26:56 -0600

I'm trying to detect some key points and match them on two images, but I'm getting this error message:

image description

Here is the code:

#include <iostream>
#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/features2d.hpp"
#include "opencv2/xfeatures2d.hpp"
#include "opencv2/xfeatures2d/nonfree.hpp"

using namespace cv;
using namespace cv::xfeatures2d;
using std::cout;
using std::endl;

int main( int argc, char* argv[] )
{
Mat img1 = imread( "11122018_150.JPG", IMREAD_GRAYSCALE );
Mat img2 = imread( "11122018_200.JPG", IMREAD_GRAYSCALE );
if ( img1.empty() || img2.empty() )
{
    cout << "Could not open or find the image!\n" << endl;
    return -1;
}else{
    cout<<"SUCESS"<<endl;
}
//-- Step 1: Detect the keypoints using SURF Detector, compute the descriptors
int minHessian = 400;
Ptr<SURF> detector = cv::xfeatures2d::SURF::create(minHessian);
cout<<"ESTOU AQUI"<<endl;
std::vector<KeyPoint> keypoints1, keypoints2;
Mat descriptors1, descriptors2;
cout<<"ESTOU AQUI"<<endl;
detector->detectAndCompute( img1, noArray(), keypoints1, descriptors1 );
detector->detectAndCompute( img2, noArray(), keypoints2, descriptors2 );
cout<<"ESTOU AQUI!"<<endl;

//-- Step 2: Matching descriptor vectors with a FLANN based matcher
// Since SURF is a floating-point descriptor NORM_L2 is used
Ptr<DescriptorMatcher> matcher = new FlannBasedMatcher;
//DescriptorMatcher::create(DescriptorMatcher::FLANNBASED);
std::vector< std::vector<DMatch> > knn_matches;
matcher->knnMatch( descriptors1, descriptors2, knn_matches, 2 );
//-- Filter matches using the Lowe's ratio test
const float ratio_thresh = 0.7f;
std::vector<DMatch> good_matches;
for (size_t i = 0; i < knn_matches.size(); i++)
{
    if (knn_matches[i][0].distance < ratio_thresh * knn_matches[i][1].distance)
    {
        good_matches.push_back(knn_matches[i][0]);
    }
}

//-- Draw matches
Mat img_matches;
drawMatches( img1, keypoints1, img2, keypoints2, good_matches, img_matches, Scalar::all(-1),
             Scalar::all(-1), std::vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );
//-- Show detected matches
imshow("Good Matches", img_matches );
waitKey(0);
return 0;
}

My OpenCV version is 4.0.1 and I've identified that this error come from detectAndCompute function, but I need help to correct it, can someone help me?

edit retag flag offensive close merge delete

Comments

DEBUG it !

why do you think, it's coming from opencv ?

berak gravatar imageberak ( 2019-03-28 01:04:31 -0600 )edit

try to get a backtrace:

gdb myprog
run
# ... let it crash
bt
berak gravatar imageberak ( 2019-03-28 02:49:47 -0600 )edit