YUV和RGB的互轉

2021-10-22 23:52:23 字數 3127 閱讀 6881

yuv和rgb的轉換

模擬訊號

rgb轉yuv

y=0.299r+0.587g+0.114b

u=0.493(b-y)=-0.147r-0.289g+0.436b

v=0.877(r-y)=0.615r-0.515g-0.100b

yuv轉rgb

r=y+1.14v

g=y-0.394u-0.581v

b=y+2.032u

數碼訊號

數碼訊號中,為了便於處理,需要在量化前做歸一化處理

cb=0.564(b-y)

cr=0.713(v-y)

8bit量化中,亮度訊號上端留20級下端留16級作為保護帶。

而在色度訊號中上端15級下端16級,且色差訊號進行歸一處理後,電平範圍為-0.5~0.5,故色差訊號的零電平應對應第128級。

rgb轉ycbcr

y=((66r+129g+25b+128)>>8)+16

cb=((-38r-74g+112b+128)>>8)+128

cr=((112r-94g-18b+128)>>8)+128

ycbcr轉rgb

r=1.164(y-16)+1.596(cr-128)

g=1.164(y-16)-0.813(cr-128)-0.392(cb-128)

b=1.164(y-16)+2.017(cb-128)

色度格式

取樣格式

常見色度取樣格式有4:4:4,4:2:2,4:2:0,4:1:1

這次實驗中選擇了4:2:2格式

儲存格式

先存所有的y,再存所有的u,最後存所有的v,之後進行下一幀

**head.h

#ifndef head_h

#define head_h

#include#include#includeextern int rgb2yuv(char*, char*);

extern int yuv2rgb(char*, char*);

extern int check(char*, char*);

#endif head_h

rgb2yuv.cpp
#include"head.h"

using namespace std;

int rgb2yuv(char*pathin, char*pathout)

in.read((char*)buffer_1, 256 * 256*3);

int pos = 0,y_pos=0;

while (pos<256*256*3)

) y = ((66 * r + 129 * g + 25 * b + 128) >> 8) + 16;

u = ((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128;

v = ((112 * r - 94 * g - 18 * b + 128) >> 8) + 128;

buffer_2[y_pos] = y;

buffer_3[y_pos] = u;

buffer_4[y_pos] = v;

y_pos++;

} int samp_pos = 0;

for (int i=0; i < 256 * 256; i++)

samp_pos = 0;

for (int i = 0; i < 256 * 256; i++)

out.write((char*)buffer_2, 256 * 256);

out.write((char*)buffer_5, 256 * 128);

out.write((char*)buffer_6, 256 * 128);

out.close();

system("pause");

return 0;

}

yuv2rgb.cpp
#include"head.h"

using namespace std;

int yuv2rgb(char*pathin, char*pathout)

int pos = 0;

in.read((char*)y, 256 * 256);

in.read((char*)u_buf, 256 * 128);

in.read((char*)v_buf, 256 * 128);

for (int i = 0; i < 256 * 256; pos++)

pos = 0;

for (int i = 0; i < 256 * 256; pos++)

pos = 0;

unsigned char r, g, b;

while (pos<256*256)

) out.write((char*)buffer_1, 3);

} in.close();

out.close();

system("pause");

return 0;

}

check.cpp
#include"head.h"

using namespace std;

int check(char*path1, char*path2)

} }in.close();

check.close();

cout << aggregate << endl;

system("pause");

return 0;

}

main.cpp
//

#include"stdafx.h"

#include"head.h"

using namespace std;

int main()

輸出結果如下

可見 有153981個資料不一樣,大約是78.3%的資料

用yuv viewer開啟輸出的yuv檔案如上

分析誤差

YUV 與RGB互轉的函式

yuv 與rgb互轉的函式 標頭檔案 ifndef colorconvert h define colorconvert h 1 void initconvttbl void i420 to rgb24 unsigned char src,unsigned char dst,int width,in...

RGB和YUV的區別和轉換

rgb和yuv都是色彩空間,用於表示顏色,兩者可以相互轉化 至於電視採用yuv分量形式是由itu 國際電信聯盟 規定的,因為其能減少資料儲存空間和資料傳輸頻寬,同時又能非常方便的相容黑白電視!yuv是被歐洲電視系統所採用的一種顏色編碼方法 屬於pal 是pal和secam模擬彩色電視制式採用的顏色空...

YUV 格式與 RGB 格式的相互轉換公式

cr vcb u 0.257r 0.504g 0.098b 160.439r 0.368g 0.071b 128 0.148r 0.291g 0.439b 128y 0.257r 0.504g 0.098b 16cr v 0.439r 0.368g 0.071b 128cb u 0.148r 0.2...