YUV基础

YUV的三种采样模式

YUV中的Y代表亮度,U代表色调,V代表色饱和度。平时看到的YCbCr,其实就是YUV的一种,Cb就是U,Cr就是V。
YUV 有 3 种采样模式:

  1. 4:4:4,一个像素占3个字节,一个Y分量对应一个U分量和一个V分量。
  2. 4:2:2,平均一个像素占 2 个字节,两个Y分量共享一个U分量和一个V分量。
  3. 4:2:0,平均一个像素占 1.5 个字节,四个Y分量共享一个U分量和一个V分量。

YUV的三种存储格式

  1. planner :平面格式,先连续存储所有像素点的Y,紧接着存储所有像素点的U,随后是所有像素点的V。注意:这里的连续存储,不是一行像素里面连续存储,是整张图片的连续存储,例如:一张yuv444图片是6075kb大小,那第1~2025kb都是Y的数据, 2026~4050kb都是像素点的U数据,以此类推。
  2. semi-Planar:半平面的YUV格式,Y分量单独存储,但是UV分量交叉存储。
  3. 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=KrR+KgG+KbBKr+Kg+Kb=1Cb=BYCr=RYCg=GY
其中:

  1. Kr、Kg、Kb分别表示red、green、blue三个通道的权重,且加起来为1。在不同的标准中,三个值不一样。例如在BT.601标准中,Y=0.299R+0.587G+0.114B。
  2. 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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值