Search code examples
c#opencvmatdepth

Wrong depth of Mat (OpenCvSharp)


I am trying to create a disparity image from two stereo images using OpenCvSharp.

This is my code so far:

      Mat left = new Mat(fileName1, ImreadModes.IgnoreOrientation);
      Mat right = new Mat(fileName2, ImreadModes.IgnoreOrientation);
      Mat output = new Mat();

      right = right.Resize(left.Size());

      MessageBox.Show($"Type left: {left.Type()}, type right: {right.Type()}");

      StereoBM bm = StereoBM.Create(16, 15);
      OutputArray outArr = OutputArray.Create(output);
      bm.Compute((InputArray) left, (InputArray) right, outArr);
      Cv2.ImShow("output", outArr.GetMat());

I am showing the message box with the mat types, because I used to get this error:

"Both input images must have CV_8UC1"

I changed the ImreadMode until it worked, and IgnoreOrientation works for some reason.

Now I get this error:

">Unsupported depth of input image:
     'VDepth::contains(depth)'
where
     'depth' is 3 (CV_16S)
"

In the last line.

So which depth would be accepted? And how do I set the depth of the output accordingly?


(I have about 4 hours of experience with OpenCv, sorry if this question is trivial, I didn't find anything about this on google)


Solution

  • I got it.

    StereoBM bm = StereoBM.Create(16, 13);
    OutputArray outArr = OutputArray.Create(output);
    bm.Compute((InputArray) left, (InputArray) right, outArr);
    outArr.GetMat().ConvertTo(output, MatType.CV_8UC1);   //   <--- key line
    Cv2.ImShow("output", output);