StereoBM OpenCV 2.4.9 or 2.4.6 bad allocation in release without debugging

asked 2014-05-20 08:19:53 -0500

I have a client/server application, my server manages the opencv library to do for example disparity mapping, the application works fine with stereoSGBM, but with stereoBM I have random crash with ctrl + f5 release, so launching it without debugging.

The crash is random, with a try/catch sometimes I can get bad allocation memory, failed to allocate 1k bytes. Instead with the call stack I'm not able to catch anything relevant because the crash is not always in the same point, sometimes is in a imgread, sometimes is a malloc, a free a mat.release, so every time is different, but always involves memory in some way.

The code is pretty simple:

void disparity_mapping(std::vector<std::string> & _return, const StereoBmValue& BmValue, const ClientShowSelection& clientShowSelection, const std::string& filenameL, const std::string& filenameR)
{
int ch;
alg = BmValue.algorithmSelection;

if((filenameL == "0" || filenameR == "0"))
_return.push_back("0");
if((filenameL != "0" && filenameR != "0"))
{
  imgL = imread(filenameL , CV_LOAD_IMAGE_GRAYSCALE );
  imgR = imread(filenameR, CV_LOAD_IMAGE_GRAYSCALE );
  _return.push_back("1");
  ch = imgL.channels();
  setAlgValue(BmValue, methodSelection, ch); //Setting the value for StereoBM or SGBM
  disp = calculateDisparity(imgL, imgR, alg); //calculating disparity
  normalize(disp, disp8, 0, 255, CV_MINMAX, CV_8U);
  string matAsStringL(imgL.begin<unsigned char>(), imgL.end<unsigned char>());
  _return.push_back(matAsStringL);

  string matAsStringR(imgR.begin<unsigned char>(), imgR.end<unsigned char>());
  _return.push_back(matAsStringR);

  string matAsStringD(disp8.begin<unsigned char>(), disp8.end<unsigned char>());
  _return.push_back(matAsStringD);
}

the two function that are called:

void setAlgValue(const StereoBmValue BmValue, int methodSelection, int ch)
{   
    if (initDisp)
        initDisparity(methodSelection); //inizializing bm.init(...) and find remap informations from steroRect, etc.

    //Select  0 == StereoSGBM, 1 == StereoBM
    int alg = BmValue.algorithmSelection;

    //storing alg value.
    stereoValue.minDisparity = BmValue.minDisparity;
    stereoValue.disp12MaxDiff = BmValue.disp12MaxDiff;
    stereoValue.SADWindowSize = BmValue.SADWindowSize;
    stereoValue.textureThreshold = BmValue.textureThreshold;
    stereoValue.uniquenessRatio = BmValue.uniquenessRatio;
    stereoValue.numberOfDisparities = BmValue.numberOfDisparities;
    stereoValue.preFilterCap = BmValue.preFilterCap;
    stereoValue.speckleWindowSize = BmValue.speckleWindowSize;
    stereoValue.speckleRange = BmValue.speckleRange;
    stereoValue.preFilterSize = BmValue.preFilterSize;

if (alg == 1) //set of the values in the bm state
{
    bm.state->roi1 = roi1;
    bm.state->roi2 = roi2;
    bm.state->preFilterCap = stereoValue.preFilterCap;
    bm.state->SADWindowSize = stereoValue.SADWindowSize;
    bm.state->minDisparity = stereoValue.minDisparity;
    bm.state->numberOfDisparities = stereoValue.numberOfDisparities;
    bm.state->textureThreshold = stereoValue.textureThreshold;
    bm.state->uniquenessRatio = stereoValue.uniquenessRatio;
    bm.state->speckleWindowSize = stereoValue.speckleWindowSize;
    bm.state->speckleRange = stereoValue.speckleRange;
    bm.state->disp12MaxDiff = stereoValue.disp12MaxDiff;
    bm.state->preFilterSize = stereoValue.preFilterSize;
}


else if(alg == 0) //same for SGBM
{
    sgbm.P1 = 8*ch*sgbm.SADWindowSize*sgbm.SADWindowSize;
    sgbm.P2 = 32*ch*sgbm.SADWindowSize*sgbm.SADWindowSize;
    sgbm.preFilterCap = stereoValue.preFilterCap;
    sgbm.SADWindowSize = stereoValue.SADWindowSize;
    sgbm.minDisparity = stereoValue.minDisparity;
    sgbm.numberOfDisparities = stereoValue.numberOfDisparities;
    sgbm.uniquenessRatio = stereoValue.uniquenessRatio;
    sgbm.speckleWindowSize = stereoValue.speckleWindowSize;
    sgbm.speckleRange = stereoValue.speckleRange;
    sgbm.disp12MaxDiff = stereoValue.disp12MaxDiff;
}
}

and the other one:

Mat calculateDisparity(Mat& imgL, Mat& imgR,  int alg)
{
    Mat disparity;
//remap for rectification

remap(imgL, imgL, map11, map12, INTER_LINEAR,BORDER_CONSTANT, Scalar());
remap(imgR, imgR, map21, map22, INTER_LINEAR,BORDER_CONSTANT, Scalar());

//disp
if (alg == 1)  
 bm( imgL , imgR , disparity);

else if (alg == 0)
 sgbm(imgL, imgR, disparity);

return disparity;

}

So as you can see the code is really simple, but using bm ... (more)

edit retag flag offensive close merge delete