注意YUV是每四个Y对应一个UV,并且YUV420P和YUV420SP的UV的存放格式不同,取法也不同,总的来说,YUV420P的取法简单,YUV420SP的取法相对复杂点。
for(int j=0;j
for(int j=0;j
上面代码里,ybase就是YUV中Y的起始地址,ubase就是u的起始地址,vbase就是v的起始地址。而YUV420SP格式中,V就是U的地址加一;YUV420P中U和V都是连续的。按照上面方法,我们就可以得到每一组YUV数据,然后自己可以将每一组数据保存下来,再进行处理。
R=Y+1.4075*(V-128)
G=Y-0.3455*(U-128) – 0.7169*(V-128)
B=Y+1.779*(U-128)
for(int j=0;j
for(int j=0;j
请看另一篇博客,RGB TO BMP
存储为JPG图片要用到一个开运库,libjpeg,或者libjpeg-turbo,我用的是libjpeg,网上关于这两个开源库的资料很多,可以从这个下载编译好的包,LIBJPEG包
封装的存储方法如下:
int rgb2jpeg(const char * filename, unsigned char* rgbData,int image_width,int image_height,int quality)
{
struct jpeg_compress_struct jpeg; //identify a compress object
struct jpeg_error_mgr jerr; //error information
jpeg.err = jpeg_std_error(&jerr);
jpeg_create_compress(&jpeg); //init compress object
FILE* pFile;
fopen_s(&pFile,filename,"wb" );
if( !pFile ) return 0;
jpeg_stdio_dest(&jpeg, pFile);
//compress param set,i just did a simple param set
jpeg.client_data=(void*)&pFile;
jpeg.image_width = image_width;
jpeg.image_height = image_height;
jpeg.input_components = 3;
jpeg.in_color_space = JCS_RGB;
jpeg_set_defaults(&jpeg);
指定亮度及色度质量
jpeg.q_scale_factor[0] = jpeg_quality_scaling(100);
jpeg.q_scale_factor[1] = jpeg_quality_scaling(100);
图像采样率,默认为2 * 2
jpeg.comp_info[0].v_samp_factor = 2;
jpeg.comp_info[0].h_samp_factor = 2;
set jpeg compress quality
jpeg_set_quality(&jpeg, quality, TRUE); //100 is the highest
//start compress
jpeg_start_compress(&jpeg, TRUE);
JSAMPROW row_pointer[1];
//from up to down ,set every pixel
for( unsigned int i=0;i
网上有不少关于YUV420数据存储为JPG的代码和博客,但是我用他们的代码,老是不成功,不是运行不起来,就是效果不好,不过还是表示万分感谢。
int yuv420p_to_jpeg(const char * filename, const char* pdata,int image_width,int image_height, int quality)
{
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
FILE * outfile; // target file
if ((outfile = fopen(filename, "wb")) == NULL) {
fprintf(stderr, "can't open %s\n", filename);
exit(1);
}
jpeg_stdio_dest(&cinfo, outfile);
cinfo.image_width = image_width; // image width and height, in pixels
cinfo.image_height = image_height;
cinfo.input_components = 3; // # of color components per pixel
cinfo.in_color_space = JCS_YCbCr; //colorspace of input image
jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo, quality, TRUE );
//
// cinfo.raw_data_in = TRUE;
cinfo.jpeg_color_space = JCS_YCbCr;
cinfo.comp_info[0].h_samp_factor = 2;
cinfo.comp_info[0].v_samp_factor = 2;
/
jpeg_start_compress(&cinfo, TRUE);
JSAMPROW row_pointer[1];
unsigned char *yuvbuf;
if((yuvbuf=(unsigned char *)malloc(image_width*3))!=NULL)
memset(yuvbuf,0,image_width*3);
unsigned char *ybase,*ubase;
ybase=pdata;
ubase=pdata+image_width*image_height;
int j=0;
while (cinfo.next_scanline < cinfo.image_height)
{
int idx=0;
for(int i=0;i
其实YUV420P和YUV420SP主要区别就是取数据方式不同,前面对于YUV420P如何取数据已经讲得很清楚了,YUV420P存储为JPG只需要在上面YUV420SP存储为JPG的基础上改改取数据方法就好了。
这是一张1280X720的图片,大小385kb,因为是USB摄像头,所以图片质量感觉不是很高,总的来说效果不错!
知道了YUV420SP以及YUV420P的内存格式后,互相转换就不是难事了。
int yuv420sp_to_yuv420p(unsigned char * yuv420sp,unsigned char* yuv420p,int width,int height)
{
if(yuv420sp==NULL)
return;
int i=0,j=0;
//Y
for(i=0;i
int yuv420p_to_yuv420sp(unsigned char * yuv420p,unsigned char* yuv420sp,int width,int height)
{
if(yuv420p==NULL)
return;
int i=0,j=0;
//Y
for(i=0;i