<style>#topics h2 { background: rgba(43, 102, 149, 1); border-radius: 6px; box-shadow: 0 0 1px rgba(95, 90, 75, 1), 1px 1px 6px 1px rgba(10, 10, 0, 0.5); color: rgba(255, 255, 255, 1); font-family: "微軟雅黑", "宋體", "黑體", Arial; font-size: 15px; font-weight: bold; height: 24px; line-height: 23px; margin: 12px 0 !important; padding: 5px 0 5px 10px; text-shadow: 2px 2px 3px rgba(34, 34, 34, 1) } #topics h1 span { font-weight: bold; line-height: 1.5; font-family: "Helvetica Neue", Helvetica, Verdana, Arial, sans-serif; text-decoration: underline; color: rgba(201, 27, 67, 1); text-shadow: 2px 2px 3px rgba(34, 34, 34, 1) }</style>
我們已經從BMP圖中拿到了需要壓縮RGB的資料,我們需要對原資料從RGB域轉變YCbCr域,之后對YCbCr資料進行下采樣(down sampling),對于不需要看文章的同學,這邊直接給出源代碼,https://github.com/Cheemion/JPEG_COMPRESS

圖片參考"Compressed Image File Formats JPEG, PNG, GIF, XBM, BMP - John Miano"[1]
1.RGB域和YCbCr域
RGB代表紅綠藍,通過3種顏色的疊加來得到我們看到的顏色,0-到255分別代表顏色從淺到深,
Y = 0.299 * red + 0.587 * green + 0.114 * blue; Cb = -0.1687 * red - 0.3313 * green + 0.5 * blue + 128; Cr = 0.5 * red - 0.4187 * green - 0.0813 * blue + 128;Y是RGB的加權平均值,稱之為亮度(luminance)
Cb是B分量和亮度的差值, 稱為Chrominance(Cb)
Cr是R分量和亮度的差值,稱為Chrominance(Cr)
以下代碼將RGB轉為YCbCr,為什么將RGB轉為YCbCr? 因為人眼對亮度(Y)的變化更敏感,所以我可以對Cr和Cb進行下采樣(壓縮,比如本來1個位元組代表一個pixel的資料,壓縮后用1個位元組代表4個pixels的資料),盡可能保留完整的Y分量,通過這樣子我們可以進一步的壓縮資料,
void JPG::convertToYCbCr() { for(uint i = 0; i < height; i++) { for(uint j = 0; j < width; j++) { YCbCr temp = BMPData[i * width + j]; BMPData[i * width + j].Y = 0.299 * temp.red + 0.587 * temp.green + 0.114 * temp.blue; BMPData[i * width + j].Cb = -0.1687 * temp.red - 0.3313 * temp.green + 0.5 * temp.blue + 128; BMPData[i * width + j].Cr = 0.5 * temp.red - 0.4187 * temp.green - 0.0813 * temp.blue + 128; } } }
2.sampling(采樣)
采樣通常是對連續信號進行采樣,比如下圖藍色是連續信號x(t),紅色是對信號進行采樣后得到的信號x[n]=x(T*n), T是采樣間隔,1/T是采樣頻率,

而在JPEG中,我們是對已經離散的資料進行采樣,并且JPEG中的采樣數值是相對采樣數值,相對于最高采樣頻率的采樣數值,
如下左圖
Y(luminance)分量的水平采樣頻率(H, Horizantal sampling frequency)和垂直采樣頻率(V, vertical sampling frequency)都是4,是最高的采樣頻率,最高的采樣頻率就相當于保留原圖的Y分量,不進行下采樣,
Cb分量的水平和垂直的采樣頻率都是2,等于最高采樣頻率的一半,所以水平每2個點采樣一次,垂直每2個點采樣一次,
Cr分量的水平和垂直采樣頻率都是1,等于最高采樣頻率的1/4,所以水平和垂直每4個點采樣一個點,
3個分量的量疊加就得到了我們的像素的值,

圖片參考"Compressed Image File Formats JPEG, PNG, GIF, XBM, BMP - John Miano"[1]
2.YCbCr資料在JPEG中的存盤
JPEG規定所有的資料都是以8*8的一個block(data unit)的形式進行離散余弦變化和存盤的.可以把這8*8的block看成是最小存盤單元,
MCU是Y,Cb,Cr的完整的block組成的能夠完整還原一個范圍的色彩的最小單元,啥意思?
假設我們的圖片是10*10的大小
.
若Y,Cb,Cr的水平和垂直的采樣頻率都為1,則原圖由4個mcu(4種顏色分別代表一個MCU)組成(每個mcu包含1個y的block,一個cb的block,一個cr的block, 每個mcu的大小為8*8),邊緣空白的地方可用0替代,也可以重復邊緣的值,
左上角那塊4*4的小block的值分別
pixel[0,0] = y[0,0] + cb[0,0] + cr[0,0]
pixel[0,1] = y[0,1] + cb[0,1] + cr[0,1]
pixel[1,0] = y[1,0] + cb[1,0] + cr[1,0]
pixel[1,1] = y[1,1] + cb[1,1] + cr[1,1]

若Y的水平和垂直采樣頻率為2, cb和cr的采樣頻率為1, 則原圖由1個mcu組成(大小為16*16),mcu中包含4個y的block(2*2),一個cb,一個cr,總共6個block,大小只占原來block的一半,
左上角那塊4*4的小block的值分別
pixel[0,0] = y[0,0] + cb[0,0] + cr[0,0]
pixel[0,1] = y[0,1] + cb[0,0] + cr[0,0]
pixel[1,0] = y[1,0] + cb[0,0] + cr[0,0]
pixel[1,1] = y[1,1] + cb[0,0] + cr[0,0]

總結:mcu大小= 垂直最大采樣值 * 水平最大采樣值, 一個mcu包含y的水平采樣值*y的垂直采樣值個的y個block(y的水平采樣為2,垂直為2,則一個muc有4個yblock),其他分量同理
1.3定義JPG class代碼
//定義Block
using Block = int[64];
//定義YCbCr,同時這個結構用來展示存放rgb資料 struct YCbCr { union { double Y; double red; }; union { double Cb; double green; }; union { double Cr; double blue; }; };
struct MCU { Block* y; Block* cb; Block* cr; };
//定義JPG類,用于壓縮圖片 class JPG { public:
//rgb轉到YCbCr void convertToYCbCr();
//下采樣 void subsampling();
//變化 void discreteCosineTransform();
//量化 void quantization();
//哈夫曼 void huffmanCoding();
//輸出 void output(std::string path); public:
MCU* data;
Block* blocks;
//BMPData存放的是bmp圖片的RGB資料 YCbCr* BMPData; uint blockNum; //原圖的像素 uint width; uint height; //mcu 有多少個 長度是多少 uint mcuWidth; uint mcuHeight; //一個完整的muc的水平和垂直像素個數 uint mcuVerticalPixelNum; uint mcuHorizontalPixelNum; //用于subsampling // only support 1 or 2 byte YVerticalSamplingFrequency; byte YHorizontalSamplingFrequency; byte CbVerticalSamplingFrequency; byte CbHorizontalSamplingFrequency; byte CrVerticalSamplingFrequency; byte CrHorizontalSamplingFrequency; byte maxVerticalSamplingFrequency; byte maxHorizontalSamplingFrequency; public:
JPG(uint width, uint height,const RGB* const rgbs, byte YVerticalSamplingFrequency, byte YHorizontalSamplingFrequency, byte CbVerticalSamplingFrequency, byte CbHorizontalSamplingFrequency, byte CrVerticalSamplingFrequency, byte CrHorizontalSamplingFrequency ) :width(width), height(height), YVerticalSamplingFrequency(YVerticalSamplingFrequency), YHorizontalSamplingFrequency(YHorizontalSamplingFrequency), CbVerticalSamplingFrequency(CbVerticalSamplingFrequency), CbHorizontalSamplingFrequency(CbHorizontalSamplingFrequency), CrVerticalSamplingFrequency(CrVerticalSamplingFrequency), CrHorizontalSamplingFrequency(CrHorizontalSamplingFrequency) { maxHorizontalSamplingFrequency = std::max({YHorizontalSamplingFrequency, CbHorizontalSamplingFrequency, CrHorizontalSamplingFrequency}); maxVerticalSamplingFrequency = std::max({YVerticalSamplingFrequency, CbVerticalSamplingFrequency, CrVerticalSamplingFrequency}); //mcu的個數 mcuWidth = (width + (maxHorizontalSamplingFrequency * 8 - 1)) / (maxHorizontalSamplingFrequency * 8); mcuHeight = (height + (maxVerticalSamplingFrequency * 8 - 1)) / (maxVerticalSamplingFrequency * 8); mcuVerticalPixelNum = maxVerticalSamplingFrequency * 8; mcuHorizontalPixelNum = maxHorizontalSamplingFrequency * 8; //總共多少個MCU data = https://www.cnblogs.com/robsann/archive/2021/02/13/new MCU[mcuWidth * mcuHeight]; //一個MCU有多少個Block blockNum = (YVerticalSamplingFrequency * YHorizontalSamplingFrequency + CbVerticalSamplingFrequency * CbHorizontalSamplingFrequency + CrHorizontalSamplingFrequency * CrVerticalSamplingFrequency); //分配block記憶體空間 blocks = new Block[mcuHeight * mcuHeight * blockNum]; //把記憶體映射到對于的結構中 for (uint i = 0; i < mcuHeight; i++) { for (uint j = 0; j < mcuWidth; j++) {
data[i * mcuWidth + j].y = &blocks[(i * mcuWidth + j) * blockNum]; data[i * mcuWidth + j].cb = data[i * mcuWidth + j].y + YVerticalSamplingFrequency * YHorizontalSamplingFrequency; data[i * mcuWidth + j].cr = data[i * mcuWidth + j].cb + CbVerticalSamplingFrequency * CbHorizontalSamplingFrequency; } } //BMP資料用于存放,bmp的原圖的資料 BMPData = new YCbCr[width * height];
//把bmp資料暫時存放在BMPdata中 for(uint i = 0; i < height; i++) { for(uint j = 0; j < width; j++) { BMPData[i * width + j].red = static_cast<double>(rgbs[i * width + j].red); BMPData[i * width + j].blue = static_cast<double>(rgbs[i * width + j].blue); BMPData[i * width + j].green = static_cast<double>(rgbs[i * width + j].green); } } } ~JPG() { delete[] data; delete[] blocks; delete[] BMPData; } };
1.6下采樣代碼
//這里直接把左上的點 當作subsampling的點了 //也可以取平均值 void JPG::subsampling() { //遍歷mcu for (uint i = 0; i < mcuHeight; i++) { for (uint j = 0; j < mcuWidth; j++) {
//拿到mcu MCU& currentMCU = data[i * mcuWidth + j];
//每個mcu起始的坐標點 uint heightOffset = i * maxVerticalSamplingFrequency * 8; uint widthOffset = j * maxHorizontalSamplingFrequency * 8; //iterate over 每一個component Y, cb cr for (uint componentID = 1; componentID <= 3; componentID++) { //遍歷block, 從muc中拿block for(uint ii = 0, yOffSet = heightOffset; ii < getVerticalSamplingFrequency(componentID); ii++, yOffSet = yOffSet + 8) { for(uint jj = 0, xOffset = widthOffset; jj < getHorizontalSamplingFrequency(componentID); jj++, xOffset = xOffset + 8) {
//拿到具體的block物件 Block& currentBlock = currentMCU[componentID][ii * getHorizontalSamplingFrequency(componentID) + jj]; //遍歷Block every pixels 像素, 并且采樣賦值 for(uint y = 0; y < 8; y++) { for(uint x = 0; x < 8; x++) {
//得到被采樣的那個點的坐標 uint sampledY = yOffSet + y * maxVerticalSamplingFrequency / getVerticalSamplingFrequency(componentID); uint sampledX = xOffset + x * maxHorizontalSamplingFrequency / getHorizontalSamplingFrequency(componentID); //cannot find in original pictures; if(sampledX >= width || sampledY >= height) { currentBlock[y * 8 + x] = 0; } else { currentBlock[y * 8 + x] = BMPData[sampledY * width + sampledX][componentID]; } } } } } } } } }
完整代碼 https://github.com/Cheemion/JPEG_COMPRESS/tree/main/Day2
完結
祝你開心每一天,
參考資料
[1]https://github.com/Cheemion/JPEG_COMPRESS/blob/main/resource/Compressed%20Image%20File%20Formats%20JPEG%2C%20PNG%2C%20GIF%2C%20XBM%2C%20BMP%20-%20John%20Miano.pdf
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/259395.html
標籤:其他
