/// <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后的基本配置

    1.添加环境变量 右键 此电脑->属性->高级系统设置->环境变量,在系统变量里面找到Path,双击.点击编辑,将mysql中bin文件的路径添加到最后一行,如:F:\AppSev\ ...

  2. Android 通过Uri获取Bitmap对象

    (转自:http://blog.sina.com.cn/s/blog_5de73d0b0100zfm8.html) private Bitmap getBitmapFromUri(Uri uri) { ...

  3. 关于android的日志输出&LogCat

    android提供了自己的log输出api-->位于android.util.Log这个类中. 这个类比较常用的打印日志的方法有5个,这5个方法都会把日志打印到LogCat中: Log.v(ta ...

  4. (转)CNBLOG离线Blog发布方法

    原文章路径:http://www.cnblogs.com/liuxianan/archive/2013/04/13/3018732.html (新添了插件路径) 去年就知道有这个功能,不过没去深究总结 ...

  5. 设置DIV可编辑

    <div id="move" contentEditable="true">可编辑</div> 设置contentEditable属性可 ...

  6. PXC(Percona XtraDB Cluster)集群的安装与配置

    Percona XtraDB Cluster是针对MySQL用户的高可用性和扩展性解决方案,基于Percona Server .其包括了Write Set REPlication补丁,使用Galera ...

  7. [转载]python:open/文件操作

    open/文件操作f=open('/tmp/hello','w') #open(路径+文件名,读写模式) #读写模式:r只读,r+读写,w新建(会覆盖原有文件),a追加,b二进制文件.常用模式 如:' ...

  8. FTP协议

    1. FTP协议 什么是FTP呢?FTP 是 TCP/IP 协议组中的协议之一,是英文File Transfer Protocol的缩写. 该协议是Internet文件传送的基础,它由一系列规格说明文 ...

  9. jdbcTemplate之jdbc模板技术

    1:为什么要使用jdbcTemplate? 在实际开发中使用jdbc技术太过复杂,为了减少代码冗余,操作简单 步骤一:创建实体类 package beans; public class Book { ...

  10. HTML5中支持新的媒体元素有这些

    HTML5对媒体的支持性很强,支持以下媒体元素: · audio  定义音频 · video  定义视频 · embed  作为外部应用的容器 · source  多种媒体源的支持 · track   ...