1. 程式人生 > >常用YUV轉RGB程式碼

常用YUV轉RGB程式碼

常用YUV轉RGB java程式碼

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;
	}
}


相關推薦

常用YUVRGB程式碼

常用YUV轉RGB java程式碼 public class YuvToRGB { private static int R = 0; private static int G = 1; private static int B = 2; //I420是yuv42

常用YUVRGB 程式碼

public class YuvToRGB { private static int R = 0; private static int G = 1; private static int B = 2; //I420是yuv420格式,是3個plane,排列方式為(

YUVRGB彙總

#region ******************彩色影象記錄的格式,YUV 轉 RGB****************** /* YUV, http://zh.wikipedia.org/wiki/YUV#.E5.B8.B8.E7.94.A8.E7.9

yuvRGB

先區分一下YUV和YCbCrYUV色彩模型來源於RGB模型,該模型的特點是將亮度和色度分離開,從而適合於影象處理領域。應用:模擬領域Y'= 0.299*R' + 0.587*G' + 0.114*B'U'= -0.147*R' - 0.289*G' + 0.436*B' =

python實現yuvRGB圖片程式

import os import cv2 import numpy as np from PIL import Image #from scipy import misc import utilty as util search_path = 'E:/sti

最簡單的基於FFmpeg的libswscale的示例(YUVRGB

=====================================================最簡單的基於FFmpeg的libswscale的示例系列文章列表:====================================================

YUVRGB各種公式 (YUVRGB的轉換公式有很多種,請注意區別!!!)

一、 公式:基於BT.601-6       BT601 UV 的座標圖(量化後): (橫座標為u,縱座標為v,左下角為原點)           通過座標圖我們可以看到UV並不會包含整個座標系,而是呈一個旋轉了一

opencv+QT實現影象操作(影象的與、或、異或、取反、兩影象相減、RGBYUVYUVRG等等)

需求簡介: 由於最近在做影象處理的專案,有時候需要快速的知道影象的最大畫素值和最小畫素值是多少,或者影象的最大最小畫素的座標在哪裡。需要快速的得到RGB影象中的R、G、B當中的某個通道。需要把RGB影象轉成YUV資料儲存。需要把YUV資料轉成RGB圖片儲存。當每次需要用到這

FFmpeg 將YUV數據RGB

分配內存 () alloc idt img yuv420 init() 復制 ext void init() //分配兩個Frame,兩段buff,一個轉換上下文 {  //為每幀圖像分配內存 m_pFrameYUV = av_frame_alloc();

FFmpeg 將YUV資料RGB

只要開始初始化一次,結束後釋放就好,中間可以迴圈轉碼  AVFrame *m_pFrameRGB,*m_pFrameYUV; uint8_t *m_rgbBuffer,*m_yuvBuffer; struct SwsContext *m_img_convert_ctx; void i

不同格式的YUVRGB

YUV色彩空間:        Y是亮度值,也就是說8位的灰度值即可組成一幅黑白影象,黑白電視機就是這樣的.        UV是色彩值,是給Y上色用的.U是Cb也就是RGB中的藍色分量,V是Cr也就是RGB中的紅色分量.        YUV444 指的是每四個畫素取樣中每個亮度Y分量都有一個色彩UV分

windows下 安裝 rabbitMQ 及操作常用命令()

點擊 結束 進程 重啟 開始 use 完成 題解 五類 windows下 安裝 rabbitMQ 及操作常用命令 rabbitMQ是一個在AMQP協議標準基礎上完整的,可服用的企業消息系統。它遵循Mozilla Public License開源協議,采用 Erlang

NV12格式RGB的CUDA實現

mic sof spa dex channel cnblogs lock microsoft eight NV12格式是yuv420格式的一種,NV12格式的u,v排布順序為交錯排布,假如一幅圖像尺寸為W*H,則先Y分量有W*H個,然後U分量和V分量交錯排布,U分量和V分

informix數據庫常用命令()

數值 用戶信息 備份與恢復 方法 linux ps -o .net -i ibm Windows(27) Command(2) AD(9) 未分配的博文(0) 微信關註 IT168企業級官微 微信號:IT168qiye 系統

Linux如何查看進程、殺死進程、啟動進程等常用命令()

緩沖 mon whoami hang -s ctrl 遠程登錄 prompt clas 1.查進程 ps命令查找與進程相關的PID號: ps a 顯示現行終端機下的所有程序,包括其他用戶的程序。 ps -A 顯示所有程序。 ps c 列出程序時,顯示

常用顏色的 RGB 顏值表

data afa 分享圖片 data- host 紅色 lsp border aso 這是我見過的顏

C語言--HSVRGB

void HSVtoRGB(uint8_t *r, uint8_t *g, uint8_t *b, uint16_t h, uint16_t s, uint16_t v) { // R,G,B from 0-255, H from 0-360, S,V from 0-100 int

SDL2---編譯SDL庫、測試播放簡單畫素資料(YUVRGB等)

本篇博文整理自雷神(雷霄驊https://blog.csdn.net/leixiaohua1020/article/list/3)多篇博文,多謝分享,在此致敬! SDL簡介: SDL庫的作用說白了就是封裝了複雜的視音訊底層操作,簡化了視音訊處理的難度。 以下轉自WiKi:

【穩定方案】集創北方ICN6211:MIPIRGB功能晶片方案

4.2、ICN6211 4.2.1 功能: ICN6211是一顆[email protected]轉RGB的橋晶片,其應用圖如下: 4.2.2產品特徵: 輸入:MIPI DSI 支援MIPI ® D-PHY Version 1.00.00 和 MIPI ® DSI Versio

YUVRGB調節色彩公式

1.YUV調節色彩公式(必須是量化後的YUV(16-235)),非量化後的YUV轉換有問題。 轉換公式為: 原始YUV(Y,U,V),轉換後YUV(Y',U',V'),亮度 :g_Bright (0-1),飽和度:g_Saturation(0-1),對比度:g_Contrast (0-1