C# RGB到Ycbcr的转换

C# RGB到Ycbcr的转换,c#,matlab,image-processing,rgb,C#,Matlab,Image Processing,Rgb,我正在做图像处理,我有一个带有图像rgb值的3D数组,我正在尝试将这些值转换成ycbcr(我制作了一个rgb数组的副本,称之为ycbcr,并且 public static void rgb2ycbcr(System.Drawing.Bitmap bmp, ref byte[, ,] arrayrgb, ref byte[, ,] arrayycbcr) { byte Y; byte Cb; byte Cr; for (i

我正在做图像处理,我有一个带有图像rgb值的3D数组,我正在尝试将这些值转换成ycbcr(我制作了一个rgb数组的副本,称之为ycbcr,并且

public static void rgb2ycbcr(System.Drawing.Bitmap bmp, ref byte[, ,] arrayrgb, ref byte[, ,] arrayycbcr)
    {

        byte Y;
        byte Cb;
        byte Cr;

        for (int i = 1; i < (bmp.Height + 1); i++)   //don't worry about bmp.height/width+2 its for my project
        {
            for (int j = 1; j < (bmp.Width + 1); j++)
            {
                byte R = arrayrgb[i, j, 0];
                byte G = arrayrgb[i, j, 1];
                byte B = arrayrgb[i, j, 2];

                Y = (byte)((0.257 * R) + (0.504 * G) + (0.098 * B) + 16);
                Cb = (byte)(-(0.148 * R) - (0.291 * G) + (0.439 * B) + 128);
                Cr = (byte)((0.439 * R) - (0.368 * G) - (0.071 * B) + 128);

                arrayycbcr[i, j, 0] = Y;
                arrayycbcr[i, j, 1] = Cb;
                arrayycbcr[i, j, 2] = Cr;

            }
        }

    }
公共静态无效rgb2ycbcr(System.Drawing.Bitmap bmp,参考字节[,]arrayrgb,参考字节[,]arrayycbcr)
{
字节Y;
字节Cb;
字节Cr;
对于(inti=1;i<(bmp.Height+1);i++)//不要担心bmp.Height/width+2对于我的项目来说是什么
{
对于(int j=1;j<(bmp.Width+1);j++)
{
字节R=arrayrgb[i,j,0];
字节G=arrayrgb[i,j,1];
字节B=arrayrgb[i,j,2];
Y=(字节)((0.257*R)+(0.504*G)+(0.098*B)+16);
Cb=(字节)((0.148*R)-(0.291*G)+(0.439*B)+128);
Cr=(字节)((0.439*R)-(0.368*G)-(0.071*B)+128);
arrayycbcr[i,j,0]=Y;
arrayycbcr[i,j,1]=Cb;
arrayycbcr[i,j,2]=Cr;
}
}
}

问题是,当我使用rgb2ycbcr时,我得到的ycbcr值与在matlab中得到的值不一样,我的代码中是否缺少某些内容?

在赋值之前将R/G/B转换为uint

uint R=ConvertToUint(arrayrgb[i,j,0]);

uint G=ConvertToUint(arrayrgb[i,j,1]);

uint B=ConvertToUint(arrayrgb[i,j,2]);
更快更准确的代码。 输出值​​介于0和255之间(JPG公式)

width=bmp.width;
高度=bmp.高度;
yData=新字节[宽度,高度];//luma
bData=新字节[宽度,高度];//Cb
rData=新字节[宽度,高度];//Cr
不安全的
{
BitmapData BitmapData=bmp.LockBits(新矩形(0,0,宽度,高度),ImageLockMode.ReadWrite,bmp.PixelFormat);
int heightInPixels=bitmapData.Height;
int widthInBytes=宽度*3;
字节*ptrFirstPixel=(字节*)bitmapData.Scan0;
//转换为YCbCr
对于(int y=0;y
“没有获得与我在matlab中获得的相同的ycbcr值”示例如何?是否尝试在命令窗口中键入
编辑rgb2ycbcr
,以查看matlab的实现功能?是的,一个具有1x1图像(x3通道)的示例这会给出不同的结果,这在这里非常有用。你可能永远不会像在Matlab中那样得到精确的值,因为你使用的是浮点计算。如果值非常接近,那没关系。可能还需要一些饱和逻辑。
            width = bmp.Width;
            height = bmp.Height;
            yData = new byte[width, height];                     //luma
            bData = new byte[width, height];                     //Cb
            rData = new byte[width, height];                     //Cr

            unsafe
            {
                BitmapData bitmapData = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, bmp.PixelFormat);
                int heightInPixels = bitmapData.Height;
                int widthInBytes = width * 3;
                byte* ptrFirstPixel = (byte*)bitmapData.Scan0;

                //Convert to YCbCr
                for (int y = 0; y < heightInPixels; y++)
                {
                    byte* currentLine = ptrFirstPixel + (y * bitmapData.Stride);
                    for (int x = 0; x < width; x++)
                    {
                        int xPor3 = x * 3;
                        float blue = currentLine[xPor3++];
                        float green = currentLine[xPor3++];
                        float red = currentLine[xPor3];

                        yData[x, y] = (byte)((0.299 * red) + (0.587 * green) + (0.114 * blue));
                        bData[x, y] = (byte)(128 - (0.168736 * red) + (0.331264 * green) + (0.5 * blue));
                        rData[x, y] = (byte)(128 + (0.5 * red) + (0.418688 * green) + (0.081312 * blue));
                    }
                }
                bmp.UnlockBits(bitmapData);
            }