YUV的三种采样模式
YUV中的Y代表亮度,U代表色调,V代表色饱和度。平时看到的YCbCr,其实就是YUV的一种,Cb就是U,Cr就是V。
YUV 有 3 种采样模式:
- 4:4:4,一个像素占3个字节,一个Y分量对应一个U分量和一个V分量。
- 4:2:2,平均一个像素占 2 个字节,两个Y分量共享一个U分量和一个V分量。
- 4:2:0,平均一个像素占 1.5 个字节,四个Y分量共享一个U分量和一个V分量。
YUV的三种存储格式
- planner :平面格式,先连续存储所有像素点的Y,紧接着存储所有像素点的U,随后是所有像素点的V。注意:这里的连续存储,不是一行像素里面连续存储,是整张图片的连续存储,例如:一张yuv444图片是6075kb大小,那第1~2025kb都是Y的数据, 2026~4050kb都是像素点的U数据,以此类推。
- semi-Planar:半平面的YUV格式,Y分量单独存储,但是UV分量交叉存储。
- packed :每个像素点的Y,U,V是连续交错存储的。
本文只关注planner格式。
YUV和RGB的转换
Y
=
K
r
∗
R
+
K
g
∗
G
+
K
b
∗
B
K
r
+
K
g
+
K
b
=
1
C
b
=
B
−
Y
C
r
=
R
−
Y
C
g
=
G
−
Y
Y = Kr * R + Kg * G + Kb * B Kr + Kg + Kb = 1 Cb = B - Y Cr = R - Y Cg = G - Y
Y=Kr∗R+Kg∗G+Kb∗BKr+Kg+Kb=1Cb=B−YCr=R−YCg=G−Y
其中:
- Kr、Kg、Kb分别表示red、green、blue三个通道的权重,且加起来为1。在不同的标准中,三个值不一样。例如在BT.601标准中,Y=0.299R+0.587G+0.114B。
- Cb就是U分量,Cr就是V分量。
从上面的公式可以看出,只需要知道Y跟Cb就能求B的值,知道Y跟Cr就能求R的值。知道Y、R跟B就能根据第一条公式求到G的值。所以Cg没必要存储或者传输。
从YUV图像中提取Y、U、V分量
YUV420P
#include <iostream>
#include <fstream>
#include <vector>
int main() {
std::ifstream in("./420yuv.yuv");
std::ofstream outY("./420y.y");
std::ofstream outU("./420u.y");
std::ofstream outV("./420v.y");
int w = 100, h = 100;
std::vector<unsigned char> pic(w * h* 3 / 2 );
in.read(reinterpret_cast<char *>(pic.data()), w * h * 3 / 2);
outY.write(reinterpret_cast<char *>(pic.data()), w * h);
outU.write(reinterpret_cast<char *>(pic.data()) + w * h, w * h / 4);
outV.write(reinterpret_cast<char *>(pic.data()) + w * h * 5 / 4, w * h / 4);
in.close();
outY.close();
outU.close();
outV.close();
return 0;
}
YUV422P
#include <iostream>
#include <fstream>
#include <vector>
int main() {
std::ifstream in("./422yuv.yuv");
std::ofstream outY("./422y.y");
std::ofstream outU("./422u.y");
std::ofstream outV("./422v.y");
int w = 100, h = 100;
std::vector<unsigned char> pic(w * h* 2 );
in.read(reinterpret_cast<char *>(pic.data()), w * h * 2);
outY.write(reinterpret_cast<char *>(pic.data()), w * h);
outU.write(reinterpret_cast<char *>(pic.data()) + w * h, w * h / 2);
outV.write(reinterpret_cast<char *>(pic.data()) + w * h * 3 / 2, w * h / 2);
in.close();
outY.close();
outU.close();
outV.close();
return 0;
}
YUV444P
#include <iostream>
#include <fstream>
#include <vector>
int main() {
std::ifstream in("./444yuv.yuv");
std::ofstream outY("./444y.y");
std::ofstream outU("./444u.y");
std::ofstream outV("./444v.y");
int w = 100, h = 100;
std::vector<unsigned char> pic(w * h* 3 );
in.read(reinterpret_cast<char *>(pic.data()), w * h * 3);
outY.write(reinterpret_cast<char *>(pic.data()), w * h);
outU.write(reinterpret_cast<char *>(pic.data()) + w * h, w * h);
outV.write(reinterpret_cast<char *>(pic.data()) + w * h * 2, w * h);
in.close();
outY.close();
outU.close();
outV.close();
return 0;
}
RGB转YUV实现
#include <iostream>
#include <fstream>
#include <vector>
void RGBToYUV420(unsigned char *rgbData, unsigned char *yData, unsigned char *uData, unsigned char *vData, int width, int height) {
int imageSize = width * height;
for (int j = 0; j<height;j++){
auto ptrRGB = rgbData + width*j*3 ;
for (int i = 0;i<width;i++){
unsigned char r = *(ptrRGB++);
unsigned char g = *(ptrRGB++);
unsigned char b = *(ptrRGB++);
*(yData++) = (unsigned char)((66 * r + 129 * g + 25 * b + 128) >> 8) + 16;
if (j % 2 == 0 && i % 2 == 0) {
*(uData++) = (unsigned char)((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128;
}
else{
if (i % 2 == 0) {
*(vData++) = (unsigned char)((112 * r - 94 * g - 18 * b + 128) >> 8) + 128;
}
}
}
}
}
int main() {
int width = 100;
int height = 100;
unsigned char *rgbData = new unsigned char[width * height * 3];
unsigned char *yuvData = new unsigned char[width * height * 3 / 2];
std::ifstream inputFile("./inRgb.rgb", std::ios::binary);
inputFile.read((char *) rgbData, width * height * 3);
inputFile.close();
RGBToYUV420(rgbData, yuvData, yuvData + width * height, yuvData + width * height * 5 / 4, width, height);
std::ofstream outputFile("./outYuv420.yuv", std::ios::binary);
outputFile.write((char*)yuvData, width * height * 3 / 2);
outputFile.flush();
outputFile.close();
delete[] rgbData;
rgbData = nullptr;
delete[] yuvData;
yuvData = nullptr;
return 0;
}