YUY2 YUYV 轉YUV420原始碼分析

2021-08-22 04:24:33 字數 2236 閱讀 1021

原始碼思路:

1、yuv有打包(packed)格式和平面(planar)格式。yuy2是packed格式,而yuv420是planar格式,需要轉換。

提取出原影象中每個y、u、v分量,放入陣列中,在根據這些分量組合成新的yuv420格式。

2、yuv420相比yuy2,在y分量上沒有變化,對u、v進行各行提取。

3、長寬為width * height的圖象。如果是yuv444格式,則圖象大小為width * height * 3

如果是yuv422格式,則圖象大小為width * height *2

如果是yuv420格式,則圖象大小為width * height * 1.5

#include

#include

#include

//#include

//src_filename out_filename src_width src_height frameno

int main(int argc, char* argv)

else

src_size = (src_width * src_height) << 1 ;      //源,格式為yuy2,大小src_width * src_height *2

src_buf = (unsigned char *)malloc(src_size*sizeof(char));  

memset(src_buf, 0, src_size);

tem_size = (src_width * src_height) << 1;    //此處開闢一塊與源圖大小相同的一塊區域。本文中首先將packed形式的yuy2格式資料,轉變

//為planar形式的術據,並將這些資料存放在此處開闢的記憶體中。總體而言,此處區域起臨時儲存作用。

//為後序構建yuv420資料服務。

tem_buf = (unsigned char *)malloc(tem_size*sizeof(char));

memset(tem_buf, 0, tem_size);

out_size = src_width*src_height * 1.5;        //開闢src_width*src_height * 1.5大小區域,用於儲存yuv420資料。

out_buf = (unsigned char *)malloc(out_size*sizeof(char));

memset(out_buf, 0, out_size);

in_file = fopen(src_filename, "rb");

if (!in_file)

out_file = fopen(out_filename, "wb");

if (!out_file)

while(framenum>0 && !feof(in_file))

p = p + src_width*2;

y = y + src_width;

u = u + (src_width>>1);

v = v + (src_width>>1);

// packed格式 源圖(以6*8影象為例)

//經過for迴圈後變為planar形式

//復位

y = tem_buf;

u = y + src_width*src_height;

v = u + (src_width*src_height>>1);

int l;

for( l=0; l>1);

memcpy(v2, v, src_width>>1);

u2 = u2 + (src_width>>1);

v2 = v2 + (src_width>>1);

u = u + (src_width);

v = v + (src_width);

}memcpy(y2, y, src_width*src_height);

//最終得到:

fwrite(out_buf, sizeof(char), out_size, out_file);

printf(".");

framenum--;

}fflush(out_file);

free(src_buf); src_buf=null;

free(tem_buf); tem_buf=null;

free(out_buf); out_buf=null;

fclose(in_file);

fclose(out_file);

return 0;

}

YUY2到YUV420的轉換

include include include include src filename out filename src width src height frameno int main int argc,char argv else src size src width src height ...

yuv420 轉bmp的方法

yuv420 轉bmp的方法 我找到了乙個c寫的exe 但是c的水平我實在是不敢去修改那個原始碼,所以我乾脆就呼叫這個dll,在c 裡建立了下面這個類,然後呼叫 y2b.exe 來轉換,而且不會顯示出來那個黑屏.要知道怎麼回事,就看 吧 呵呵.using system using system.co...

基於opencv RGB與YUV420的轉換

opencv的cvtcolor函式可以實現rgb與yuv420的轉換 rgb轉化為yuv420範例 片段 int w 2448 寬 int h 2080 高 int buflen w h 3 2 unsigned char pyuvbuf new unsigned char buflen cv ma...