常用YUV转RGB

  1. public class YuvToRGB {
  2. private static int R = 0;
  3. private static int G = 1;
  4. private static int B = 2;
  5. //I420是yuv420格式,是3个plane,排列方式为(Y)(U)(V)
  6. public static int[] I420ToRGB(byte[] src, int width, int height){
  7. int numOfPixel = width * height;
  8. int positionOfV = numOfPixel;
  9. int positionOfU = numOfPixel/4 + numOfPixel;
  10. int[] rgb = new int[numOfPixel*3];
  11. for(int i=0; i<height; i++){
  12. int startY = i*width;
  13. int step = (i/2)*(width/2);
  14. int startU = positionOfV + step;
  15. int startV = positionOfU + step;
  16. for(int j = 0; j < width; j++){
  17. int Y = startY + j;
  18. int U = startU + j/2;
  19. int V = startV + j/2;
  20. int index = Y*3;
  21. RGB tmp = yuvTorgb(src[Y], src[U], src[V]);
  22. rgb[index+R] = tmp.r;
  23. rgb[index+G] = tmp.g;
  24. rgb[index+B] = tmp.b;
  25. }
  26. }
  27. return rgb;
  28. }
  29. private static class RGB{
  30. public int r, g, b;
  31. }
  32. private static RGB yuvTorgb(byte Y, byte U, byte V){
  33. RGB rgb = new RGB();
  34. rgb.r = (int)((Y&0xff) + 1.4075 * ((V&0xff)-128));
  35. rgb.g = (int)((Y&0xff) - 0.3455 * ((U&0xff)-128) - 0.7169*((V&0xff)-128));
  36. rgb.b = (int)((Y&0xff) + 1.779 * ((U&0xff)-128));
  37. rgb.r =(rgb.r<0? 0: rgb.r>255? 255 : rgb.r);
  38. rgb.g =(rgb.g<0? 0: rgb.g>255? 255 : rgb.g);
  39. rgb.b =(rgb.b<0? 0: rgb.b>255? 255 : rgb.b);
  40. return rgb;
  41. }
  42. //YV16是yuv422格式,是三个plane,(Y)(U)(V)
  43. public static int[] YV16ToRGB(byte[] src, int width, int height){
  44. int numOfPixel = width * height;
  45. int positionOfU = numOfPixel;
  46. int positionOfV = numOfPixel/2 + numOfPixel;
  47. int[] rgb = new int[numOfPixel*3];
  48. for(int i=0; i<height; i++){
  49. int startY = i*width;
  50. int step = i*width/2;
  51. int startU = positionOfU + step;
  52. int startV = positionOfV + step;
  53. for(int j = 0; j < width; j++){
  54. int Y = startY + j;
  55. int U = startU + j/2;
  56. int V = startV + j/2;
  57. int index = Y*3;
  58. //rgb[index+R] = (int)((src[Y]&0xff) + 1.4075 * ((src[V]&0xff)-128));
  59. //rgb[index+G] = (int)((src[Y]&0xff) - 0.3455 * ((src[U]&0xff)-128) - 0.7169*((src[V]&0xff)-128));
  60. //rgb[index+B] = (int)((src[Y]&0xff) + 1.779 * ((src[U]&0xff)-128));
  61. RGB tmp = yuvTorgb(src[Y], src[U], src[V]);
  62. rgb[index+R] = tmp.r;
  63. rgb[index+G] = tmp.g;
  64. rgb[index+B] = tmp.b;
  65. }
  66. }
  67. return rgb;
  68. }
  69. //YV12是yuv420格式,是3个plane,排列方式为(Y)(V)(U)
  70. public static int[] YV12ToRGB(byte[] src, int width, int height){
  71. int numOfPixel = width * height;
  72. int positionOfV = numOfPixel;
  73. int positionOfU = numOfPixel/4 + numOfPixel;
  74. int[] rgb = new int[numOfPixel*3];
  75. for(int i=0; i<height; i++){
  76. int startY = i*width;
  77. int step = (i/2)*(width/2);
  78. int startV = positionOfV + step;
  79. int startU = positionOfU + step;
  80. for(int j = 0; j < width; j++){
  81. int Y = startY + j;
  82. int V = startV + j/2;
  83. int U = startU + j/2;
  84. int index = Y*3;
  85. //rgb[index+R] = (int)((src[Y]&0xff) + 1.4075 * ((src[V]&0xff)-128));
  86. //rgb[index+G] = (int)((src[Y]&0xff) - 0.3455 * ((src[U]&0xff)-128) - 0.7169*((src[V]&0xff)-128));
  87. //rgb[index+B] = (int)((src[Y]&0xff) + 1.779 * ((src[U]&0xff)-128));
  88. RGB tmp = yuvTorgb(src[Y], src[U], src[V]);
  89. rgb[index+R] = tmp.r;
  90. rgb[index+G] = tmp.g;
  91. rgb[index+B] = tmp.b;
  92. }
  93. }
  94. return rgb;
  95. }
  96. //YUY2是YUV422格式,排列是(YUYV),是1 plane
  97. public static int[] YUY2ToRGB(byte[] src, int width, int height){
  98. int numOfPixel = width * height;
  99. int[] rgb = new int[numOfPixel*3];
  100. int lineWidth = 2*width;
  101. for(int i=0; i<height; i++){
  102. int startY = i*lineWidth;
  103. for(int j = 0; j < lineWidth; j+=4){
  104. int Y1 = j + startY;
  105. int Y2 = Y1+2;
  106. int U = Y1+1;
  107. int V = Y1+3;
  108. int index = (Y1>>1)*3;
  109. RGB tmp = yuvTorgb(src[Y1], src[U], src[V]);
  110. rgb[index+R] = tmp.r;
  111. rgb[index+G] = tmp.g;
  112. rgb[index+B] = tmp.b;
  113. index += 3;
  114. tmp = yuvTorgb(src[Y2], src[U], src[V]);
  115. rgb[index+R] = tmp.r;
  116. rgb[index+G] = tmp.g;
  117. rgb[index+B] = tmp.b;
  118. }
  119. }
  120. return rgb;
  121. }
  122. //UYVY是YUV422格式,排列是(UYVY),是1 plane
  123. public static int[] UYVYToRGB(byte[] src, int width, int height){
  124. int numOfPixel = width * height;
  125. int[] rgb = new int[numOfPixel*3];
  126. int lineWidth = 2*width;
  127. for(int i=0; i<height; i++){
  128. int startU = i*lineWidth;
  129. for(int j = 0; j < lineWidth; j+=4){
  130. int U = j + startU;
  131. int Y1 = U+1;
  132. int Y2 = U+3;
  133. int V = U+2;
  134. int index = (U>>1)*3;
  135. RGB tmp = yuvTorgb(src[Y1], src[U], src[V]);
  136. rgb[index+R] = tmp.r;
  137. rgb[index+G] = tmp.g;
  138. rgb[index+B] = tmp.b;
  139. index += 3;
  140. tmp = yuvTorgb(src[Y2], src[U], src[V]);
  141. rgb[index+R] = tmp.r;
  142. rgb[index+G] = tmp.g;
  143. rgb[index+B] = tmp.b;
  144. }
  145. }
  146. return rgb;
  147. }
  148. //NV21是YUV420格式,排列是(Y), (VU),是2 plane
  149. public static int[] NV21ToRGB(byte[] src, int width, int height){
  150. int numOfPixel = width * height;
  151. int positionOfV = numOfPixel;
  152. int[] rgb = new int[numOfPixel*3];
  153. for(int i=0; i<height; i++){
  154. int startY = i*width;
  155. int step = i/2*width;
  156. int startV = positionOfV + step;
  157. for(int j = 0; j < width; j++){
  158. int Y = startY + j;
  159. int V = startV + j/2;
  160. int U = V + 1;
  161. int index = Y*3;
  162. RGB tmp = yuvTorgb(src[Y], src[U], src[V]);
  163. rgb[index+R] = tmp.r;
  164. rgb[index+G] = tmp.g;
  165. rgb[index+B] = tmp.b;
  166. }
  167. }
  168. return rgb;
  169. }
  170. //NV12是YUV420格式,排列是(Y), (UV),是2 plane
  171. public static int[] NV12ToRGB(byte[] src, int width, int height){
  172. int numOfPixel = width * height;
  173. int positionOfU = numOfPixel;
  174. int[] rgb = new int[numOfPixel*3];
  175. for(int i=0; i<height; i++){
  176. int startY = i*width;
  177. int step = i/2*width;
  178. int startU = positionOfU + step;
  179. for(int j = 0; j < width; j++){
  180. int Y = startY + j;
  181. int U = startU + j/2;
  182. int V = U + 1;
  183. int index = Y*3;
  184. RGB tmp = yuvTorgb(src[Y], src[U], src[V]);
  185. rgb[index+R] = tmp.r;
  186. rgb[index+G] = tmp.g;
  187. rgb[index+B] = tmp.b;
  188. }
  189. }
  190. return rgb;
  191. }
  192. //NV16是YUV422格式,排列是(Y), (UV),是2 plane
  193. public static int[] NV16ToRGB(byte[] src, int width, int height){
  194. int numOfPixel = width * height;
  195. int positionOfU = numOfPixel;
  196. int[] rgb = new int[numOfPixel*3];
  197. for(int i=0; i<height; i++){
  198. int startY = i*width;
  199. int step = i*width;
  200. int startU = positionOfU + step;
  201. for(int j = 0; j < width; j++){
  202. int Y = startY + j;
  203. int U = startU + j/2;
  204. int V = U + 1;
  205. int index = Y*3;
  206. RGB tmp = yuvTorgb(src[Y], src[U], src[V]);
  207. rgb[index+R] = tmp.r;
  208. rgb[index+G] = tmp.g;
  209. rgb[index+B] = tmp.b;
  210. }
  211. }
  212. return rgb;
  213. }
  214. //NV61是YUV422格式,排列是(Y), (VU),是2 plane
  215. public static int[] NV61ToRGB(byte[] src, int width, int height){
  216. int numOfPixel = width * height;
  217. int positionOfV = numOfPixel;
  218. int[] rgb = new int[numOfPixel*3];
  219. for(int i=0; i<height; i++){
  220. int startY = i*width;
  221. int step = i*width;
  222. int startV = positionOfV + step;
  223. for(int j = 0; j < width; j++){
  224. int Y = startY + j;
  225. int V = startV + j/2;
  226. int U = V + 1;
  227. int index = Y*3;
  228. RGB tmp = yuvTorgb(src[Y], src[U], src[V]);
  229. rgb[index+R] = tmp.r;
  230. rgb[index+G] = tmp.g;
  231. rgb[index+B] = tmp.b;
  232. }
  233. }
  234. return rgb;
  235. }
  236. //YVYU是YUV422格式,排列是(YVYU),是1 plane
  237. public static int[] YVYUToRGB(byte[] src, int width, int height){
  238. int numOfPixel = width * height;
  239. int[] rgb = new int[numOfPixel*3];
  240. int lineWidth = 2*width;
  241. for(int i=0; i<height; i++){
  242. int startY = i*lineWidth;
  243. for(int j = 0; j < lineWidth; j+=4){
  244. int Y1 = j + startY;
  245. int Y2 = Y1+2;
  246. int V = Y1+1;
  247. int U = Y1+3;
  248. int index = (Y1>>1)*3;
  249. RGB tmp = yuvTorgb(src[Y1], src[U], src[V]);
  250. rgb[index+R] = tmp.r;
  251. rgb[index+G] = tmp.g;
  252. rgb[index+B] = tmp.b;
  253. index += 3;
  254. tmp = yuvTorgb(src[Y2], src[U], src[V]);
  255. rgb[index+R] = tmp.r;
  256. rgb[index+G] = tmp.g;
  257. rgb[index+B] = tmp.b;
  258. }
  259. }
  260. return rgb;
  261. }
  262. //VYUY是YUV422格式,排列是(VYUY),是1 plane
  263. public static int[] VYUYToRGB(byte[] src, int width, int height){
  264. int numOfPixel = width * height;
  265. int[] rgb = new int[numOfPixel*3];
  266. int lineWidth = 2*width;
  267. for(int i=0; i<height; i++){
  268. int startV = i*lineWidth;
  269. for(int j = 0; j < lineWidth; j+=4){
  270. int V = j + startV;
  271. int Y1 = V+1;
  272. int Y2 = V+3;
  273. int U = V+2;
  274. int index = (U>>1)*3;
  275. RGB tmp = yuvTorgb(src[Y1], src[U], src[V]);
  276. rgb[index+R] = tmp.r;
  277. rgb[index+G] = tmp.g;
  278. rgb[index+B] = tmp.b;
  279. index += 3;
  280. tmp = yuvTorgb(src[Y2], src[U], src[V]);
  281. rgb[index+R] = tmp.r;
  282. rgb[index+G] = tmp.g;
  283. rgb[index+B] = tmp.b;
  284. }
  285. }
  286. return rgb;
  287. }
  288. }

常用YUV转RGB代码的更多相关文章

  1. 【视频处理】YUV与RGB格式转换

    YUV格式具有亮度信息和色彩信息分离的特点,但大多数图像处理操作都是基于RGB格式. 因此当要对图像进行后期处理显示时,需要把YUV格式转换成RGB格式. RGB与YUV的变换公式如下: YUV(25 ...

  2. YUV到RGB的转换

    以下内容来源于网络,下面三个链接里的内容是比较好的,感谢博主的分享. http://blog.csdn.net/housisong/article/details/1859084 http://blo ...

  3. YUV和RGB格式分析

    做嵌入式项目的时候,涉及到YUV视频格式到RGB图像的转换,虽然之前有接触到RGB到都是基于opencv的处理,很多东西并不需要我们过多深入的去探讨,现在需要完全抛弃现有的算法程序,需要从内存中一个字 ...

  4. 图像色彩空间YUV和RGB的差别

    http://blog.csdn.net/scg881008/article/details/7168637 假如是200万像素的sensor,是不是RGB一个pixel是2M,YUV是1M? 首先, ...

  5. 最简单的视音频播放演示样例3:Direct3D播放YUV,RGB(通过Surface)

    ===================================================== 最简单的视音频播放演示样例系列文章列表: 最简单的视音频播放演示样例1:总述 最简单的视音频 ...

  6. YUV和RGB格式分析【转】

    转自:http://www.cnblogs.com/silence-hust/p/4465354.html 做嵌入式项目的时候,涉及到YUV视频格式到RGB图像的转换,虽然之前有接触到RGB到都是基于 ...

  7. YUV与RGB格式转换

    YUV格式具有亮度信息和色彩信息分离的特点,但大多数图像处理操作都是基于RGB格式. 因此当要对图像进行后期处理显示时,需要把YUV格式转换成RGB格式. RGB与YUV的变换公式如下: YUV(25 ...

  8. 【图像处理与医学图像处理】YUV与RGB格式转换速度几种方法对比

    [视频处理]YUV与RGB格式转换 YUV格式具有亮度信息和色彩信息分离的特点,但大多数图像处理操作都是基于RGB格式. 因此当要对图像进行后期处理显示时,需要把YUV格式转换成RGB格式. RGB与 ...

  9. 【DSP开发】【VS开发】YUV与RGB格式转换

    [视频处理]YUV与RGB格式转换 YUV格式具有亮度信息和色彩信息分离的特点,但大多数图像处理操作都是基于RGB格式. 因此当要对图像进行后期处理显示时,需要把YUV格式转换成RGB格式. RGB与 ...

随机推荐

  1. ASP.NET没有魔法——ASP.NET MVC 模型验证

    在前面的文章中介绍了用户的注册及登录功能,在注册用户时可以通过代码的形式限制用户名及密码的格式,如果不符合要求那么就无法完成操作,如下图: 该功能的原理是Identity基于的Entity Frame ...

  2. TensorflowTutorial_一维数据构造简单CNN

    使用一维数据构造简单卷积神经网络 觉得有用的话,欢迎一起讨论相互学习~Follow Me 神经网络对于一维数据非常重要,时序数据集.信号处理数据集和一些文本嵌入数据集都是一维数据,会频繁的使用到神经网 ...

  3. JAVA并发编程学习笔记------线程的三种创建方式

    创建线程一般有如下几个方式: 1. 通过继承Thread类来创建一个线程: /** * 步骤1:定义一个继承Thread类的子类 * 步骤2:构造子类的一个对象 * 步骤3:启动线程: * */ pu ...

  4. System.nanoTime

    System.currentTimeMillis()返回的毫秒,这个毫秒其实就是自1970年1月1日0时起的毫秒数. System.nanoTime()返回的是纳秒,nanoTime而返回的可能是任意 ...

  5. BZOJ 4517: [Sdoi2016]排列计数 [容斥原理]

    4517: [Sdoi2016]排列计数 题意:多组询问,n的全排列中恰好m个不是错排的有多少个 容斥原理强行推♂倒她 $恰好m个不是错排 $ \[ =\ \ge m个不是错排 - \ge m+1个不 ...

  6. 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]的价值,每 ...

  7. BZOJ 2084: [Poi2010]Antisymmetry [Manacher]

    2084: [Poi2010]Antisymmetry Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 609  Solved: 387[Submit] ...

  8. Redis服务器启动之后3个警告信息的解决方案

    今天是年前最后一篇文章了,不想写太多的东西,就写一些有关Redis相关问题的解决方案.当我们启动了Redis服务器之后,会看到3个警告,如果没看到,那是很好的,但是我看到了.看到了就不能不管,所以就好 ...

  9. 当inline元素包裹block元素时会发生什么

    经常有图片链接写法如下: <a href="www.baidu.com"><img src="baidu.jpg" /></a&g ...

  10. Redmine基础: 邮件配置

    1.用文本编辑器打开 D:\Bitnami\redmine-2.6.5-0\apps\redmine\htdocs\config\configuration.yml 文件,找到以下内容: 2.配置邮件 ...