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

江枫思渺然 提交于 2020-01-04 14:02:22

问题


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


回答1:


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



回答2:


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).




回答3:


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#



来源:https://stackoverflow.com/questions/6959275/c-sharp-convert-yuv-422-to-rgb

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!