日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

一文读懂JPEG算法!附C++代码实现JPEG算法,实现从BMP到JPEG转换!

發布時間:2023/12/18 c/c++ 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 一文读懂JPEG算法!附C++代码实现JPEG算法,实现从BMP到JPEG转换! 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

寫在前面:

????????本學期,博主在上一門現代通信技術雙語課程,期末有一個小組Topic“Data Compression”,我被分到去簡單梳理下JPEG、MPEG1-音頻和MPEG1-視頻三種編碼壓縮技術。想起博客好久沒更新了,遂順便水一期。嘿嘿。因為是雙語課程,所以博主是用英語寫的,但不要擔心,我在英語段落下面,進行了翻譯,覺得對您有用的話,請幫作者點個贊支持一下。下面正式開始JPEG講解:

一、What is JPEG:

????????JPEG(Joint Photographic Experts Group) is a standard for continuous tone still image compression. The file suffix is.jpg or.jpeg, which is the most commonly used image file format.?It mainly adopts the joint encoding method of predictive coding (DPCM), discrete cosine transform (DCT) and?entropy coding?to remove redundant image and color data. It is?a lossy compression format, which can compress the image in a small storage space, and to a certain extent will cause damage to the image data. In particular, if the compression ratio is too high, the image quality will be reduced after the final decompression. If the pursuit of high-quality image, it is not appropriate to use too high compression ratio.

????????翻譯:JPEG是用于連續色調靜態圖像壓縮的一種標準,文件后綴名為.jpg或.jpeg是最常用的圖像文件格式。其主要是采用預測編碼(DPCM)、離散余弦變換(DCT)以及熵編碼的聯合編碼方式,以去除冗余的圖像和彩色數據,屬于有損壓縮格式,它能夠將圖像壓縮在很小的儲存空間,一定程度上會造成圖像數據的損傷。尤其是使用過高的壓縮比例,將使最終解壓縮后恢復的圖像質量降低,如果追求高品質圖像,則不宜采用過高的壓縮比例。?

????????Lossy compression, which is to remove unimportant parts of the original data so that it can be stored in a smaller volume, such as the number 485194.200000000001, if we use 485194.2, is a "lossy" preservation method, Because the "0.000000000001" after the decimal point is not important, it can be ignored. JPEG's entire compression process basically follows this step:

????????1. Divide data into "important parts" and "unimportant parts"

????????2. Strain out the unimportant parts

????????3. Save

????????翻譯:有損壓縮,就是把原始數據中不重要的部分去掉,以便可以用更小的體積保存,比如485194.200000000001這個數,如果我們用485194.2來保存,就是一種“有損”的保存方法,因為小數點后面的那個“0.000000000001”屬于不重要的部分,所以可以被忽略掉。JPEG整個壓縮過程基本上也是遵循這個步驟:
????????1. 把數據分為“重要部分”和“不重要部分”
????????2. 濾掉不重要的部分
????????3. 保存

二、Step of compression:

Segmentation of image【圖像分割】:Divide the image into 8 by 8 pixel chunks

????????翻譯:將圖片分割成大小為8*8個像素的小塊

????????Q: What if the image can't be divided by an 8x8 matrix? For example 450x450 graph, jpg how to compress?

????????A: General boundary extension, zero filling, periodic symmetry extension, etc.

????????翻譯:

????????小問題:如果圖像不能被8x8大小的矩陣分割怎么辦?比如450x450的圖,jpg如何壓縮?

????????答:一般邊界拓展,補零,周期對稱延拓等。【這里可以進行一個小的課堂互動】

figure?1?“Lenna”——The world's first JPG image

②RGB->YCbCr【顏色空間轉換】

? ? ? ? "Color space" refers to the mathematical model that represents colors

????????翻譯:“顏色空間”,是指表達顏色的數學模型

figure?2?“RGB”Color space

????????For example, the "RGB" model is to decompose the color into three components, red, green and blue, so that a picture can be decomposed into three grayscale images. Mathematically, every 8X8 pattern can be expressed into three 8X8 matrices, the range of which is generally between [0,255].

????????In the JPEG compression algorithm, the pattern needs to be transformed into a YCbCr model, where Y represents Luminance, Cb and Cr represent the "chromatic aberration" of green and red respectively. The following is the mathematical relation between "RGB" and "YCbCr", and the coefficients are generally empirical values.

????????翻譯:

????????例如“RGB”模型,就是把顏色分解成紅綠藍三種分量,這樣一張圖片就可以分解成三張灰度圖,數學表達上,每一個8X8的圖案,可以表達成三個8X8的矩陣,其中的數值的范圍一般在[0,255]之間。

????????在JPEG壓縮算法中,需要把圖案轉換成為YCbCr模型,這里的Y表示亮度(Luminance),Cb和Cr分別表示綠色和紅色的“色差值”。以下是“RGB”轉“YCbCr”的數學關系式,系數一般為經驗值。

③DCT【離散余弦變換】

????????According to the property of the discrete Fourier transform, the Fourier transform of the real even function only contains the real cosine term, so a kind of transform of the real number domain, the discrete cosine transform (DCT), is constructed.

????????翻譯:根據離散傅里葉變換的性質,實偶函數的傅里葉變換只含實的余弦項,因此構造了一種實數域的變換——離散余弦變換(DCT)。

????????The above equation is a DCT expression. As can be seen from the above equation, a group of one-dimensional data [x0,x1,x2...,xn-1] can be transformed into n transformation series Fi through DCT.

????????翻譯:上式為DCT表達式,從上式可知,一組一維數據[x0,x1,x2,…,xn-1],通過DCT變換后可以得到n個變換級數Fi

????????The above equation is IDCT transformation. After IDCT transformation, the original one-dimensional array can be decomposed into the sum of multiple arrays whose coefficients dB is the transformation series Fi, that is

????????翻譯:上式為IDCT變換,經過IDCT變換,就可以把原來的一個一維數組分解成系數分貝為變換級數Fi的多個數組的和來表示,即

Why do we use the DCT?

????????DCT has a strong "energy concentration" characteristic: most natural signals (including sound and images) are concentrated in the low-frequency portion of the DCT, and when the signal has the statistical characteristics of Markov processes, The de-correlation of DCT is close to the performance of K-L transform (Karhunen-Loeve)?transform - it has optimal de-correlation).

????????翻譯:離散余弦變換具有很強的"能量集中"特性:大多數的自然信號(包括聲音和圖像)的能量都集中在離散余弦變換后的低頻部分,而且當信號具有接近馬爾科夫過程(Markov processes)的統計特性時,離散余弦變換的去相關性接近于K-L變換(Karhunen-Loève變換--它具有最優的去相關性)的性能。

????????Let's take the y-component of the first image in the upper left corner of Lenna as an example, and the transformed matrix is

????????翻譯:我們以Lenna左上角第一塊圖像的Y分量為例,經過變換的矩陣為

figure3?The black block is the processing object

????????It can be seen that after DCT conversion, the "energy" of the matrix is almost all concentrated on the DC component F(0,0) in the upper left corner, and the values in other positions are all very small. In other words, after DCT changes, the data is obviously divided into DC component and AC component, which has played a sufficient role in paving the way for further compression.

????????翻譯:可以看到,經過DCT轉換,矩陣的“能量”幾乎被全部集中在左上角上的直流分量F(0,0)上,其他位置的值都很小。也即數據經過DCT變化后,被明顯分成了直流分量和交流分量兩部分,為后面的進一步壓縮起到了充分的鋪墊作用。

????????The purpose of DCT in the JPEG algorithm is twofold, the first is to make the coefficients as uncorrelated as possible, and the second is that the energy of the input signal should be included in the least number of coefficients as possible.

? ? ? ? 翻譯:在JPEG算法中進行DCT的目的有兩個,第一是使系數盡可能不相關,第二是輸入信號的能量應盡可能地包含到最少數目的系數中。

????????Note that so far, the data is in a reversible state, i.e. no information is lost! Data quantization and Huffman coding are two processes that cause information loss!

????????翻譯:注意,到目前為止,數據都是可逆狀態,即沒有信息丟失!數據量化和霍夫曼編碼這兩個過程會引起信息丟失!

④Quantization of data【數據量化】

????????JPEG provides the following quantization algorithm:

????????翻譯:JPEG提供的量子化算法如下:

????????Where, G is the image matrix we need to process, and Q is called quantization coefficient matrix.?The JPEG algorithm provides two standard quantization coefficient matrices for processing brightness data Y and chromatic aberration data Cr and Cb respectively.

????????翻譯:式中G是我們需要處理的圖像矩陣,Q稱作量化系數矩陣。JPEG算法提供了兩種標準的量化系數矩陣,分別用于處理亮度數據Y和色差數據Cr以及Cb。

table?1?Standard luminance quantifier

table 2?Standard chromatic aberration quantization table

????????The round function is an integer function

????????翻譯:round函數是取整函數

figure 4?Example of quantization process

????????After quantization, we need to transform the two-dimensional array obtained by quantization into a one-dimensional array for the convenience of subsequent Huffman coding.?In the conversion, the order is as follows, since the zeros are mostly clustered in the bottom right corner, in this order you can put as many zeros together as possible.

????????翻譯:在量化后,我們需要把量化得到的二維數組轉變為一維數組,以方便后續的霍夫曼編碼。在轉換時,順序如下,因為0大部分集中在右下角,按此順序可以盡可能地把0放在一起。

figure 5?Conversion sequence diagram

⑤Huffman coding【霍夫曼編碼】

????????The basic principle of JPEG coding is to adjust the encoding length of elements according to the frequency of use of elements in the data to get a higher compression ratio.

????????翻譯:霍夫曼編碼的基本原理是根據數據中元素的使用頻率,調整元素的編碼長度,以得到更高的壓縮比。

????????In Huffman coding there are the following rules:

  • The non-zero data in the array and the number of zeros preceding the data are treated as a processing unit.
  • If the number of zeros in a cell exceeds 16, it is divided into groups of 16.
  • If the last cell is all zeros, it is represented by the special character "EOB". EOB means "the following data is all zeros".
  • ????????翻譯:在JPEG編碼中有如下規定: 將數組中非零的數據,以及數據前面0的個數作為一個處理單元。如果其中某個單元的0的個數超過16,則需要分成每16個一組,如果最后一個單元全都是0,則使用特殊字符“EOB”表示,EOB意思就是“后面的數據全都是0”

    ????????It can be found that the first two digits in the parentheses are between 0 and 15, so the two numbers can be combined into a byte. The high four digits are the number of the first 0, and the last four digits are the number of the following digits. According to this processing, the BIT encoding can be obtained

    ????????翻譯:可以發現,括號中前兩個數字分都在0~15之間,所以這兩個數可以合并成一個byte,高四位是前面0的個數,后四位是后面數字的位數,根據這一處理,可以得到BIT編碼

    ????????Note: The conversion from RLE encoding to BIT encoding is done according to the standard code table lookup provided by JPEG.

    ????????翻譯:注:從RLE編碼轉到BIT編碼,是根據JPEG提供的標準碼表查表完成。

    ????????For the encoding of the numbers before the parentheses, use the Huffman code.?We complete this code according to the official Huffman code table.

    ????????翻譯:對于括號前面的數字的編碼,就要使用霍弗曼編碼。我們根據官方提供的弗曼編碼表,完成這一編碼。

    table 3?DC Huffman code table

    ????????According to table 3, we can know that the corresponding binary code of the DC part data 0x06 is "100".

    ????????翻譯:根據table 3,我們可以知道DC部分數據0x06,對應的二進制編碼是“100”

    ????????Finally, after Huffman encoding and serialization, the following table is obtained, where the serialized data is the data stored in jpeg.

    ????????翻譯:最終,經過霍夫曼編碼和序列化后,可得到下表,其中序列化后的數據就是jpeg中存儲的數據。

    table?4?Huffman coding result

    ????????On a computer, open any JPEG image in binary form and find the following contents:

    ????????翻譯:在電腦中,以二進制形式打開任意一張JPEG格式圖片,發現其內容如下,

    figure 6?JPEG's stored content

    總結:

    編碼過程為

    相應的解碼過程(與編碼過程類似,不再詳述)為

    三、Performance of JPEG:

    ????????Its advantages are:

    ①It supports high compression rate, so the download speed of JPEG image is greatly accelerated;

    ② It can easily process 16.8M color, can reproduce the full color image;

    ③ In the process of image compression, the image format can be freely selected between the minimum file size (lowest image quality) and the maximum file size (highest image quality);

    ④The file size of this format is relatively small and the download speed is fast, which is conducive to the transmission in the case of the bandwidth is not "rich".

    ????????Its disadvantages are:

    ① Not all browsers support the insertion of JPEG images into web pages;

    ② compression may cause loss of image quality, so it is not suitable to use this format to display high definition images.

    ????????翻譯:

    ????????它的優點是:

    ????????①它支持極高的壓縮率,因此JPEG圖像的下載速度大大加快;

    ????????②它能夠輕松地處理16.8M顏色,可以很好地再現全彩色的圖像;

    ????????③在對圖像的壓縮處理過程中,該圖像格式可以允許自由地在最小文件尺寸(最低圖像質量)和最大文件尺寸(最高圖像質量)之間選擇;

    ????????④該格式的文件尺寸相對較小,下載速度快,有利于在帶寬并不“富裕”的情況下傳輸。

    ????????它的缺點是:

    ????????①并非所有的瀏覽器都支持將各種JPEG圖像插入網頁;

    ????????②壓縮時,可能使圖像的質量受到損失,因此不適宜用該格式來顯示高清晰度的圖像。

    四、Implementation of JPEG:

    ????????①主函數代碼見下,輸入為BMP格式圖像,輸出為JPEG格式圖像,可以通過修改“encoderToJPG”函數中的參數,修改壓縮比。

    ????????②通過g++編譯生成a.exe文件,并輸入參數“test.bmp”運行得到out.jpg

    ????????③查看壓縮前后圖片大小

    figure 7?輸入BMP圖像大小

    ????????當參數設定為50時,輸出大小為

    ??????figure 8?參數設為50時,輸出圖像的大小

    ????????當參數設為100時,輸出大小為

    figure9 參數設為100時,輸出圖像的大小

    figure 10?壓縮前后圖像對比

    ????????由上圖可知,通過JPEG算法,可以在保證一定圖像質量的前提下,有效實現數據壓縮。

    代碼連接:

    ?數據壓縮JPEG算法的C++實現

    總結

    以上是生活随笔為你收集整理的一文读懂JPEG算法!附C++代码实现JPEG算法,实现从BMP到JPEG转换!的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。