/// <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. 0027 Java学习笔记-面向对象-(非静态、静态、局部、匿名)内部类

    内部类 内部类就是把一个类写在另一个类的内部 用途: 如果一个类只希望它被某一个类访问,那么可以把它定义在另一个类的内部,并用private修饰 内部类可以访问它所在外部类的private成员:但所在 ...

  2. PHP笔记(CSS篇)

    HTML常用于在网页中显示内容,当然,还可以是布局,但是比较不方便,所以,引进了CSS CSS全称Cascading Style Sheets,中文名:层叠样式表 帮助文档:CSS.chm 作用:布局 ...

  3. nodejs中stream相关资料

    nodejs中流(stream)的理解 nodejs stream 手册完整中文版本 nodejs stream详细使用介绍

  4. iptables详解

    Netfilter包含有三种表,三种表下共包含有五种链,链下面包含各种规则.即表包含若干链,链包含若干规则.  (一)三种表为:filter   nat  mangle 1.filter:处理与本机有 ...

  5. 乌版图 read-only file system

    今天在启动虚拟机的时候,运行命令svn up的时候,提示lock,并且read-only file system,这个....我是小白啊,怎么办?前辈在专心写代码,不好打扰,果断找度娘啊 于是乎,折腾 ...

  6. Redmine 插件安装

    将对应的插件都复制进redmine的plugins 安装对应所需要的GEMS bundle install --without development test rmagick 执行插件合并 bund ...

  7. js数组中的常用方法总结

    栈方法(后进先出) ArrayObj.push()方法 ArrayObj.pop()方法 ArrayObj.push():就是向数组末尾添加新的元素,返回的是数组新的长度.ArrayObj.pop() ...

  8. enumerate用法

    Return an enumerate object. sequence must be a sequence, an iterator, or some other object which sup ...

  9. thinkphp怎么实现图片验证码

    1.控制器 function verify() { ob_clean();//丢弃输出缓冲区中的内容 $config = array( 'fontSize' => 20, // 字体大小 'le ...

  10. C#中TransactionScope的使用方法和原理

    在.net 1.1的时代,还没有TransactionScope类,因此很多关于事务的处理,都交给了SqlTransaction和SqlConnection,每个Transaction是基于每个Con ...