【转】C# YUV转RGB RGB转BMP
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Drawing;
namespace yuv2rgbcs
{
class Program
{
static double[,] YUV2RGB_CONVERT_MATRIX = new double[3, 3] { { 1, 0, 1.4022 }, { 1, -0.3456, -0.7145 }, { 1, 1.771, 0 } };
static void Main(string[] args)
{
int width = 1280;
int height = 720;
int imgSize = width * height;
int frameSize = imgSize + (imgSize >> 1);
byte[] yuv = new byte[frameSize];
byte[] rgb = new byte[3 * imgSize];
using (FileStream fs = File.OpenRead("D:\\test.cif"))
{
int frame = (int)fs.Length / frameSize;
using (BinaryReader br = new BinaryReader(fs))
{
int index = 0;
while (br.PeekChar() != -1)
{
// 循环读取每一桢
br.Read(yuv, 0, frameSize);
int start = Environment.TickCount;
// 转换为 RGB
Bitmap bm = ConvertYUV2RGB (yuv, rgb, width, height);
int interval = Environment.TickCount - start;
Console.WriteLine("yuv 2 bmp = "+interval);
start = Environment.TickCount;
bm.Save("D:\\yuv2bmp_1.bmp");
// 写 BMP 文件。
//WriteBMP(rgb, width, height, string.Format("D:\\yuv2bmp_{0}.bmp", index++));
interval = Environment.TickCount - start;
Console.WriteLine("rgb to bmp write bmp = " + interval);
}
}
}
}
/// <summary>
/// 将转换后的 RGB 图像数据按照 BMP 格式写入文件。
/// </summary>
/// <param name="rgbFrame">RGB 格式图像数据。</param>
/// <param name="width">图像宽(单位:像素)。</param>
/// <param name="height">图像高(单位:像素)。</param>
/// <param name="bmpFile"> BMP 文件名。</param>
static void WriteBMP(byte[] rgbFrame, int width, int height, string bmpFile)
{
// 写 BMP 图像文件。
int yu = width * 3 % 4;
int bytePerLine = 0;
yu = yu != 0 ? 4 - yu : yu;
bytePerLine = width * 3 + yu;
using (FileStream fs = File.Open(bmpFile, FileMode.Create))
{
using (BinaryWriter bw = new BinaryWriter(fs))
{
bw.Write('B');
bw.Write('M');
bw.Write(bytePerLine * height + 54);
bw.Write(0);
bw.Write(54);
bw.Write(40);
bw.Write(width);
bw.Write(height);
bw.Write((ushort)1);
bw.Write((ushort)24);
bw.Write(0);
bw.Write(bytePerLine * height);
bw.Write(0);
bw.Write(0);
bw.Write(0);
bw.Write(0);
byte[] data = new byte[bytePerLine * height];
int gIndex = width * height;
int bIndex = gIndex * 2;
for (int y = height - 1, j = 0; y >= 0; y--, j++)
{
for (int x = 0, i = 0; x < width; x++)
{
data[y * bytePerLine + i++] = rgbFrame[bIndex + j * width + x]; // B
data[y * bytePerLine + i++] = rgbFrame[gIndex + j * width + x]; // G
data[y * bytePerLine + i++] = rgbFrame[j * width + x]; // R
}
}
bw.Write(data, 0, data.Length);
bw.Flush();
}
}
}
/// <summary>
/// 将一桢 YUV 格式的图像转换为一桢 RGB 格式图像并转换成BMP图片。
/// </summary>
/// <param name="yuvFrame">YUV 格式图像数据。</param>
/// <param name="rgbFrame">RGB 格式图像数据。</param>
/// <param name="width">图像宽(单位:像素)。</param>
/// <param name="height">图像高(单位:像素)。</param>
/// <returns>Bitmap图片</returns>
static Bitmap ConvertYUV2RGB(byte[] yuvFrame, byte[] rgbFrame, int width, int height)
{
int uIndex = width * height;
int vIndex = uIndex + ((width * height) >> 2);
int gIndex = width * height;
int bIndex = gIndex * 2;
int temp = 0;
//图片为pic1,RGB颜色的二进制数据转换得的int r,g,b;
Bitmap bm = new Bitmap(width, height);
int r = 0;
int g = 0;
int b = 0;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
// R分量
temp = (int)(yuvFrame[y * width + x] + (yuvFrame[vIndex + (y / 2) * (width / 2) + x / 2] - 128) * YUV2RGB_CONVERT_MATRIX[0, 2]);
rgbFrame[y * width + x] = (byte)(temp < 0 ? 0 : (temp > 255 ? 255 : temp));
// G分量
temp = (int)(yuvFrame[y * width + x] + (yuvFrame[uIndex + (y / 2) * (width / 2) + x / 2] - 128) * YUV2RGB_CONVERT_MATRIX[1, 1] + (yuvFrame[vIndex + (y / 2) * (width / 2) + x / 2] - 128) * YUV2RGB_CONVERT_MATRIX[1, 2]);
rgbFrame[gIndex + y * width + x] = (byte)(temp < 0 ? 0 : (temp > 255 ? 255 : temp));
// B分量
temp = (int)(yuvFrame[y * width + x] + (yuvFrame[uIndex + (y / 2) * (width / 2) + x / 2] - 128) * YUV2RGB_CONVERT_MATRIX[2, 1]);
rgbFrame[bIndex + y * width + x] = (byte)(temp < 0 ? 0 : (temp > 255 ? 255 : temp));
Color c = Color.FromArgb(rgbFrame[y * width + x], rgbFrame[gIndex + y * width + x], rgbFrame[bIndex + y * width + x]);
bm.SetPixel(x, y, c);
}
}
return bm;
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)