crzmy 于 2016.03.09 17:46 提问

【求助】YUV格式转RGB 5C

R=Y+1.140V
G=Y-0.395U-V
B=Y+2.032U

Y取值[16,235],UV取值[16,240]

1：大于255按255计
2：归一化

3个回答

lianshaohua   2016.03.09 18:32

qq_21792169   2016.03.09 20:04

lx624909677      2016.03.09 21:54
`````` 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;
}
}

``````
lx624909677 回复Royn1994: 公式这个应该是固定的，不一样很可能是错的
2 年多之前 回复
crzmy 咦。。公式还不一样。。明天我试试
2 年多之前 回复

YUV格式学习：Y转换成RGB24

YUV 格式与 RGB 格式的相互转换公式及C++ 代码
YUV 格式与 RGB 格式的相互转换公式最近在用的一个工业相机，输出的图像格式是 YUY2 格式。而在电脑上显示时需要 RGB 格式，所以就花了些时间在网上查了些相关的资料。说实话，网上关于 YUV 与 RGB 格式变换的文章挺多的，本来不需要我再多写这么一篇。但是网上那些文章中给出了各种各样的变换公式，公式的系数又各不相同，让人看了之后越看越糊涂。其实那些文章的公式基本都是对的，只不过因为作者忘
YUV与RGB格式转换

yuv转成matlab可读的rgb格式
YUV（亦称YCrCb）是被欧洲电视系统所采用的一种颜色编码方法（属于PAL,PAL是指色彩系统，经常被配以625线、每秒25格画面、隔行扫描的电视广播格式）。 YUV主要用于优化彩色视频信号的传输，使其向后兼容老式黑白电视。与RGB视频信号传输相比，它最大的优点在于只需占用极少的带宽（RGB要求三个独立的视频信号同时传输）。其次YUV表示法的重要性是它的亮度信号(Y)和色度信号(U、V)是相互独

YUV格式、RGB格式、JPEG格式、MJPEG格式之间的转换（C程序）之一
1. Convert YUV420p to YUV422sp 2. Convert YUV422sp to YUV420sp 3. Convert YUV420sp to YUV420p 4. Convert YUV422sp to YUV420p /* Convert YUV420p to YUV422sp */ void convert_yuv420p_to_yu
YUV视频格式到RGB32格式转换的速度优化 上篇
YUV视频格式到RGB32格式转换的速度优化 上篇                    HouSisong@GMail.com   2007.10.30  tag: YUV,YCbCr,YUV到RGB颜色转换,YUV解码,VFW,视频,MMX,SSE,多核优化   摘要: 我们得到的很多视频数据(一些解码器的输出或者摄像头的输出等)
RGB与YUV图像视频格式的相互转换
RGB与YUV图像视频格式的相互转换 本代码演示了RGB与YUV图像视频格
YUV格式转换为RGB（基于opencv）

VC2008项目打包，使用FFmpeg版本为2.2.2 win32bit /* *将RGB转换为YUV420P * *码术 codemanship *http://blog.csdn.net/codemanship *微信公众号: codemanship *本程序实现了RGB像素数据转换为YUV像素数据 *是最简单的FFmpeg视频编码方面的教程。 *通过学习本例子可以了解FFmpeg的图片格式转换的过程。 */