/// <summary>
/// 将一桢 YUV 格式的图像转换为一桢 RGB 格式图像。
/// </summary>
/// <param name="yuvFrame">YUV 格式图像数据。</param>
/// <param name="rgbFrame">RGB 格式图像数据。</param>
/// <param name="width">图像宽(单位:像素)。</param>
/// <param name="height">图像高(单位:像素)。</param>
static void ConvertYUV2RGB(byte[] yuvFrame, byte[] rgbFrame, int width, int height)
{
int uIndex = width * height;
int vIndex = uIndex + ((width * height) >> );
int gIndex = width * height;
int bIndex = gIndex * ; int temp = ; for (int y = ; y < height; y++)
{
for (int x = ; x < width; x++)
{
// R分量
temp = (int)(yuvFrame[y * width + x] + (yuvFrame[vIndex + (y / ) * (width / ) + x / ] - ) * YUV2RGB_CONVERT_MATRIX[, ]);
rgbFrame[y * width + x] = (byte)(temp < ? : (temp > ? : temp)); // G分量
temp = (int)(yuvFrame[y * width + x] + (yuvFrame[uIndex + (y / ) * (width / ) + x / ] - ) * YUV2RGB_CONVERT_MATRIX[, ] + (yuvFrame[vIndex + (y / ) * (width / ) + x / ] - ) * YUV2RGB_CONVERT_MATRIX[, ]);
rgbFrame[gIndex + y * width + x] = (byte)(temp < ? : (temp > ? : temp)); // B分量
temp = (int)(yuvFrame[y * width + x] + (yuvFrame[uIndex + (y / ) * (width / ) + x / ] - ) * YUV2RGB_CONVERT_MATRIX[, ]);
rgbFrame[bIndex + y * width + x] = (byte)(temp < ? : (temp > ? : temp));
}
}
} void WriteBMP(byte[] rgbFrame, int width, int height)
{
// 写 BMP 图像文件。
int yu = width * % ;
int bytePerLine = ;
yu = yu != ? - yu : yu;
bytePerLine = width * + yu; int length = rgbFrame.GetLength(); //将文件头以及数据写到内存流里面
using (MemoryStream stream = new MemoryStream(length + ))
{
using (BinaryWriter bw = new BinaryWriter(stream, Encoding.Default))
{
bw.Write('B');
bw.Write('M');
bw.Write(bytePerLine * height + );
bw.Write();
bw.Write();
bw.Write();
bw.Write(width);
bw.Write(height);
bw.Write((ushort));
bw.Write((ushort));
bw.Write();
bw.Write(bytePerLine * height);
bw.Write();
bw.Write();
bw.Write();
bw.Write(); byte[] data = new byte[bytePerLine * height];
int gIndex = width * height;
int bIndex = gIndex * ;
for (int y = height - , j = ; y >= ; y--, j++)
{
for (int x = , i = ; 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, , data.Length);
Bitmap image = new Bitmap(stream);
this.pictureBox2.Image = image;
stream.Flush();
stream.Close();
bw.Flush();
bw.Close(); }
}
}

yuv420转rgb 及 rgb转bmp保存的更多相关文章

  1. Bayer RGB和RGB Raw

    Bayer RGB和RGB Raw 对于SENSOR来说,Bayer RGB和RGB Raw两者的图象结构都是BG/GR的(Bayer pattern说的是COLOR FILTER的结构, 分为两种: ...

  2. RGB 与 (RGB转 YCbCr再转为 RGB)的图像

           RGB 与 (RGB转 YCbCr再转为 RGB)的图像   不可逆,能够从 矩阵的逆运算看出来. 附上 matlab 代码:         clc,clear; Source=imr ...

  3. Android camera2 回调imagereader 从Image拿到YUV数据转化成RGB,生成bitmap并保存

    ImageUtil.java import android.graphics.ImageFormat; import android.media.Image; import android.os.Bu ...

  4. 【学习ffmpeg】打开视频文件,帧分析,并bmp保存关键帧

    http://www.tuicool.com/articles/jiUzua   http://blog.csdn.net/code_future/article/details/8646717 主题 ...

  5. YCbCr to RGB and RGB toYCbCr

    RGB => YCbCr: Y = 0.299R + 0.587G + 0.114BCb = -0.1726R - 0.3388G + 0.5114B + 128Cr = 0.5114R - 0 ...

  6. 视音频数据处理入门:RGB、YUV像素数据处理

    ===================================================== 视音频数据处理入门系列文章: 视音频数据处理入门:RGB.YUV像素数据处理 视音频数据处理 ...

  7. bgr to rgb

    因为在研究车牌识别算法(plr),遇到了算法 处理的格式问题,可分三个常用格式: 0:rgb 1:bgr 2:yuv422——需要注意的是,这里为啥选yuv422做识别,当然还可选yuv444,最坏打 ...

  8. [转载] 视音频数据处理入门:RGB、YUV像素数据处理

    ===================================================== 视音频数据处理入门系列文章: 视音频数据处理入门:RGB.YUV像素数据处理 视音频数据处理 ...

  9. 视音频数据处理入门:RGB、YUV像素数据处理【转】

    转自:http://blog.csdn.net/leixiaohua1020/article/details/50534150 ==================================== ...

随机推荐

  1. Mysql错误:Ignoring query to other database解决方法

    Mysql错误:Ignoring query to other database解决方法 今天登陆mysql show databases出现Ignoring query to other datab ...

  2. BZOJ 3112: [Zjoi2013]防守战线 [单纯形法]

    题目描述 战线可以看作一个长度为n 的序列,现在需要在这个序列上建塔来防守敌兵,在序列第i 号位置上建一座塔有Ci 的花费,且一个位置可以建任意多的塔,费用累加计算.有m 个区间[L1, R1], [ ...

  3. COGS743. [网络流24题] 最长k可重区间集

    743. [网络流24题] 最长k可重区间集 ★★★   输入文件:interv.in   输出文件:interv.out   简单对比时间限制:1 s   内存限制:128 MB «问题描述: «编 ...

  4. UVA11324 The Largest Clique[强连通分量 缩点 DP]

    UVA - 11324 The Largest Clique 题意:求一个节点数最大的节点集,使任意两个节点至少从一个可以到另一个 同一个SCC要选一定全选 求SCC 缩点建一个新图得到一个DAG,直 ...

  5. BZOJ1055: [HAOI2008]玩具取名[区间DP]

    1055: [HAOI2008]玩具取名 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1588  Solved: 925[Submit][Statu ...

  6. 3942: [Usaco2015 Feb]Censoring [KMP]

    3942: [Usaco2015 Feb]Censoring Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 375  Solved: 206[Subm ...

  7. 图片左右间隔滚动Jquery特效

    图片左右间隔滚动Jquery特效,支持自动播放的开启与关闭,同时支持左右箭头的点击播放,具体处理程序如下 <!DOCTYPE html > <html> <head> ...

  8. 栈的理解以及如何计算程序所需栈的大小并在IAR中设置栈

    文章首发于浩瀚先森博客 #栈的理解 一个程序大体上讲都是由变量和函数组合而成,变量有全局变量和局部变量,还有函数间传值的参数以及返回值. Stack是为了程序运行过程中临时保存所需数据而在内存里分配的 ...

  9. Windows phone应用开发[18]-下拉刷新

    在windows phone 中采用数据列表时为了保证用户体验常遇到加载数据的问题.这个问题普遍到只要你用到数据列表就要早晚面对这个问题. 很多人会说这个问题已经有解决方案. 其实真正问题并不在于如何 ...

  10. Java JVM proxy setting

    -Dhttp.proxyPort=8080(your port) -Dhttp.proxyHost=192.168.19.200(your IP) -Dhttp.nonProxyHosts=local ...