Search code examples
c#opencvemgucv

Disparity Map in Emgu.CV


I try to use disparity map calculation in C# using Emgu.CV

I read the images from this article as bitmapLeft and bitmapRight. For reference I used the example code from here

Here is my source code:

bitmapLeft = (Bitmap) mainForm.pictureBoxLeft.Image;
bitmapRight = (Bitmap)mainForm.pictureBoxRight.Image;

Image<Gray, Byte> imageLeft = new Image<Gray, Byte>(bitmapLeft);
Image<Gray, Byte> imageRight = new Image<Gray, Byte>(bitmapRight);
Image<Gray, Byte> imageDisparity = new Image<Gray, Byte>(bitmapLeft.Width, bitmapLeft.Height);

StereoBM stereoBM = new StereoBM(16, 15);
StereoMatcherExtensions.Compute(stereoBM, imageLeft, imageRight, imageDisparity);

Image bitmapDisparity = imageDisparity.ToBitmap();

However, the resulting bitmap is all black.


Solution

  • I think your problem is at the end. The result of calling StereoMatcherExtentions.Comput is a Mat/Image that has a depth of Cv16S, I converted that back to Cv8U and was able to display it. Here is my example. I used the same two images.

        Mat leftImage = new Mat(@"C:\Users\jones_d\Desktop\Disparity\LeftImage.png", ImreadModes.Grayscale);
        Mat rightImage = new Mat(@"C:\Users\jones_d\Desktop\Disparity\\RightImage.png", ImreadModes.Grayscale);
    
        CvInvoke.Imshow("Left", leftImage);
        CvInvoke.Imshow("Right", rightImage);
    
        Mat imageDisparity = new Mat();
        StereoBM stereoBM = new StereoBM(16, 15);
        StereoMatcherExtensions.Compute(stereoBM, leftImage, rightImage, imageDisparity);
    
        Mat show = new Mat();
    
        imageDisparity.ConvertTo(show, DepthType.Cv8U);
        CvInvoke.Imshow("Disparity", show);
    
        CvInvoke.WaitKey(0);
    

    Here are the images:

    Left

    Right

    enter image description here

    Which seems to match the result at: Depth Map from Image

    Doug