Ask Your Question

Revision history [back]

StereoBM doesn't give proper output

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);
    ImageProcessor.showResult(disparity);
    mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}

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;
    try
    {
        InputStream in = new ByteArrayInputStream(byteArray);
        buffImage = ImageIO.read(in);
        JFrame frame = new JFrame();
        frame.getContentPane().add(new JLabel(new ImageIcon(buffImage)));
        frame.pack();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setVisible(true);
    } catch (IOException e)
    {
        e.printStackTrace();
    }
}

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