Today JPEG is very popular as a data format. But it is not really a data format. It is an compression method. There are many compression method and JPEG is one of them. It becomes a standard in 1992 and JPEG stands for Joint Photographic Experts Group. JPEG is a lossy compression method.
Lossy compression vs Lossless compression
Lossy compression is when the compression happens it losses data and it never cannot be remade to the original image.
Lossless compression is it does not loose data when compression happens and it can be regenerated to the original image.
JPEG Compression Algorithm
JPEG Compression algorithm has five main basic steps.
- RGB color space to YCbCr color space Conversion
- Preprocessing for DCT transformation
- DCT Transformation
- Co-efficient Quantization
- Lossless Encoding
Let’s see what are these steps in more detail.
1. RGB color space to YCbCr color space conversion
I assume you know these color spaces. If you want to know more about this read this article. In the JPEG compression algorithm, first what it does is this conversion. An digital image in RGB format that is a combination of Red, Green, Blue color channel is converted to YCbCr color channels. Y is the brightness of the image and Cb is the blue difference relative to the green color and Cr is the red difference relative to the red color.
2. Preprocessing for DCT transformation
DCT transformation stands for Discrete Cosine Transformation. Before doing this transformation, some preprocessing should be done. We will take an grey scale image for our example and following is the image.
First an image need to be separated for 8*8 pixel blocks. That means each block has 8*8 pixels and it is 64 pixels in one block. Let’s assume that the dimensions of this image is 240*320. That means this image has 76800 pixels. if we divide this in 64 we can get the number of blocks. 76800 = 64 * 1200. That means we have 1200 blocks in this image. Following image is the partitioned image.
Let’s take one block zoomed and the each size of these pixels. Let’s take the zoomed block in 3rd column on 10th row.
Following is the pixel matrix of this chosen block.
005 176 193 168 168 170 167 165
006 176 158 172 162 177 168 151
005 167 172 232 158 061 145 214
033 179 169 174 005 005 135 178
008 104 180 178 172 197 188 169
063 005 102 101 160 142 133 139
051 047 063 005 180 191 165 005
049 053 043 005 184 170 168 074
Then these values should be centralized about 0 as it helps the next steps. For this substitute 127 from each. Then the new matrix is the following.
–122 0049 0066 0041 0041 0043 0040 0038
–121 0049 0031 0045 0035 0050 0041 0024
–122 0040 0045 0105 0031 –066 0018 0087
–094 0052 0042 0047 –122 –122 0008 0051
–119 –023 0053 0051 0045 0070 0061 0042
–064 –122 –025 –026 0033 0015 0006 0012
–076 –080 –064 –122 0053 0064 0038 –122
–078 –074 –084 –122 0057 0043 0041 –053
3. DCT transformation
This transformation stands for discrete cosine transformation and this increases the effectiveness in the 5th step encoding. Before going to this transform I’ll bit talk about the cosine signal. Cosine signal lies in between the 1 and -1. you can see in the following diagram.
What we do with this transform is that we represent the image pixels with different of cosine waves. By doing this we eliminate high frequencies in the signal. Because human eye is not sensitive to the very high frequency changes of the image. It is the graphical explanation of this transformation. If we talk about the numerical explanation, this transformation creates a new matrix that has the values in left upper corner of the matrix and other places are almost nearly zero. Following is the new matrix after the transformation.
4. Coefficient Quantization
Here what this step does is the values near to 0 is converted to the 0 and other elements also will be shrink towards zero. Then each value of the resultant matrix is divided by another matrix called standard jpeg quantization table. There are two tables for luminace and chromicnace components as shown in the following diagram.
Following matrix is the resultant matrix after the quantization.
5. Lossless Encoding
This is the final step and this method uses huffman coding and it reduces a large amount of memory without loosing any detail of the image. Most of the times it saves 70% of the memory. What this encoding method does is it gets the frequency of different pixels and store that pixels and the frequency instead of storing each pixel.
We have talked about JPEG compression. This article gives very basic idea of the JPEG compression. This needs more mathematics to fully understand the process. Let’s see you in a next article with lot of mathematical details of these processes.