Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1805220
  • 博文数量: 274
  • 博客积分: 2366
  • 博客等级: 大尉
  • 技术积分: 1880
  • 用 户 组: 普通用户
  • 注册时间: 2007-04-22 09:37
文章分类

全部博文(274)

文章存档

2022年(1)

2020年(10)

2019年(7)

2018年(18)

2017年(26)

2016年(32)

2015年(43)

2014年(30)

2013年(44)

2012年(36)

2011年(17)

2010年(10)

分类: LINUX

2017-11-01 11:13:08

YUV420存储为BMP和JPG图片

原创 2016年08月11日 14:52:49
  • 5286

       网上大多数关于YUV420的资料都是关于YUV420P的,很少有YUV420SP的,因为YUV420SP的UV是交错存放的,处理起来相对麻烦点,但是YUV420SP也是一种常见格式,因此,在这里,我将关于YUV420SP格式数据的处理总结下,方便有需要的同志。


一、YUV420格式数据介绍

       YUV,分为三个分量,“Y”表示明亮度,也就是灰度值;“U"和”V"表示的则是色度,作用是描述影像色彩饱和度,用于指定像素的颜色。YUV主流的采样方式有三种:YUV4:4:4,YUV4:2:2,YUV4:2:0,这里主要介绍下YUV420。

        在YUV420中,一个像素点对应一个Y,一个2X2的小方块对应一个U和V。对于所有YUV420图像,它们的Y值排列是完全相同的,因为只有Y的图像就是灰度图像。YUV420又分为YUV420SP与YUV420P这两种,这两种格式的Y分布是相同的,区别在于UV:YUV420p它是先把U存放完后,再存放V,也就是说UV它们是连续的;而YUV420sp它是UV、UV这样交替存放的。(他们的内存分布图如下,左是YUV420sp,右是YUV420p)
               

二、从YUV内存中取数据组方法

       注意YUV是每四个Y对应一个UV,并且YUV420P和YUV420SP的UV的存放格式不同,取法也不同,总的来说,YUV420P的取法简单,YUV420SP的取法相对复杂点。

1、YUV420SP

[cpp] view plain copy
  1. for(int j=0;j
  2. {    
  3.     for(int i=0;i
  4.     {    
  5.         y=ybase[i + j * DataWidth];// 每四个y对应一个uv    
  6.         u=ubase[j/2 * DataWidth+(i/2)*2];    
  7.         v=ubase[j/2 * DataWidth+(i/2)*2+1];  //一定要注意是u+1        
  8.     }    
  9. }   



2、YUV420P

[cpp] view plain copy
  1. for(int j=0;j
  2. {    
  3.     for(int i=0;i
  4.     {    
  5.         //yyyyyy ... uuuu ...vvv  
  6.         y=ybase[i + j * DataWidth];  
  7.         u=ubase[j/2 * DataWidth/2+(i/2)];    
  8.         v=vbase[j/2 * DataWidth/2+(i/2)];    
  9.     }    
  10. }   


上面代码里,ybase就是YUV中Y的起始地址,ubase就是u的起始地址,vbase就是v的起始地址。而YUV420SP格式中,V就是U的地址加一;YUV420P中U和V都是连续的。按照上面方法,我们就可以得到每一组YUV数据,然后自己可以将每一组数据保存下来,再进行处理。


三、YUV420转换为RGB数据

1、转换公式

R=Y+1.4075*(V-128)
G=Y-0.3455*(U-128) – 0.7169*(V-128)
B=Y+1.779*(U-128)

2.转换方法

YUV420SP

[cpp] view plain copy
  1. for(int j=0;j
  2. {    
  3.     for(int i=0;i
  4.     {    
  5.         unsigned char r,g,b;    
  6.         y=ybase[i + j * DataWidth];  
  7.         u=ubase[j/2 * DataWidth+(i/2)*2];    
  8.         v=ubase[j/2 * DataWidth+(i/2)*2+1];        
  9.   
  10.         b=(unsigned char)(y+1.779*(u- 128));    
  11.         g=(unsigned char)(y-0.7169*(v - 128)-0.3455*(u - 128));      
  12.         r=(unsigned char)(y+ 1.4075*(v - 128));*/    
  13.     }    
  14. }    

YUV420P


[cpp] view plain copy
  1. for(int j=0;j
  2. {    
  3.     for(int i=0;i
  4.     {    
  5.         unsigned char r,g,b;    
  6.         
  7.         y=ybase[i + j * DataWidth];  
  8.         u=ubase[j/2 * DataWidth/2+(i/2)];    
  9.         v=vbase[j/2 * DataWidth/2+(i/2)];    
  10.   
  11.         b=(unsigned char)(y+1.779*(u- 128));    
  12.         g=(unsigned char)(y-0.7169*(v - 128)-0.3455*(u - 128));      
  13.         r=(unsigned char)(y+ 1.4075*(v - 128));*/    
  14.     }    
  15. }   



四、RGB数据存储为图片

注意rgb数据存储为bmp和jpg时的不同,将RBG数据存储为bmp时,数据是逆序存放,并且不是rgb,而是bgr;当将rgb数据存储为jpg时,则不用,不用逆序,数据也还是rgb。

1、存储为BMP图片

请看另一篇博客,RGB TO BMP

2、存储为JPG图片

存储为JPG图片要用到一个开运库,libjpeg,或者libjpeg-turbo,我用的是libjpeg,网上关于这两个开源库的资料很多,可以从这个下载编译好的包,LIBJPEG包

封装的存储方法如下:

[cpp] view plain copy
  1. int rgb2jpeg(const char * filename, unsigned char* rgbData,int image_width,int image_height,int quality)    
  2. {    
  3.     struct jpeg_compress_struct jpeg;  //identify a compress object  
  4.     struct jpeg_error_mgr jerr;  //error information  
  5.   
  6.     jpeg.err = jpeg_std_error(&jerr);    
  7.     jpeg_create_compress(&jpeg);  //init compress object  
  8.   
  9.     FILE* pFile;  
  10.     fopen_s(&pFile,filename,"wb" );    
  11.     if( !pFile )  return 0;    
  12.     jpeg_stdio_dest(&jpeg, pFile);    
  13.   
  14.     //compress param set,i just did a simple param set  
  15.     jpeg.client_data=(void*)&pFile;  
  16.     jpeg.image_width = image_width;    
  17.     jpeg.image_height = image_height;    
  18.     jpeg.input_components  = 3;    
  19.     jpeg.in_color_space = JCS_RGB;     
  20.     jpeg_set_defaults(&jpeg);     
  21.     //// 指定亮度及色度质量    
  22.     jpeg.q_scale_factor[0] = jpeg_quality_scaling(100);    
  23.     jpeg.q_scale_factor[1] = jpeg_quality_scaling(100);    
  24.     //// 图像采样率,默认为2 * 2    
  25.     jpeg.comp_info[0].v_samp_factor = 2;    
  26.     jpeg.comp_info[0].h_samp_factor = 2;    
  27.     //// set jpeg compress quality    
  28.     jpeg_set_quality(&jpeg, quality, TRUE);  //100 is the highest  
  29.   
  30.     //start compress  
  31.     jpeg_start_compress(&jpeg, TRUE);    
  32.   
  33.     JSAMPROW row_pointer[1];    
  34.   
  35.     //from up to down ,set every pixel  
  36.     for( unsigned int i=0;i
  37.     {    
  38.         row_pointer[0] = rgbData+i*jpeg.image_width*3;    
  39.         jpeg_write_scanlines( &jpeg,row_pointer,1 );    
  40.     }    
  41.     //stop compress  
  42.     jpeg_finish_compress(&jpeg);    
  43.   
  44.     fclose( pFile );    
  45.     pFile = NULL;    
  46.     jpeg_destroy_compress(&jpeg);    
  47.     return 0;    
  48. }    


五、YUV数据存储为JPG


网上有不少关于YUV420数据存储为JPG的代码和博客,但是我用他们的代码,老是不成功,不是运行不起来,就是效果不好,不过还是表示万分感谢。

1、YUV420SP


[cpp] view plain copy
  1. int yuv420p_to_jpeg(const char * filename, const char* pdata,int image_width,int image_height, int quality)  
  2. {     
  3.     struct jpeg_compress_struct cinfo;    
  4.     struct jpeg_error_mgr jerr;    
  5.     cinfo.err = jpeg_std_error(&jerr);    
  6.     jpeg_create_compress(&cinfo);    
  7.   
  8.     FILE * outfile;    // target file    
  9.     if ((outfile = fopen(filename, "wb")) == NULL) {    
  10.         fprintf(stderr, "can't open %s\n", filename);    
  11.         exit(1);    
  12.     }    
  13.     jpeg_stdio_dest(&cinfo, outfile);    
  14.   
  15.     cinfo.image_width = image_width;  // image width and height, in pixels    
  16.     cinfo.image_height = image_height;    
  17.     cinfo.input_components = 3;    // # of color components per pixel    
  18.     cinfo.in_color_space = JCS_YCbCr;  //colorspace of input image    
  19.     jpeg_set_defaults(&cinfo);    
  20.     jpeg_set_quality(&cinfo, quality, TRUE );    
  21.   
  22.     //////////////////////////////    
  23.     //  cinfo.raw_data_in = TRUE;    
  24.     cinfo.jpeg_color_space = JCS_YCbCr;    
  25.     cinfo.comp_info[0].h_samp_factor = 2;    
  26.     cinfo.comp_info[0].v_samp_factor = 2;    
  27.     /////////////////////////    
  28.   
  29.     jpeg_start_compress(&cinfo, TRUE);    
  30.   
  31.     JSAMPROW row_pointer[1];  
  32.   
  33.     unsigned char *yuvbuf;  
  34.     if((yuvbuf=(unsigned char *)malloc(image_width*3))!=NULL)  
  35.         memset(yuvbuf,0,image_width*3);  
  36.   
  37.     unsigned char *ybase,*ubase;  
  38.     ybase=pdata;  
  39.     ubase=pdata+image_width*image_height;    
  40.     int j=0;  
  41.     while (cinfo.next_scanline < cinfo.image_height)   
  42.     {  
  43.         int idx=0;  
  44.         for(int i=0;i
  45.         {   
  46.             yuvbuf[idx++]=ybase[i + j * image_width];  
  47.             yuvbuf[idx++]=ubase[j/2 * image_width+(i/2)*2];  
  48.             yuvbuf[idx++]=ubase[j/2 * image_width+(i/2)*2+1];      
  49.         }  
  50.         row_pointer[0] = yuvbuf;  
  51.         jpeg_write_scanlines(&cinfo, row_pointer, 1);  
  52.         j++;  
  53.     }  
  54.     jpeg_finish_compress(&cinfo);    
  55.     jpeg_destroy_compress(&cinfo);    
  56.     fclose(outfile);    
  57.     return 0;    
  58. }  



2、YUV420P

其实YUV420P和YUV420SP主要区别就是取数据方式不同,前面对于YUV420P如何取数据已经讲得很清楚了,YUV420P存储为JPG只需要在上面YUV420SP存储为JPG的基础上改改取数据方法就好了。


效果:


这是一张1280X720的图片,大小385kb,因为是USB摄像头,所以图片质量感觉不是很高,总的来说效果不错!


六、YUV420SP 与 YUV420P相互转换

       知道了YUV420SP以及YUV420P的内存格式后,互相转换就不是难事了。

1、YUV420SP TO YUV420P

[cpp] view plain copy
  1. int yuv420sp_to_yuv420p(unsigned char * yuv420sp,unsigned char* yuv420p,int width,int height)  
  2. {  
  3.     if(yuv420sp==NULL)  
  4.         return;  
  5.     int i=0,j=0;  
  6.     //Y  
  7.     for(i=0;i
  8.     {  
  9.         yuv420p[i]=yuv420sp[i];  
  10.     }  
  11.     //U  
  12.     for(int j=0,i=0;j
  13.     {  
  14.         yuv420p[i + width*height] = yuv420sp[j+width*height];  
  15.     }  
  16.       
  17.     //V  
  18.     for(i=0,j=1,j
  19.     {  
  20.         yuv420p[i+width*height*5/4] = yuv420sp[j+width*height];  
  21.     }  
  22. }  


2、YUV420P TO YUV420SP

[cpp] view plain copy
  1. int yuv420p_to_yuv420sp(unsigned char * yuv420p,unsigned char* yuv420sp,int width,int height)  
  2. {  
  3.     if(yuv420p==NULL)  
  4.         return;  
  5.     int i=0,j=0;  
  6.     //Y  
  7.     for(i=0;i
  8.     {  
  9.         yuv420sp[i]=yuv420p[i];  
  10.     }  
  11.   
  12.     int m=0,n=0;  
  13.     for(int j=0;j
  14.     {  
  15.         if(j%2==0)  
  16.            yuv420sp[j+width*height]=yuv420p[m++];  
  17.         else  
  18.            yuv420sp[j+width*height]=yuv420p[n++];  
  19.     }  
  20. }  


七、参考博客

阅读(1818) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~