c# convert YUV 4:2:2 to RGB?

2019-07-16 13:32发布

I have a screen grabber which gives me a image in YUV 4:2:2 format.

I need to convert my byte[]'s to RGB format?

Please help, Jason

标签: c# rgb
3条回答
【Aperson】
2楼-- · 2019-07-16 13:34

A solution using integer only calculations (i.e. should be quicker than float/double calculations) is:

    static byte asByte(int value)
    {
        //return (byte)value;
        if (value > 255)
            return 255;
        else if (value < 0)
            return 0;
        else
            return (byte)value;
    }

    static unsafe void PixelYUV2RGB(byte * rgb, byte y, byte u, byte v)
    {
        int C = y - 16;
        int D = u - 128;
        int E = v - 128;

        rgb[2] = asByte((298 * C + 409 * E + 128) >> 8);
        rgb[1] = asByte((298 * C - 100 * D - 208 * E + 128) >> 8);
        rgb[0] = asByte((298 * C + 516 * D + 128) >> 8);
    }

here you use the PixelYUV2RGB function to fill values in a byte array for rgb.

This is much quicker than the above, but is still sub-par in terms of performance for a full HD image in c#

查看更多
混吃等死
3楼-- · 2019-07-16 13:58

You can use this library to convert from RGB, YUV, HSB, HSL and many other color formats, example using the static method at RGB class to convert from YUV, just call

RGB rgb = RBB.YUVtoRBG(y, u, v);

And the underlying implementation of it:

public static RGB YUVtoRGB(double y, double u, double v)
{
    RGB rgb = new RGB();

    rgb.Red = Convert.ToInt32((y + 1.139837398373983740*v)*255);
    rgb.Green = Convert.ToInt32((
        y - 0.3946517043589703515*u - 0.5805986066674976801*v)*255);
    rgb.Blue = Convert.ToInt32((y + 2.032110091743119266*u)*255);

    return rgb;
}
查看更多
相关推荐>>
4楼-- · 2019-07-16 13:59

There is code here to do exactly what you want, but the byte order for the RGB bitmap is inverted (exchange the reds and blues).

查看更多
登录 后发表回答