常用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与 ...
随机推荐
- [DeeplearningAI笔记]改善深层神经网络1.1_1.3深度学习使用层面_偏差/方差/欠拟合/过拟合/训练集/验证集/测试集
觉得有用的话,欢迎一起讨论相互学习~Follow Me 1.1 训练/开发/测试集 对于一个数据集而言,可以将一个数据集分为三个部分,一部分作为训练集,一部分作为简单交叉验证集(dev)有时候也成为验 ...
- JAVA设计模式---单例模式的几种实现方式比较
1.延迟实例化方式:(懒汉模式) public class Singleton { private static Singleton uniqueInstance; private Singleton ...
- c# HTTP技术
种植头发能完全成活吗?头发上出现了掉落,头发变细等情况下是需要去看看是不是皮下的毛囊出了问题,要解决这个头发脱发上,选择植发的方式能有效改善,不过也不要小看这个头发脱发,脱发后如果不加以做好毛发护理, ...
- I/O模型简述
1. 前言 最近在学习 Java NIO 方面的知识,为了加深理解.特地去看了 Unix/Linux I/O 方面的知识,并写了一些代码进行验证.在本文接下来的一章中,我将通过举例的方式向大家介绍五种 ...
- 洛谷 [P1963] [NOI2009] 变换序列
这是一道二分图匹配的题 先%dalao博客 建图并没有什么难的,但是关键在于如何使字典序最小. 一个很显然的想法是先求出一个完美匹配,然后从x集合的第一个元素开始,如果该元素匹配的较小的一个,那么继续 ...
- BZOJ 3879: SvT [虚树 后缀树]
传送门 题意: 多次询问,给出一些后缀,求两两之间$LCP$之和 哈哈哈哈哈哈哈竟然$1A$了,刚才还在想如果写不好这道题下节数学就不上了,看来是上天让我上数学课啊 $Suffix\ Virtual\ ...
- POJ 3608 Bridge Across Islands [旋转卡壳]
Bridge Across Islands Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10455 Accepted: ...
- CentOS安装编译Lua
Lua介绍 Lua 是一个小巧的脚本语言.是巴西里约热内卢天主教大学(Pontifical Catholic University of Rio de Janeiro)里的一个研究小组,由Robert ...
- c++中对于json的key不带双引号的问题修复
在引用了第三方数据时,数据源通过转义,将json的key上双引号给去掉了. 在PHP开发时,可以通过正则表达式替换方式来补充丢失的双引号,处理代码如下 function ex_json_decode( ...
- jenkins入门系列之一 jenkins的安装
Jenkins是一个CI(持续集成环境)工具.它可以根据设定持续定期编译,运行相应代码:运行UT或集成测试:将运行结果发送至邮件,或展示成报告... 这样做的最终目的是: 让项目保持健康的状态.如果任 ...