Ask Your Question

StereoBM doesn't give proper output

asked 2017-09-27 01:39:42 -0500

Vijay Nirmal gravatar image

I want to find depth from stereo image. My code is working for one pair of an image but not working for another pair of an image.

Later, I want to convert video inputs from two cameras into a stereo output. I have tried that but that also have the same issue as the images (second pair of images) shown below.

I have tried it in with Java and Python and faced the same issue in both the languages.

Here is my java code

public static void main(String[] args)
    Mat left = Imgcodecs.imread("path", Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);
    Mat right = Imgcodecs.imread("path", Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);

    Core.normalize(left, left, 0, 255, NORM_MINMAX, CvType.CV_8U);
    Core.normalize(right, right, 0, 255, NORM_MINMAX, CvType.CV_8U);

    StereoBM bm = StereoBM.create(16, 15);

    Mat disparity = new Mat();
    bm.compute(left, right, disparity);

public static void showResult(Mat img)
    Imgproc.resize(img, img, new Size(640, 480));
    MatOfByte matOfByte = new MatOfByte();
    Imgcodecs.imencode(".jpg", img, matOfByte);
    byte[] byteArray = matOfByte.toArray();
    BufferedImage buffImage = null;
        InputStream in = new ByteArrayInputStream(byteArray);
        buffImage =;
        JFrame frame = new JFrame();
        frame.getContentPane().add(new JLabel(new ImageIcon(buffImage)));
    } catch (IOException e)

How can I correct my code so that it will work for all images?

The below images gives expected output

image description image description image description

The below images gives incorrect output

image description image description image description

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2017-09-29 13:03:36 -0500

The default aloeL.jpg and aloeR.jpg images are for a close up object with large disparities. I have a hunch that at a number of disparities set to 16 you are not able to resolve the full range of disparities present in the image. Try setting the number of disparities to a larger number:

StereoBM bm = StereoBM.create(128, 15);
edit flag offensive delete link more

Question Tools



Asked: 2017-09-27 01:39:42 -0500

Seen: 563 times

Last updated: Sep 29 '17