Ask Your Question

Revision history [back]

StereoBM in OpenCV 2.4.3 giving non-deterministic results

I am using StereoBM to compute the stereo disparity map using OpenCV 2.4.3, and I seem to be getting different disparity maps every time I run. Not visually noticeable, but several pixels are different, in random.

I tried saving the disparity map to a YML file, and upon comparing them using WinMerge, I notice that several pixels have a value of -16 where the other run produced a disparity value. This is easily reproducible also.

This snippet shows what I am doing:

//initialize bm
StereoBM bm(StereoBM::BASIC_PRESET, 128, 7);
bm.state->preFilterType = CV_STEREO_BM_XSOBEL;
bm.state->preFilterCap = 63; //31
bm.state->SADWindowSize = 7;
bm.state->minDisparity = 0;
bm.state->numberOfDisparities = 96; //96
bm.state->textureThreshold = 3;
bm.state->uniquenessRatio = 3;
bm.state->speckleWindowSize = 20;
bm.state->speckleRange = 32;
bm.state->disp12MaxDiff = 1;

// Get rectified images
videoOut[MASTER] = Mat(frameHeight, frameWidth, CV_8UC3, static_cast<void *="">(frameBuf[MASTER]));
videoOut[SLAVE] = Mat(frameHeight, frameWidth, CV_8UC3, static_cast<void *="">(frameBuf[SLAVE]));

// grayscale conversion
cvtColor(videoOut[MASTER], frame[MASTER], CV_RGB2GRAY);
cvtColor(videoOut[SLAVE], frame[SLAVE], CV_RGB2GRAY);

// Subsample
resize(frame[MASTER], frame[MASTER], cv::Size(), 0.5, 0.5, cv::INTER_LINEAR);
resize(frame[SLAVE], frame[SLAVE], cv::Size(), 0.5, 0.5, cv::INTER_LINEAR);

// stereo computation
Mat disp = cv::Mat(frame[MASTER].rows, frame[MASTER].cols, CV_16S);
bm(frame[MASTER], frame[SLAVE], disp);

// write to file
FileStorage f1;
f1.open("data.yml", FileStorage::WRITE);
f1 << "disp" << disp;

Is this a bug in OpenCV or am I doing something wrong? I tried writing out the input images also, and they are identical everytime. It is only the disparity map that is changing.