YUV420转YUV444,YUV444转RGB

1.先转换YUV420转换YUV444,这部分网上都有。

var width = ...//图像宽度
var height = ...//图像高度
var bytes = ...//yuv420 byte数组
var newBytes = new Uint8Array(bytes.length  *  2);//新YUV444 byte数组
for(var i = 0 ; i < height ; i ++){
				for(var j = 0 ; j < width ; j ++){
					newBytes[i * width + j] = bytes[i * width + j];
				}
			}
			for(var i = 0 ; i < height ; i +=2){
				for( var j = 0 ; j < width ; j +=2 ){
					newBytes[width * height + i * width + j ] = bytes[width * height + i / 2 * width / 2 + j / 2];
					newBytes[width * height + i * width + j + 1] = bytes[width * height + i / 2 * width / 2 + j / 2];
					newBytes[width * height + ( i + 1 ) * width + j ] = bytes[width * height + i / 2 * width / 2 + j / 2];
					newBytes[width * height + ( i + 1 ) * width + j + 1] = bytes[width * height + i / 2 * width / 2 + j / 2];
					
					
					newBytes[2 * width * height + i * width + j ] = bytes[5 * width * height / 4 + i / 2 * width / 2 + j / 2];
					newBytes[2 * width * height + i * width + j + 1] = bytes[5 * width * height  / 4 + i / 2 * width / 2 + j / 2];
					newBytes[2 * width * height + ( i + 1 ) * width + j ] = bytes[5 * width * height  / 4 + i / 2 * width / 2 + j / 2];
					newBytes[2 * width * height + ( i + 1 ) * width + j + 1] = bytes[5 * width * height  / 4 + i / 2 * width / 2 + j / 2];
	
				}
			}

			


2.yuv444转RGB(其实是BGR)

		function getR(y,u){
			var num = parseInt(y + (1.779 *  (u - 128))) ;
			if( num < 0 ){
				num = 0;
			}else if( num > 255){
				num = 255 ;
			}
			return num;

			
		}
	
		
		function getG(y,u,v){
			var num = parseInt(y - 0.34413 *  (v - 128 )  - ( 0.71414 * ( u - 128 ) ) );
			if( num < 0){
				num = 0;
			}else if(num > 255){
				num = 255 ;
			}
			return  num ; 	
		}
		
		function getB(y,v){
			var num = parseInt(y +  1.402 * (v -128) );
			if(num < 0){
				num = 0;
			}else if(num > 255){
				num = 255;
			}
			 return num;
		}

		var a = 0 ;
		for(var i = 0 ; i < height ; i ++){
			for(var j = 0 ; j < width ; j ++){				
				newBytes[a * 3 + 0] = getR(bytes[i * width + j],bytes[height * width  + i *  width + j]);
				newBytes[a * 3 + 1] = getG(bytes[i * width + j],bytes[2 * height * width  + i *  width + j],bytes[height * width  + i *  width + j]);
				newBytes[a * 3 + 2] = getB(bytes[i * width + j],bytes[2 * height * width  + i *  width + j]);	
				a ++ ;
			}
		}



你可能感兴趣的:(2017-12,图像处理)