StereoBM doesn't give proper output

Viewed 917

I have seen many posts regarding this issue but this one is different. 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.

The below images gives expected output

enter image description here enter image description here enter image description here

The below images gives incorrect output

enter image description here enter image description here enter image description here

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

Here is my 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();
    }
}
2 Answers

Look on both images in gray scale, after normalization - just as your algorithm sees it. At least, I would start from that.

It takes gray image and detects borders. Since all was normalized, probably borders from background are better seen then the one from your real object.

To fix that you can use different algorithm like background subtraction. Hope that helps.

StereoBM computes correlation between two images. In second pair you have pattern visible on the wall - and that can cause problem (better correlation with another pattern element). As you can see, your plant remain black (furthest element, least distances between corresponding points). In digital image correlation the more random distribution of intensities in image the better results are. There is much research done on special random patterns being added to object in order to increase randomness of intensity distribution.

https://orbi.ulg.ac.be/bitstream/2268/15779/2/Quality%20assessment%20of%20speckle%20patterns%20for%20digital%20image%20correlation%20(2006)%20(OCR).pdf

You can fix that problem by adding max distance constraints or modify you image with some distance-based function. As a origin of that function you could use point, which has two correlated points closest each other. Then you could perform second iteration.

Related