c# convert YUV 4:2:2 to RGB?
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 f开发者_运维知识库ormat?
Please help, Jason
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;
}
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).
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#.
Note that this is just for YUV to RGB. YUV 422 is stored with 2 Y (lightness) values for every pair of UV (colour) values. So you will call the function with the same u, v values for each pair. The source for this is mentioned in comments below.
精彩评论