yuv420转rgb 及 rgb转bmp保存
/// <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保存的更多相关文章
- Bayer RGB和RGB Raw
Bayer RGB和RGB Raw 对于SENSOR来说,Bayer RGB和RGB Raw两者的图象结构都是BG/GR的(Bayer pattern说的是COLOR FILTER的结构, 分为两种: ...
- RGB 与 (RGB转 YCbCr再转为 RGB)的图像
RGB 与 (RGB转 YCbCr再转为 RGB)的图像 不可逆,能够从 矩阵的逆运算看出来. 附上 matlab 代码: clc,clear; Source=imr ...
- Android camera2 回调imagereader 从Image拿到YUV数据转化成RGB,生成bitmap并保存
ImageUtil.java import android.graphics.ImageFormat; import android.media.Image; import android.os.Bu ...
- 【学习ffmpeg】打开视频文件,帧分析,并bmp保存关键帧
http://www.tuicool.com/articles/jiUzua http://blog.csdn.net/code_future/article/details/8646717 主题 ...
- 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 ...
- 视音频数据处理入门:RGB、YUV像素数据处理
===================================================== 视音频数据处理入门系列文章: 视音频数据处理入门:RGB.YUV像素数据处理 视音频数据处理 ...
- bgr to rgb
因为在研究车牌识别算法(plr),遇到了算法 处理的格式问题,可分三个常用格式: 0:rgb 1:bgr 2:yuv422——需要注意的是,这里为啥选yuv422做识别,当然还可选yuv444,最坏打 ...
- [转载] 视音频数据处理入门:RGB、YUV像素数据处理
===================================================== 视音频数据处理入门系列文章: 视音频数据处理入门:RGB.YUV像素数据处理 视音频数据处理 ...
- 视音频数据处理入门:RGB、YUV像素数据处理【转】
转自:http://blog.csdn.net/leixiaohua1020/article/details/50534150 ==================================== ...
随机推荐
- sql server2014不允许保存更改。阻止保存要求重新创建表的更改
错误描述: SQL Server2014在原有的数据表中修改表结构后,保存数据表,提示错误如下: 不允许保存更改.您所做的更改要求删除并重新创建以下您对无法重新创建的表进行了更改或启用了"阻 ...
- 执行openstack命令报错【You must provide a username via either -...】
openstack环境搭建好后,openstack的服务都启动了,当执行openstack命令时如nova service list报如下错误 You must provide a username ...
- 【2016-10-21】【坚持学习】【Day11】【.net 自带的三种委托】
三种自带委托: Action Predicate Func Action: 无返回类型 Predicate 返回类型是bool类型 Func 自定义返回类型 Action:没有参数没有返回值 Acti ...
- Learn Python The Hard Way ex41中的程序
import random from urllib import urlopen import sys WORD_URL = "http://learncodethehardway.org/ ...
- POJ3281Dining[最大流]
Dining Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 16352 Accepted: 7307 Descripti ...
- UVA 10564 Paths through the Hourglass[DP 打印]
UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...
- zookeeper安装
http://blog.itpub.net/27099995/viewspace-1394831/ http://blog.csdn.net/huwei2003/article/details/491 ...
- Android的setVisibility(View.GONE)无效的问题及原因分析(转)
出现这种情况很可能是因为设置了animation,并且调用了setFillAfter(true),这就会导致setVisibility无效,只需要调用一下clearAnimation()方法或者去掉s ...
- Python-11-RabbitMQ、Redis使用
RabbitMQ RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统.他遵循Mozilla Public License开源协议. MQ全称为Message Queue, 消息队列(M ...
- [转载]一张图看懂开源许可协议,开源许可证GPL、BSD、MIT、Mozilla、Apache和LGPL的区别
本文转载自:http://blog.csdn.net/testcs_dn/article/details/38496107 开源许可证GPL.BSD.MIT.Mozilla.Apache和LGPL的区 ...