简体   繁体   English

EmguCV 中的 image.Mat 始终为 BGR 格式?

[英]image.Mat in EmguCV is always in BGR format?

My code is all done thinking that the input Mat is in BGR format, so I am wondering if given an Image object in EmguCV, the property Mat from this object is always a BGR Mat .我的代码都认为输入Mat是 BGR 格式,所以我想知道如果在 EmguCV 中给定一个Image object,这个 object 中的属性Mat总是一个 BGR Mat Otherwise, I would need to use CvtColor to get the BGR representation.否则,我需要使用CvtColor来获取 BGR 表示。

Example code:示例代码:

byte[] data = GetPngPixelsArray(string); // Byte array in RGB format

Image<Rgb, byte> image = new Image<Rgb, byte>(width, height)
{
  Bytes = data
};

CvInvoke.Imshow("image mat", image.Mat);
CvInvoke.WaitKey(0);

The function I am using to get the byte array data:我用来获取字节数组数据的 function:

internal static byte[] GetPngPixelsArray(string filename)
{
  byte[] rgbValues = null;

  using (var imageIn = Image.FromFile(filename))
  using (var bmp = new Bitmap(imageIn))
  {
    BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height),
    ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

    IntPtr ptr = bmpData.Scan0;

    int bytes = Math.Abs(bmpData.Stride) * bmp.Height;
    rgbValues = new byte[bytes];

    Marshal.Copy(ptr, rgbValues, 0, bytes);

    bmp.UnlockBits(bmpData);
  }

  return rgbValues;
}

In the example code above, the Imshow function is showing the image properly, and as far as I know Imshow always made a representation of the image using the BGR format.在上面的示例代码中, Imshow function 正确显示图像,据我所知, Imshow总是使用 BGR 格式表示图像。

So in fact the image.Mat is in BGR format, but I've checked the EmguCV documentation and haven't found any declaration that this is as I stated.所以实际上image.Mat是 BGR 格式,但我已经检查了 EmguCV 文档,但没有找到任何声明表明这是我所说的。

Just checked the behaviour of EmguCV creating an Image object, and the .Mat property has the same representation as the Image object, meaning that could be BGR or RGB.刚刚检查了 EmguCV 创建Image object 的行为, .Mat属性与Image object 具有相同的表示,这意味着可以是 BGR 或 RGB。

I filled two vectors of size 750000 (500x500x3), this is what I've done:我填充了两个大小为 750000 (500x500x3) 的向量,这就是我所做的:

byte[] rgbChar = new byte[750000];
byte[] bgrChar = new byte[750000];

for (int i = 0; i < rgbChar.Length; i+=3)
{
    rgbChar[i] = 255;
    rgbChar[i+1] = 0;
    rgbChar[i+2] = 0;

    bgrChar[i] = 0;
    bgrChar[i+1] = 0;
    bgrChar[i+2] = 255;
}

Image<Rgb, byte> imageRgb = new Image<Rgb, byte>(500, 500)
{
    Bytes = rgbChar
};
Image<Bgr, byte> imageBgr = new Image<Bgr, byte>(500, 500)
{
    Bytes = bgrChar
};

CvInvoke.Imshow("RGB Image", imageRgb);
CvInvoke.Imshow("RGB Image mat", imageRgb.Mat);
CvInvoke.Imshow("BGR Image", imageBgr);
CvInvoke.Imshow("BGR Image mat", imageBgr.Mat);
CvInvoke.WaitKey(0);

在此处输入图像描述

Edit: I would add some explanations, as it seems that the results are not self explaining.编辑:我会添加一些解释,因为结果似乎无法自我解释。

I thought that the EmguCV Image class, ever was creating the Mat property as BGR.我认为 EmguCV Image class曾经Mat属性创建为 BGR。 What means that?那是什么意思?

If you check the Image creation, you need to specify the format, eg.如果您检查Image创建,您需要指定格式,例如。 Image<Rgb, byte> . Image<Rgb, byte> I thought, that as you have to specify the format of the Image , that the .Mat property was a BGR representation of the RGB Image .我认为,由于您必须指定Image的格式,所以.Mat属性是 RGB Image的 BGR 表示。

Basically, I thought that the behaviour was like that:基本上,我认为行为是这样的:

  • Image<Rgb, byte> : -> Internal conversion from RGB to BGR into the .Mat property (Wrong). Image<Rgb, byte> : -> 从 RGB 到 BGR 的内部转换到.Mat属性(错误)。
  • Image<Bgr, byte> : -> No conversion, the .Mat is BGR same as Image . Image<Bgr, byte> : -> 没有转换, .Mat是 BGR 与Image相同。

But the real behaviour, is that the .Mat property is just a Mat representation of the Image , doesn't matter if the Image is in RGB or BGR format, the .Mat property will be in the same representation.但真正的行为是.Mat属性只是ImageMat表示形式,无论Image是 RGB 还是 BGR 格式, .Mat属性都将采用相同的表示形式。

This is the reason that in the two first cases the image shows blue ( Imshow ever represents the image as BGR, if the image is in RGB it will show a blue image).这就是在前两种情况下图像显示蓝色的原因( Imshow曾经将图像表示为 BGR,如果图像是 RGB,它将显示蓝色图像)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM