2

I've been working towards using the StereoBM class to generate a disparity map based on two camera input feeds.

I can create a pointed variable StereoBM *sbm; but whenever I call a function, I'm presented with a segmentation fault with a Release build. The Debug build will not run as it aborts due to malloc(): memory corruption.

Disparity_Map::Disparity_Map(int rows, int cols, int type) : inputLeft(), inputRight(), greyLeft(), greyRight(), Disparity() {
    inputLeft.create(rows, cols, type);
    inputRight.create(rows, cols, type);

    greyLeft.create(rows, cols, type);
    greyRight.create(rows, cols, type);
}
void Disparity_Map::computeDisparity(){

    cvtColor(inputLeft, greyLeft, CV_BGR2GRAY);
    cvtColor(inputRight, greyRight, CV_BGR2GRAY);

    StereoBM *sbm;

    // This is where the segfault/memory corruption occurs
    sbm->setNumDisparities(112);
    sbm->setBlockSize(9);
    sbm->setPreFilterCap(61);
    sbm->setPreFilterSize(5);
    sbm->setTextureThreshold(500);
    sbm->setSpeckleWindowSize(0);
    sbm->setSpeckleRange(8);
    sbm->setMinDisparity(0);
    sbm->setUniquenessRatio(0);
    sbm->setDisp12MaxDiff(1);

    sbm->compute(greyLeft, greyRight, Disparity);
    normalize(Disparity, Disparity, 0, 255, CV_MINMAX, CV_8U);
}

I'm not entirely sure what I'm doing wrong with the above. When creating a non-pointer variable, I have this warning for all of the class's methods :

The type 'cv::StereoBM' must implement the inherited pure virtual method 'cv::StereoMatcher::setSpeckleRange'

I've included the header <opencv2/calib3d/calib3d.hpp>, I've ensured the library is linked, and I'm running opencv 3.1.0.

Would anyone be able to shed light on all of the above? As I'm still learning OpenCV and pushing myself through C++.

humroben
  • 87
  • 9
  • Can you provide a small snippet that reproduces the error? Se how to do a [mcve] – Miki Mar 04 '16 at 18:23
  • I've updated the original post with the code I'm trying to use with opencv 3.1.0 when setting sbm up as a pointer, the other error is when I define `StereoBM sbm` nothing else The code compiles, but doesn't run. – humroben Mar 04 '16 at 21:13
  • 1
    You define a pointer to an SMB but you don't actually create one – Martin Beckett Mar 04 '16 at 21:18
  • Could you please elaborate on your comment – humroben Mar 04 '16 at 21:53
  • Change StereoBM *sbm; to StereoBM sbm; ... and all instances of sbm-> to sbm. That way you'll have a StereoBM object allocated on the stack that you can use, rather than a pointer-to-undefined-nowhere that will crash when you try to dereference it. – Jeremy Friesner Mar 05 '16 at 02:19
  • As I've stated, when doing all of that, the definition of sbm, provides me an error similar to this, before compiling, and will fail compilation: `The type 'cv::StereoBM' must implement the inherited pure virtual method 'cv::StereoMatcher::setSpeckleRange` – humroben Mar 05 '16 at 09:11

1 Answers1

2
StereoBM *sbm;

You declare pointer without allocation of object.

cv::Ptr<cv::StereoBM> sbm = cv::StereoBM::create() - it's correct way to create StereoBM object.

Pavel
  • 277
  • 2
  • 13
  • 1
    Thanks, that was what I needed. My code now runs, and I can continue with my project. I would up vote it, but I don't have enough reputation. – humroben Mar 05 '16 at 10:17