Search code examples
emgucvmat

EmguCV - Mat is not empty after creating


I'm working currently with EmguCV and I need empty Mat. But apparently when I create it, the new Mat has sometimes some random values which I do not want.

I'm creating it like that:

Mat mask =new Mat(mainImg.Size,Emgu.CV.CvEnum.DepthType.Cv8U,1);

And when I display the 'mask' it looks like that: enter image description here

It should be completely black but as you can see there is some trash which cause me trouble in reading the mat.

Does anyone know why it is like that? Maybe is there a clever way to clear the Mat?

Thanks in advance!


Solution

  • To create an empty Mat just use the code below.

    Mat img = new Mat();

    If you want to make it a specific size, use the following code. In your question above, you were choosing a depth type of 8U, which might be contributing to your low quality of an image. Here I choose a depth type of 32F, which should increase the quality of the new mask image. I also added 3 channels instead of 1, so you can have full access to the Bgr color space.

    Mat mask = new Mat(500, 500, DepthType.Cv32F, 3);

    Mat objects are great because you don't need to specify the size or depth of the image beforhand. Simmilarly, if you want to use an Image instead, you can use the code below.

    Image<Bgr, byte> img = new Image<Bgr, byte>(500, 500);

    You will need to add some dependencies, but this is the easiest and my preferred way of doing it.