RGB到Ycbcr的转换

时间:2013-07-11 23:58:19

标签: 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 (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时,我没有得到ycbcr的相同值,就像我在matlab中得到的那样,我的代码中是否缺少某些东西?

2 个答案:

答案 0 :(得分:1)

更快更准确的代码。 输出值介于0到255之间(JPG公式)

            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);
            }

答案 1 :(得分:0)

在分配之前将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]);

相关问题