常用YUV转RGB代码
常用YUV转RGB
- public class YuvToRGB {
- private static int R = 0;
- private static int G = 1;
- private static int B = 2;
- //I420是yuv420格式,是3个plane,排列方式为(Y)(U)(V)
- public static int[] I420ToRGB(byte[] src, int width, int height){
- int numOfPixel = width * height;
- int positionOfV = numOfPixel;
- int positionOfU = numOfPixel/4 + numOfPixel;
- int[] rgb = new int[numOfPixel*3];
- for(int i=0; i<height; i++){
- int startY = i*width;
- int step = (i/2)*(width/2);
- int startU = positionOfV + step;
- int startV = positionOfU + step;
- for(int j = 0; j < width; j++){
- int Y = startY + j;
- int U = startU + j/2;
- int V = startV + j/2;
- int index = Y*3;
- RGB tmp = yuvTorgb(src[Y], src[U], src[V]);
- rgb[index+R] = tmp.r;
- rgb[index+G] = tmp.g;
- rgb[index+B] = tmp.b;
- }
- }
- return rgb;
- }
- private static class RGB{
- public int r, g, b;
- }
- private static RGB yuvTorgb(byte Y, byte U, byte V){
- RGB rgb = new RGB();
- rgb.r = (int)((Y&0xff) + 1.4075 * ((V&0xff)-128));
- rgb.g = (int)((Y&0xff) - 0.3455 * ((U&0xff)-128) - 0.7169*((V&0xff)-128));
- rgb.b = (int)((Y&0xff) + 1.779 * ((U&0xff)-128));
- rgb.r =(rgb.r<0? 0: rgb.r>255? 255 : rgb.r);
- rgb.g =(rgb.g<0? 0: rgb.g>255? 255 : rgb.g);
- rgb.b =(rgb.b<0? 0: rgb.b>255? 255 : rgb.b);
- return rgb;
- }
- //YV16是yuv422格式,是三个plane,(Y)(U)(V)
- public static int[] YV16ToRGB(byte[] src, int width, int height){
- int numOfPixel = width * height;
- int positionOfU = numOfPixel;
- int positionOfV = numOfPixel/2 + numOfPixel;
- int[] rgb = new int[numOfPixel*3];
- for(int i=0; i<height; i++){
- int startY = i*width;
- int step = i*width/2;
- int startU = positionOfU + step;
- int startV = positionOfV + step;
- for(int j = 0; j < width; j++){
- int Y = startY + j;
- int U = startU + j/2;
- int V = startV + j/2;
- int index = Y*3;
- //rgb[index+R] = (int)((src[Y]&0xff) + 1.4075 * ((src[V]&0xff)-128));
- //rgb[index+G] = (int)((src[Y]&0xff) - 0.3455 * ((src[U]&0xff)-128) - 0.7169*((src[V]&0xff)-128));
- //rgb[index+B] = (int)((src[Y]&0xff) + 1.779 * ((src[U]&0xff)-128));
- RGB tmp = yuvTorgb(src[Y], src[U], src[V]);
- rgb[index+R] = tmp.r;
- rgb[index+G] = tmp.g;
- rgb[index+B] = tmp.b;
- }
- }
- return rgb;
- }
- //YV12是yuv420格式,是3个plane,排列方式为(Y)(V)(U)
- public static int[] YV12ToRGB(byte[] src, int width, int height){
- int numOfPixel = width * height;
- int positionOfV = numOfPixel;
- int positionOfU = numOfPixel/4 + numOfPixel;
- int[] rgb = new int[numOfPixel*3];
- for(int i=0; i<height; i++){
- int startY = i*width;
- int step = (i/2)*(width/2);
- int startV = positionOfV + step;
- int startU = positionOfU + step;
- for(int j = 0; j < width; j++){
- int Y = startY + j;
- int V = startV + j/2;
- int U = startU + j/2;
- int index = Y*3;
- //rgb[index+R] = (int)((src[Y]&0xff) + 1.4075 * ((src[V]&0xff)-128));
- //rgb[index+G] = (int)((src[Y]&0xff) - 0.3455 * ((src[U]&0xff)-128) - 0.7169*((src[V]&0xff)-128));
- //rgb[index+B] = (int)((src[Y]&0xff) + 1.779 * ((src[U]&0xff)-128));
- RGB tmp = yuvTorgb(src[Y], src[U], src[V]);
- rgb[index+R] = tmp.r;
- rgb[index+G] = tmp.g;
- rgb[index+B] = tmp.b;
- }
- }
- return rgb;
- }
- //YUY2是YUV422格式,排列是(YUYV),是1 plane
- public static int[] YUY2ToRGB(byte[] src, int width, int height){
- int numOfPixel = width * height;
- int[] rgb = new int[numOfPixel*3];
- int lineWidth = 2*width;
- for(int i=0; i<height; i++){
- int startY = i*lineWidth;
- for(int j = 0; j < lineWidth; j+=4){
- int Y1 = j + startY;
- int Y2 = Y1+2;
- int U = Y1+1;
- int V = Y1+3;
- int index = (Y1>>1)*3;
- RGB tmp = yuvTorgb(src[Y1], src[U], src[V]);
- rgb[index+R] = tmp.r;
- rgb[index+G] = tmp.g;
- rgb[index+B] = tmp.b;
- index += 3;
- tmp = yuvTorgb(src[Y2], src[U], src[V]);
- rgb[index+R] = tmp.r;
- rgb[index+G] = tmp.g;
- rgb[index+B] = tmp.b;
- }
- }
- return rgb;
- }
- //UYVY是YUV422格式,排列是(UYVY),是1 plane
- public static int[] UYVYToRGB(byte[] src, int width, int height){
- int numOfPixel = width * height;
- int[] rgb = new int[numOfPixel*3];
- int lineWidth = 2*width;
- for(int i=0; i<height; i++){
- int startU = i*lineWidth;
- for(int j = 0; j < lineWidth; j+=4){
- int U = j + startU;
- int Y1 = U+1;
- int Y2 = U+3;
- int V = U+2;
- int index = (U>>1)*3;
- RGB tmp = yuvTorgb(src[Y1], src[U], src[V]);
- rgb[index+R] = tmp.r;
- rgb[index+G] = tmp.g;
- rgb[index+B] = tmp.b;
- index += 3;
- tmp = yuvTorgb(src[Y2], src[U], src[V]);
- rgb[index+R] = tmp.r;
- rgb[index+G] = tmp.g;
- rgb[index+B] = tmp.b;
- }
- }
- return rgb;
- }
- //NV21是YUV420格式,排列是(Y), (VU),是2 plane
- public static int[] NV21ToRGB(byte[] src, int width, int height){
- int numOfPixel = width * height;
- int positionOfV = numOfPixel;
- int[] rgb = new int[numOfPixel*3];
- for(int i=0; i<height; i++){
- int startY = i*width;
- int step = i/2*width;
- int startV = positionOfV + step;
- for(int j = 0; j < width; j++){
- int Y = startY + j;
- int V = startV + j/2;
- int U = V + 1;
- int index = Y*3;
- RGB tmp = yuvTorgb(src[Y], src[U], src[V]);
- rgb[index+R] = tmp.r;
- rgb[index+G] = tmp.g;
- rgb[index+B] = tmp.b;
- }
- }
- return rgb;
- }
- //NV12是YUV420格式,排列是(Y), (UV),是2 plane
- public static int[] NV12ToRGB(byte[] src, int width, int height){
- int numOfPixel = width * height;
- int positionOfU = numOfPixel;
- int[] rgb = new int[numOfPixel*3];
- for(int i=0; i<height; i++){
- int startY = i*width;
- int step = i/2*width;
- int startU = positionOfU + step;
- for(int j = 0; j < width; j++){
- int Y = startY + j;
- int U = startU + j/2;
- int V = U + 1;
- int index = Y*3;
- RGB tmp = yuvTorgb(src[Y], src[U], src[V]);
- rgb[index+R] = tmp.r;
- rgb[index+G] = tmp.g;
- rgb[index+B] = tmp.b;
- }
- }
- return rgb;
- }
- //NV16是YUV422格式,排列是(Y), (UV),是2 plane
- public static int[] NV16ToRGB(byte[] src, int width, int height){
- int numOfPixel = width * height;
- int positionOfU = numOfPixel;
- int[] rgb = new int[numOfPixel*3];
- for(int i=0; i<height; i++){
- int startY = i*width;
- int step = i*width;
- int startU = positionOfU + step;
- for(int j = 0; j < width; j++){
- int Y = startY + j;
- int U = startU + j/2;
- int V = U + 1;
- int index = Y*3;
- RGB tmp = yuvTorgb(src[Y], src[U], src[V]);
- rgb[index+R] = tmp.r;
- rgb[index+G] = tmp.g;
- rgb[index+B] = tmp.b;
- }
- }
- return rgb;
- }
- //NV61是YUV422格式,排列是(Y), (VU),是2 plane
- public static int[] NV61ToRGB(byte[] src, int width, int height){
- int numOfPixel = width * height;
- int positionOfV = numOfPixel;
- int[] rgb = new int[numOfPixel*3];
- for(int i=0; i<height; i++){
- int startY = i*width;
- int step = i*width;
- int startV = positionOfV + step;
- for(int j = 0; j < width; j++){
- int Y = startY + j;
- int V = startV + j/2;
- int U = V + 1;
- int index = Y*3;
- RGB tmp = yuvTorgb(src[Y], src[U], src[V]);
- rgb[index+R] = tmp.r;
- rgb[index+G] = tmp.g;
- rgb[index+B] = tmp.b;
- }
- }
- return rgb;
- }
- //YVYU是YUV422格式,排列是(YVYU),是1 plane
- public static int[] YVYUToRGB(byte[] src, int width, int height){
- int numOfPixel = width * height;
- int[] rgb = new int[numOfPixel*3];
- int lineWidth = 2*width;
- for(int i=0; i<height; i++){
- int startY = i*lineWidth;
- for(int j = 0; j < lineWidth; j+=4){
- int Y1 = j + startY;
- int Y2 = Y1+2;
- int V = Y1+1;
- int U = Y1+3;
- int index = (Y1>>1)*3;
- RGB tmp = yuvTorgb(src[Y1], src[U], src[V]);
- rgb[index+R] = tmp.r;
- rgb[index+G] = tmp.g;
- rgb[index+B] = tmp.b;
- index += 3;
- tmp = yuvTorgb(src[Y2], src[U], src[V]);
- rgb[index+R] = tmp.r;
- rgb[index+G] = tmp.g;
- rgb[index+B] = tmp.b;
- }
- }
- return rgb;
- }
- //VYUY是YUV422格式,排列是(VYUY),是1 plane
- public static int[] VYUYToRGB(byte[] src, int width, int height){
- int numOfPixel = width * height;
- int[] rgb = new int[numOfPixel*3];
- int lineWidth = 2*width;
- for(int i=0; i<height; i++){
- int startV = i*lineWidth;
- for(int j = 0; j < lineWidth; j+=4){
- int V = j + startV;
- int Y1 = V+1;
- int Y2 = V+3;
- int U = V+2;
- int index = (U>>1)*3;
- RGB tmp = yuvTorgb(src[Y1], src[U], src[V]);
- rgb[index+R] = tmp.r;
- rgb[index+G] = tmp.g;
- rgb[index+B] = tmp.b;
- index += 3;
- tmp = yuvTorgb(src[Y2], src[U], src[V]);
- rgb[index+R] = tmp.r;
- rgb[index+G] = tmp.g;
- rgb[index+B] = tmp.b;
- }
- }
- return rgb;
- }
- }
常用YUV转RGB代码的更多相关文章
- 【视频处理】YUV与RGB格式转换
YUV格式具有亮度信息和色彩信息分离的特点,但大多数图像处理操作都是基于RGB格式. 因此当要对图像进行后期处理显示时,需要把YUV格式转换成RGB格式. RGB与YUV的变换公式如下: YUV(25 ...
- YUV到RGB的转换
以下内容来源于网络,下面三个链接里的内容是比较好的,感谢博主的分享. http://blog.csdn.net/housisong/article/details/1859084 http://blo ...
- YUV和RGB格式分析
做嵌入式项目的时候,涉及到YUV视频格式到RGB图像的转换,虽然之前有接触到RGB到都是基于opencv的处理,很多东西并不需要我们过多深入的去探讨,现在需要完全抛弃现有的算法程序,需要从内存中一个字 ...
- 图像色彩空间YUV和RGB的差别
http://blog.csdn.net/scg881008/article/details/7168637 假如是200万像素的sensor,是不是RGB一个pixel是2M,YUV是1M? 首先, ...
- 最简单的视音频播放演示样例3:Direct3D播放YUV,RGB(通过Surface)
===================================================== 最简单的视音频播放演示样例系列文章列表: 最简单的视音频播放演示样例1:总述 最简单的视音频 ...
- YUV和RGB格式分析【转】
转自:http://www.cnblogs.com/silence-hust/p/4465354.html 做嵌入式项目的时候,涉及到YUV视频格式到RGB图像的转换,虽然之前有接触到RGB到都是基于 ...
- YUV与RGB格式转换
YUV格式具有亮度信息和色彩信息分离的特点,但大多数图像处理操作都是基于RGB格式. 因此当要对图像进行后期处理显示时,需要把YUV格式转换成RGB格式. RGB与YUV的变换公式如下: YUV(25 ...
- 【图像处理与医学图像处理】YUV与RGB格式转换速度几种方法对比
[视频处理]YUV与RGB格式转换 YUV格式具有亮度信息和色彩信息分离的特点,但大多数图像处理操作都是基于RGB格式. 因此当要对图像进行后期处理显示时,需要把YUV格式转换成RGB格式. RGB与 ...
- 【DSP开发】【VS开发】YUV与RGB格式转换
[视频处理]YUV与RGB格式转换 YUV格式具有亮度信息和色彩信息分离的特点,但大多数图像处理操作都是基于RGB格式. 因此当要对图像进行后期处理显示时,需要把YUV格式转换成RGB格式. RGB与 ...
随机推荐
- ASP.NET没有魔法——ASP.NET MVC 模型验证
在前面的文章中介绍了用户的注册及登录功能,在注册用户时可以通过代码的形式限制用户名及密码的格式,如果不符合要求那么就无法完成操作,如下图: 该功能的原理是Identity基于的Entity Frame ...
- TensorflowTutorial_一维数据构造简单CNN
使用一维数据构造简单卷积神经网络 觉得有用的话,欢迎一起讨论相互学习~Follow Me 神经网络对于一维数据非常重要,时序数据集.信号处理数据集和一些文本嵌入数据集都是一维数据,会频繁的使用到神经网 ...
- JAVA并发编程学习笔记------线程的三种创建方式
创建线程一般有如下几个方式: 1. 通过继承Thread类来创建一个线程: /** * 步骤1:定义一个继承Thread类的子类 * 步骤2:构造子类的一个对象 * 步骤3:启动线程: * */ pu ...
- System.nanoTime
System.currentTimeMillis()返回的毫秒,这个毫秒其实就是自1970年1月1日0时起的毫秒数. System.nanoTime()返回的是纳秒,nanoTime而返回的可能是任意 ...
- BZOJ 4517: [Sdoi2016]排列计数 [容斥原理]
4517: [Sdoi2016]排列计数 题意:多组询问,n的全排列中恰好m个不是错排的有多少个 容斥原理强行推♂倒她 $恰好m个不是错排 $ \[ =\ \ge m个不是错排 - \ge m+1个不 ...
- BZOJ 4276: [ONTAK2015]Bajtman i Okrągły Robin [线段树优化建边]
4276: [ONTAK2015]Bajtman i Okrągły Robin 题意:\(n \le 5000\)个区间\(l,r\le 5000\),每个区间可以选一个点得到val[i]的价值,每 ...
- BZOJ 2084: [Poi2010]Antisymmetry [Manacher]
2084: [Poi2010]Antisymmetry Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 609 Solved: 387[Submit] ...
- Redis服务器启动之后3个警告信息的解决方案
今天是年前最后一篇文章了,不想写太多的东西,就写一些有关Redis相关问题的解决方案.当我们启动了Redis服务器之后,会看到3个警告,如果没看到,那是很好的,但是我看到了.看到了就不能不管,所以就好 ...
- 当inline元素包裹block元素时会发生什么
经常有图片链接写法如下: <a href="www.baidu.com"><img src="baidu.jpg" /></a&g ...
- Redmine基础: 邮件配置
1.用文本编辑器打开 D:\Bitnami\redmine-2.6.5-0\apps\redmine\htdocs\config\configuration.yml 文件,找到以下内容: 2.配置邮件 ...