Skip to content

Instantly share code, notes, and snippets.

@wrtcoder
Forked from rexxiang/YuvBytesToBpp24Bitmap.cs
Created September 4, 2018 16:34
Show Gist options
  • Save wrtcoder/cfcc95145e0fc477e375641d71f49189 to your computer and use it in GitHub Desktop.
Save wrtcoder/cfcc95145e0fc477e375641d71f49189 to your computer and use it in GitHub Desktop.
yuv422 to bitmap (using OpenCV.net)
public static Bitmap YuvBytesToBpp24Bitmap(byte[] yuvBytes, int width, int height) {
if (yuvBytes == null || yuvBytes.Length == 0) {
return null;
}
const PixelFormat format = PixelFormat.Format24bppRgb;
var image = YuvBytesToIplImage(yuvBytes, width, height);
var bitmap = new Bitmap(image.Width, image.Height, image.WidthStep, format, image.ImageData);
return bitmap;
}
public static IplImage YuvBytesToIplImage(byte[] yuvBytes, int width, int height) {
var yuv = Mat.FromArray(yuvBytes);
var data = yuv.Data;
var y = new Mat(new Size(width, height), Depth.U8, 1);
var u = new Mat(new Size(width / 2, height / 2), Depth.U8, 1);
var v = new Mat(new Size(width / 2, height / 2), Depth.U8, 1);
y.SetData(data, width);
u.SetData(data + width * height, width / 2);
v.SetData(data + (int)(width * height * 1.25), width / 2);
var uu = new Mat(new Size(width, height), Depth.U8, 1);
var vv = new Mat(new Size(width, height), Depth.U8, 1);
CV.Resize(u, uu);
CV.Resize(v, vv);
var merge = new IplImage(new Size(width, height), IplDepth.U8, 3);
CV.Merge(y, uu, vv, null, merge);
var rgb = new IplImage(new Size(width, height), IplDepth.U8, 3);
CV.CvtColor(merge, rgb, ColorConversion.YCrCb2Rgb);
return rgb;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment