Ask Your Question

Revision history [back]

StereoBM truncates right edge of disparity by minDisparities

When using StereoBM, as the minimum disparities values is increased, the right side of the resulting disparity map is truncated by a corresponding pixel count. The actual calculated disparity has been correctly calculated, but getValidDisparityROI has truncated the right side of the disparity map. StereoSGBM does not similarly truncate the right side of the disparity map.

I've observed this behavior on OpenCV master (as of 01-Oct-2017) and the 2.4.8+dfsg1 as packaged with Ubuntu 14.04 on amd64 and armhf.

Example: Input left and right images are the data/aloeL.jpg and data/aloeR.jpg used by the samples/python/stereo_match.py example.

Left image is:

image description

Disparity image at min disparities = 0 (this is expected) has the expected features: a left margin that is minDisparities + numberOfDisparities wide, and top, bottom and right margins of half the block match window width (17 / 2 = 8). Here is the disparity image at min disparities = 0:

image description

The disparity image at min disparities = 32 shows both left and right side margin expanded by min disparities. The expanded right margin is my concern:

image description

Possible solution: If the line from getValidDisparityROI (from modules/calib3d/src/stereosgbm.cpp) which reads:

int xmax = std::min(roi1.x + roi1.width, roi2.x + roi2.width - minD) - SW2;

it is instead calculated thusly:

int xmax = std::min(roi1.x + roi1.width, roi2.x + roi2.width) - SW2;

Then the output disparity map is correctly calculated, without truncation on the right side:

image description

Discussion: I'm suspecting this is a bug that only affects StereoBM. Like StereoBM, StereoSGBM calculates correct disparity data to the right edge disparity image, but StereoSGBM does not truncate the right side.

My application needs the speed of StereoBM, and my cameras are relatively widely spaced for a relatively near target, meaning I'm expecting disparities of 100-250 on an image only ~1000 pixels wide - lopping off an extra minDisparity = 100 pixels on the right side is quite a sacrifice.

Question: Check my reasoning/assertions. Shall I file this as a bug and propose the above getValidDisparityROI modification as a fix? Is there a better or more correct fix?

Appendix: My Calculation/display code is:

#!/usr/bin/env python
# Python 2/3 compatibility
from __future__ import print_function
import sys
import numpy as np
import cv2

print('load and downscale images')
imgL = cv2.pyrDown( cv2.imread('../data/aloeL.jpg') )
imgR = cv2.pyrDown( cv2.imread('../data/aloeR.jpg') )

min_disp = 16
num_disp = 112 - min_disp
if sys.argv[1] > 0:
    min_disp = int(sys.argv[1])
window_size = 17
stereo = cv2.StereoBM_create(numDisparities = num_disp, blockSize = window_size)
stereo.setMinDisparity(min_disp)
stereo.setNumDisparities(num_disp)
stereo.setBlockSize(window_size)
stereo.setDisp12MaxDiff(0)
stereo.setUniquenessRatio(10)
stereo.setSpeckleRange(32)
stereo.setSpeckleWindowSize(100)

print('compute disparity')
grayL = cv2.cvtColor(imgL, cv2.COLOR_BGR2GRAY)
grayR = cv2.cvtColor(imgR, cv2.COLOR_BGR2GRAY)
disp = stereo.compute(grayL, grayR).astype(np.float32) / 16.0
disp_map = (disp - min_disp)/num_disp

print('display')
cv2.imshow('left', imgL)
cv2.imshow('disparity', disp_map)
cv2.waitKey()
cv2.destroyAllWindows()

StereoBM truncates right edge of disparity by minDisparities

When using StereoBM, as the minimum disparities values is increased, the right side of the resulting disparity map is truncated by a corresponding pixel count. The actual calculated disparity has been correctly calculated, but getValidDisparityROI has truncated the right side of the disparity map. StereoSGBM does not similarly truncate the right side of the disparity map.

I've observed this behavior on OpenCV master (as of 01-Oct-2017) and the 2.4.8+dfsg1 as packaged with Ubuntu 14.04 on amd64 and armhf.

Example: Input left and right images are the data/aloeL.jpg and data/aloeR.jpg used by the samples/python/stereo_match.py example.

Left image is:

image description

Disparity image at min disparities = 0 (this is expected) has the expected features: a left margin that is minDisparities + numberOfDisparities wide, and top, bottom and right margins of half the block match window width (17 / 2 = 8). Here is the disparity image at min disparities = 0:

image description

The disparity image at min disparities = 32 shows both left and right side margin expanded by min disparities. The expanded right margin is my concern:

image description

Possible solution: If the line from getValidDisparityROI (from modules/calib3d/src/stereosgbm.cpp) which reads:

int xmax = std::min(roi1.x + roi1.width, roi2.x + roi2.width - minD) - SW2;

it is instead calculated thusly:

int xmax = std::min(roi1.x + roi1.width, roi2.x + roi2.width) - SW2;

Then the output disparity map is correctly calculated, without truncation on the right side:

image description

Discussion: I'm suspecting this is a bug that only affects StereoBM. Like StereoBM, StereoSGBM calculates correct disparity data to the right edge disparity image, but StereoSGBM does not truncate the right side.

My application needs the speed of StereoBM, and my cameras are relatively widely spaced for a relatively near target, meaning I'm expecting disparities of 100-250 on an image only ~1000 pixels wide - lopping off an extra minDisparity = 100 pixels on the right side is quite a sacrifice.

Question: Check my reasoning/assertions. Shall I file this as a bug and propose the above getValidDisparityROI modification as a fix? Is there a better or more correct fix?

Appendix: My Calculation/display code is:

#!/usr/bin/env python
# Python 2/3 compatibility
from __future__ import print_function
import sys
import numpy as np
import cv2

print('load and downscale images')
imgL = cv2.pyrDown( cv2.imread('../data/aloeL.jpg') )
imgR = cv2.pyrDown( cv2.imread('../data/aloeR.jpg') )

min_disp = 16
num_disp = 112 - min_disp
if sys.argv[1] > 0:
    min_disp = int(sys.argv[1])
window_size = 17
stereo = cv2.StereoBM_create(numDisparities = num_disp, blockSize = window_size)
stereo.setMinDisparity(min_disp)
stereo.setNumDisparities(num_disp)
stereo.setBlockSize(window_size)
stereo.setDisp12MaxDiff(0)
stereo.setUniquenessRatio(10)
stereo.setSpeckleRange(32)
stereo.setSpeckleWindowSize(100)

print('compute disparity')
grayL = cv2.cvtColor(imgL, cv2.COLOR_BGR2GRAY)
grayR = cv2.cvtColor(imgR, cv2.COLOR_BGR2GRAY)
disp = stereo.compute(grayL, grayR).astype(np.float32) / 16.0
disp_map = (disp - min_disp)/num_disp

print('display')
cv2.imshow('left', imgL)
cv2.imshow('disparity', disp_map)
cv2.waitKey()
cv2.destroyAllWindows()

StereoBM truncates right edge of disparity by minDisparities

When using StereoBM, as the minimum disparities values is increased, the right side of the resulting disparity map is truncated by a corresponding pixel count. The actual calculated disparity has been correctly calculated, but getValidDisparityROI getValidDisparityROI() has truncated the right side of the disparity map. StereoSGBM does not similarly truncate the right side of the disparity map.

I've observed this behavior on OpenCV master (as of 01-Oct-2017) and the 2.4.8+dfsg1 as packaged with Ubuntu 14.04 on amd64 and armhf.

Example: Input left and right images are the data/aloeL.jpg and data/aloeR.jpg used by the samples/python/stereo_match.py example.

Left image is:

image description

Disparity left input image

The disparity image at min disparities = 0 (this is expected) (below) has all the expected features: a left margin that is minDisparities + numberOfDisparities wide, and top, bottom and right margins of half the block match window width (17 / 2 = 8). Here is the 8).

Disparity Image, min disparities = 0

The disparity image at min disparities = 0:

image description

The disparity image at min disparities = 32 (below) shows both left and right side margin expanded by min disparities. The expanded right margin is my concern:

image descriptionDisparity image, min disparities = 32 (truncated on right margin)

Possible solution: If the line from getValidDisparityROI (from modules/calib3d/src/stereosgbm.cpp) which reads:

int xmax = std::min(roi1.x + roi1.width, roi2.x + roi2.width - minD) - SW2;

it is instead calculated thusly:as:

int xmax = std::min(roi1.x + roi1.width, roi2.x + roi2.width) - SW2;

Then the output disparity map is correctly calculated, without truncation on the right side:

image descriptionDisparity image, min disparities = 32 (not truncated on right)

Discussion: I'm suspecting this is a bug that only affects StereoBM. Like StereoBM, StereoSGBM calculates correct disparity data to the right edge disparity image, but StereoSGBM does not truncate the right side.

My application needs the speed of StereoBM, and my cameras are relatively widely spaced for a relatively near target, meaning I'm expecting disparities of 100-250 on an image only ~1000 pixels wide - lopping off an extra minDisparity = 100 pixels on the right side is quite a sacrifice.

Question: Check my reasoning/assertions. Shall I file this as a bug and propose the above getValidDisparityROI modification as a fix? Is there a better or more correct fix?

Appendix: My Calculation/display code is:

#!/usr/bin/env python
# Python 2/3 compatibility
from __future__ import print_function
import sys
import numpy as np
import cv2

print('load and downscale images')
imgL = cv2.pyrDown( cv2.imread('../data/aloeL.jpg') )
imgR = cv2.pyrDown( cv2.imread('../data/aloeR.jpg') )

min_disp = 16
num_disp = 112 - min_disp
if sys.argv[1] > 0:
    min_disp = int(sys.argv[1])
window_size = 17
stereo = cv2.StereoBM_create(numDisparities = num_disp, blockSize = window_size)
stereo.setMinDisparity(min_disp)
stereo.setNumDisparities(num_disp)
stereo.setBlockSize(window_size)
stereo.setDisp12MaxDiff(0)
stereo.setUniquenessRatio(10)
stereo.setSpeckleRange(32)
stereo.setSpeckleWindowSize(100)

print('compute disparity')
grayL = cv2.cvtColor(imgL, cv2.COLOR_BGR2GRAY)
grayR = cv2.cvtColor(imgR, cv2.COLOR_BGR2GRAY)
disp = stereo.compute(grayL, grayR).astype(np.float32) / 16.0
disp_map = (disp - min_disp)/num_disp

print('display')
cv2.imshow('left', imgL)
cv2.imshow('disparity', disp_map)
cv2.waitKey()
cv2.destroyAllWindows()

StereoBM truncates right edge of disparity by minDisparities

When using StereoBM, as the minimum disparities values is increased, the right side of the resulting disparity map is truncated by a corresponding pixel count. The actual calculated disparity has been correctly calculated, but getValidDisparityROI() has truncated the right side of the disparity map. StereoSGBM does not similarly truncate the right side of the disparity map.

I've observed this behavior on OpenCV master (as of 01-Oct-2017) and the 2.4.8+dfsg1 as packaged with Ubuntu 14.04 on amd64 and armhf.

Example: Input left and right images are the data/aloeL.jpg and data/aloeR.jpg used by the samples/python/stereo_match.py example.

Left image is:

left input image

The disparity image at min disparities = 0 (below) has all the expected features: a left margin that is minDisparities + numberOfDisparities wide, and top, bottom and right margins of half the block match window width (17 / 2 = 8).

Disparity Image, min disparities = 0

The disparity image at min disparities = 32 (below) shows both left and right side margin expanded by min disparities. The expanded right margin is my concern:

Disparity image, min disparities = 32 (truncated on right margin)

Possible solution: If the line from getValidDisparityROI (from modules/calib3d/src/stereosgbm.cpp) which reads:

int xmax = std::min(roi1.x + roi1.width, roi2.x + roi2.width - minD) - SW2;

it is instead calculated as:

int xmax = std::min(roi1.x + roi1.width, roi2.x + roi2.width) - SW2;

Then the output disparity map is correctly calculated, without truncation on the right side:

Disparity image, min disparities = 32 (not truncated on right)

Discussion: I'm suspecting this is a bug that only affects StereoBM. Like StereoBM, StereoSGBM calculates correct disparity data to the right edge disparity image, but StereoSGBM does not truncate the right side.

My application needs the speed of StereoBM, and my cameras are relatively widely spaced for a relatively near target, meaning I'm expecting disparities of 100-250 on an image only ~1000 pixels wide - lopping off an extra minDisparity = 100 pixels on the right side is quite a sacrifice.

Question: Check my reasoning/assertions. Shall I file this as a bug and propose the above getValidDisparityROI modification as a fix? Is there a better or more correct fix?

Appendix: My Calculation/display code is:

#!/usr/bin/env python
# Python 2/3 compatibility
from __future__ import print_function
import sys
import numpy as np
import cv2

print('load and downscale images')
imgL = cv2.pyrDown( cv2.imread('../data/aloeL.jpg') )
imgR = cv2.pyrDown( cv2.imread('../data/aloeR.jpg') )

min_disp = 16
num_disp = 112 - min_disp
if sys.argv[1] > 0:
    min_disp = int(sys.argv[1])
window_size = 17
stereo = cv2.StereoBM_create(numDisparities = num_disp, blockSize = window_size)
stereo.setMinDisparity(min_disp)
stereo.setNumDisparities(num_disp)
stereo.setBlockSize(window_size)
stereo.setDisp12MaxDiff(0)
stereo.setUniquenessRatio(10)
stereo.setSpeckleRange(32)
stereo.setSpeckleWindowSize(100)

print('compute disparity')
grayL = cv2.cvtColor(imgL, cv2.COLOR_BGR2GRAY)
grayR = cv2.cvtColor(imgR, cv2.COLOR_BGR2GRAY)
disp = stereo.compute(grayL, grayR).astype(np.float32) / 16.0
disp_map = (disp - min_disp)/num_disp

print('display')
cv2.imshow('left', imgL)
cv2.imshow('disparity', disp_map)
cv2.waitKey()
cv2.destroyAllWindows()

The following C++ code exhibits the same problem:

#include <opencv2/core.hpp>
#include <opencv2/calib3d.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>

using namespace cv;
using namespace std;

int main(int argc, char *argv[])
{
    Mat in1, in2;
    in1 = imread("../data/aloeL.jpg", IMREAD_COLOR);
    in2 = imread("../data/aloeR.jpg", IMREAD_COLOR);

    Mat img1, img2;
    pyrDown(in1, img1);
    pyrDown(in2, img2);

    int min_disp = 16;
    int num_disp = 112 - min_disp;
    int arg = (argc > 1) ? atoi(argv[1]) : 0;
    if (arg > 0) {
        min_disp = arg;
    }
    int window_size = 17;

    Ptr<StereoBM> stereo = StereoBM::create(num_disp, window_size);
    stereo->setMinDisparity(min_disp);
    stereo->setNumDisparities(num_disp);
    stereo->setBlockSize(window_size);
    stereo->setDisp12MaxDiff(0);
    stereo->setUniquenessRatio(10);
    stereo->setSpeckleRange(32);
    stereo->setSpeckleWindowSize(100);

    Mat g1,g2, disp, disp8;
    cvtColor(img1, g1, CV_BGR2GRAY);
    cvtColor(img2, g2, CV_BGR2GRAY);
    stereo->compute(g1, g2, disp);

    normalize(disp, disp8, 0, 255, CV_MINMAX, CV_8U);

    Mat dispBMscale; 
    disp.convertTo(dispBMscale,CV_32F, 1./16); 

    imshow("image", img1);
    imshow("disparity", disp8);
    waitKey(0);

    return 0;
}

StereoBM truncates right edge of disparity by minDisparities

When using StereoBM, as the minimum disparities values is increased, the right side of the resulting disparity map is truncated by a corresponding pixel count. count.

Excessive left margin on disparity image

The actual calculated disparity has been correctly calculated, but getValidDisparityROI() has truncated the right side of the disparity map. StereoSGBM does not similarly truncate the right side of the disparity map.

I've observed this behavior on OpenCV master (as of 01-Oct-2017) and the 2.4.8+dfsg1 as packaged with Ubuntu 14.04 on amd64 and armhf.

Example: Input left and right images are the data/aloeL.jpg and data/aloeR.jpg used by the samples/python/stereo_match.py example.

Left image is:

left input image

The disparity image at min disparities = 0 (below) has all the expected features: a left margin that is minDisparities + numberOfDisparities wide, and top, bottom and right margins of half the block match window width (17 / 2 = 8).

Disparity Image, min disparities = 0

The disparity image at min disparities = 32 (below) shows both left and right side margin expanded by min disparities. The expanded right margin is my concern:

Disparity image, min disparities = 32 (truncated on right margin)

Possible solution: If the line from getValidDisparityROI (from modules/calib3d/src/stereosgbm.cpp) which reads:

int xmax = std::min(roi1.x + roi1.width, roi2.x + roi2.width - minD) - SW2;

it is instead calculated as:

int xmax = std::min(roi1.x + roi1.width, roi2.x + roi2.width) - SW2;

Then the output disparity map is correctly calculated, without truncation on the right side:

Disparity image, min disparities = 32 (not truncated on right)

Discussion: I'm suspecting this is a bug that only affects StereoBM. Like StereoBM, StereoSGBM calculates correct disparity data to the right edge disparity image, but StereoSGBM does not truncate the right side.

My application needs the speed of StereoBM, and my cameras are relatively widely spaced for a relatively near target, meaning I'm expecting disparities of 100-250 on an image only ~1000 pixels wide - lopping off an extra minDisparity = 100 pixels on the right side is quite a sacrifice.

Question: Check my reasoning/assertions. Shall I file this as a bug and propose the above getValidDisparityROI modification as a fix? Is there a better or more correct fix?

Appendix: My Calculation/display code is:

#!/usr/bin/env python
# Python 2/3 compatibility
from __future__ import print_function
import sys
import numpy as np
import cv2

print('load and downscale images')
imgL = cv2.pyrDown( cv2.imread('../data/aloeL.jpg') )
imgR = cv2.pyrDown( cv2.imread('../data/aloeR.jpg') )

min_disp = 16
num_disp = 112 - min_disp
if sys.argv[1] > 0:
    min_disp = int(sys.argv[1])
window_size = 17
stereo = cv2.StereoBM_create(numDisparities = num_disp, blockSize = window_size)
stereo.setMinDisparity(min_disp)
stereo.setNumDisparities(num_disp)
stereo.setBlockSize(window_size)
stereo.setDisp12MaxDiff(0)
stereo.setUniquenessRatio(10)
stereo.setSpeckleRange(32)
stereo.setSpeckleWindowSize(100)

print('compute disparity')
grayL = cv2.cvtColor(imgL, cv2.COLOR_BGR2GRAY)
grayR = cv2.cvtColor(imgR, cv2.COLOR_BGR2GRAY)
disp = stereo.compute(grayL, grayR).astype(np.float32) / 16.0
disp_map = (disp - min_disp)/num_disp

print('display')
cv2.imshow('left', imgL)
cv2.imshow('disparity', disp_map)
cv2.waitKey()
cv2.destroyAllWindows()

The following C++ code exhibits the same problem:

#include <opencv2/core.hpp>
#include <opencv2/calib3d.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>

using namespace cv;
using namespace std;

int main(int argc, char *argv[])
{
    Mat in1, in2;
    in1 = imread("../data/aloeL.jpg", IMREAD_COLOR);
    in2 = imread("../data/aloeR.jpg", IMREAD_COLOR);

    Mat img1, img2;
    pyrDown(in1, img1);
    pyrDown(in2, img2);

    int min_disp = 16;
    int num_disp = 112 - min_disp;
    int arg = (argc > 1) ? atoi(argv[1]) : 0;
    if (arg > 0) {
        min_disp = arg;
    }
    int window_size = 17;

    Ptr<StereoBM> stereo = StereoBM::create(num_disp, window_size);
    stereo->setMinDisparity(min_disp);
    stereo->setNumDisparities(num_disp);
    stereo->setBlockSize(window_size);
    stereo->setDisp12MaxDiff(0);
    stereo->setUniquenessRatio(10);
    stereo->setSpeckleRange(32);
    stereo->setSpeckleWindowSize(100);

    Mat g1,g2, disp, disp8;
    cvtColor(img1, g1, CV_BGR2GRAY);
    cvtColor(img2, g2, CV_BGR2GRAY);
    stereo->compute(g1, g2, disp);

    normalize(disp, disp8, 0, 255, CV_MINMAX, CV_8U);

    Mat dispBMscale; 
    disp.convertTo(dispBMscale,CV_32F, 1./16); 

    imshow("image", img1);
    imshow("disparity", disp8);
    waitKey(0);

    return 0;
}

StereoBM truncates right edge of disparity by minDisparities

When using StereoBM, as the minimum disparities values is increased, the right side of the resulting disparity map is truncated by a corresponding pixel count.

Excessive left margin on disparity imageExcessive left margin on disparity image

Although the disparity can be correctly calculated in the truncated area, it is not performed. in contrast, StereoSGBM calculates disparity all the way to the right edge of the disparity map.

The actual calculated disparity has been correctly calculated, StereoBM algorithm uses getValidDisparityROI() to determine the valid area of the disparity map over which to calculate disparity, but getValidDisparityROI() has truncated the right side of the disparity map. StereoSGBM does not similarly truncate the right side of the disparity map.its calculation of the right margin is incorrect.

I've observed this behavior on OpenCV master (as of 01-Oct-2017) and the 2.4.8+dfsg1 as packaged with Ubuntu 14.04 on amd64 and armhf.

Example: Input left and right images are the data/aloeL.jpg and data/aloeR.jpg used by the samples/python/stereo_match.py example.

Left image is:

left input image

The disparity image at min disparities = 0 (below) has all the expected features: a left margin that is minDisparities + numberOfDisparities wide, and top, bottom and right margins of half the block match window width (17 / 2 = 8).

Disparity Image, min disparities = 0

The disparity image at min disparities = 32 (below) shows both left and right side margin expanded by min disparities. disparities (right margin of 40 instead of 8). The expanded right margin is my concern:

Disparity image, min disparities = 32 (truncated on right margin)

Possible solution: If the line from getValidDisparityROI (from modules/calib3d/src/stereosgbm.cpp) which reads:

int xmax = std::min(roi1.x + roi1.width, roi2.x + roi2.width - minD) - SW2;

it is instead calculated as:

int xmax = std::min(roi1.x + roi1.width, roi2.x + roi2.width) - SW2;

Then the output disparity map is correctly calculated, without truncation on the right side:

Disparity image, min disparities = 32 (not truncated on right)

Discussion: I'm suspecting this is a bug that only affects StereoBM. Like StereoBM, StereoSGBM calculates correct disparity data to the right edge disparity image, but StereoSGBM does not truncate the right side.

My application needs the speed of StereoBM, and my cameras are relatively widely spaced for a relatively near target, meaning I'm expecting disparities of 100-250 on an image only ~1000 pixels wide - lopping off an extra minDisparity = 100 pixels on the right side is quite a sacrifice.

Question: Check my reasoning/assertions. Shall I file this as a bug and propose the above getValidDisparityROI modification as a fix? Is there a better or more correct fix?

Appendix: My Calculation/display code is:

#!/usr/bin/env python
# Python 2/3 compatibility
from __future__ import print_function
import sys
import numpy as np
import cv2

print('load and downscale images')
imgL = cv2.pyrDown( cv2.imread('../data/aloeL.jpg') )
imgR = cv2.pyrDown( cv2.imread('../data/aloeR.jpg') )

min_disp = 16
num_disp = 112 - min_disp
if sys.argv[1] > 0:
    min_disp = int(sys.argv[1])
num_disp = 128 - min_disp
window_size = 17
stereo = cv2.StereoBM_create(numDisparities = num_disp, blockSize = window_size)
stereo.setMinDisparity(min_disp)
stereo.setNumDisparities(num_disp)
stereo.setBlockSize(window_size)
stereo.setDisp12MaxDiff(0)
stereo.setUniquenessRatio(10)
stereo.setSpeckleRange(32)
stereo.setSpeckleWindowSize(100)

print('compute disparity')
grayL = cv2.cvtColor(imgL, cv2.COLOR_BGR2GRAY)
grayR = cv2.cvtColor(imgR, cv2.COLOR_BGR2GRAY)
disp = stereo.compute(grayL, grayR).astype(np.float32) / 16.0
disp_map = (disp - min_disp)/num_disp

print('display')
cv2.imshow('left', imgL)
cv2.imshow('disparity', disp_map)
cv2.waitKey()
cv2.destroyAllWindows()

The following C++ code exhibits the same problem:

#include <opencv2/core.hpp>
#include <opencv2/calib3d.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>

using namespace cv;
using namespace std;

int main(int argc, char *argv[])
{
    Mat in1, in2;
    in1 = imread("../data/aloeL.jpg", IMREAD_COLOR);
    in2 = imread("../data/aloeR.jpg", IMREAD_COLOR);

    Mat img1, img2;
    pyrDown(in1, img1);
    pyrDown(in2, img2);

    int min_disp = 16;
    int num_disp = 112 - min_disp;
    int arg = (argc > 1) ? atoi(argv[1]) : 0;
    if (arg > 0) {
        min_disp = arg;
    }
    int num_disp = 128 - min_disp;
    int window_size = 17;

    Ptr<StereoBM> stereo = StereoBM::create(num_disp, window_size);
    stereo->setMinDisparity(min_disp);
    stereo->setNumDisparities(num_disp);
    stereo->setBlockSize(window_size);
    stereo->setDisp12MaxDiff(0);
    stereo->setUniquenessRatio(10);
    stereo->setSpeckleRange(32);
    stereo->setSpeckleWindowSize(100);

    Mat g1,g2, disp, disp8;
    cvtColor(img1, g1, CV_BGR2GRAY);
    cvtColor(img2, g2, CV_BGR2GRAY);
    stereo->compute(g1, g2, disp);

    normalize(disp, disp8, 0, 255, CV_MINMAX, CV_8U);

    Mat dispBMscale; 
    disp.convertTo(dispBMscale,CV_32F, 1./16); 

    imshow("image", img1);
    imshow("disparity", disp8);
    waitKey(0);

    return 0;
}