Top Banner

Click here to load reader

JPEG Image Compression

Sep 26, 2015

ReportDownload

Documents

JPEG Image Compression

JPEG Image Compression

ENGI-4557 Digital Communications JPEG Image Compression

46

2013ENGI-4557 Digital Communications Jonathan Evangelista Brian England Lucas Muller

[JPEG Image Compression]The paper analyzes JPEG image compression utilizing the discrete cosine transform algorithm, quantization tables for compression quality factors and run-length encoding technique.

Table of ContentsIntroduction2Discrete Cosine Transform3DCT in Basis Vectors5MATLAB Functions for Discrete Cosine Transform dct2 and idct28Zig Zag Transformation9Run-Length Encoding10Message Decoding12Quantization for a JPEG Image12Appendix A Code for DCT and Graphical User Interfaces (GUIs)16DCT as Basis Vectors (DCTTest.m)16JPEG Compression (used only for a sample block) (JPEGCompression.m)17JPEG Compression for Gray Scale Image (used in Lena Image) (JPEGCompressionGrayscale,m)1990% Compression (JPEGCompression90.m)2110% Compression (JPEGCompression10.m)23Homepage (Homepage.m)25DCT Test Using Basis Vectors (DCTTest.m)27GUI For Test Bench First Page (DCTFig.m)28GUI For Test Bench Second Page (DCTFig2.m)33GUI for Lena First Page (DCTLena.m)36GUI For Lena Second Page (DCTLena2.m)38Zig Zag Transform for Quantized, Rounded Matrix (zigzag.m)44Run Length Encoding for Zig Zag Transformed Image (RLE_encode.m)46Run Length Decoding for Received Encoded Image (RLE_decode.m)47Inverse Zig Zag Function for Decoded Image (invzigzag.m)48Appendix B MATLAB Graphical User Interfaces50Works Cited52

Introduction

This project analyzes the method of modifying a raw image in spatial domain, such as a bitmap file, into a compressed state. The compression method used is JPEG compression. The discrete cosine transform (DCT) formula is a mathematical algorithm which takes the values of an image in spatial domain and transforms them to the frequency domain; which is required for the compression. A quantization table determines the type and amount of compression which is desired on the image which is to be reconstructed. Many redundant high frequency values from the image are removed using run-length encoding. This is what ultimately reduces the size of the image file. Once the image is to be viewed, the file is decoded and reconstructed. The final result is a compressed image.

Discrete Cosine Transform

The Discrete Cosine Transform (DCT) is a relative of the Discrete Fourier Transform (DFT). The key difference between the two is that DCT uses only real numbers, hence cosine. The forward DCT takes a signal from the spatial domain and transforms it into the frequency domain. This provides many values of zero in the transformed matrix which will allow for efficient encoding such as run-length or Huffman (run-length used in this project). The inverse DCT (IDCT) does the exact opposite, where the signal from the frequency domain is transformed back into the spatial domain to provide a reconstructed image.In image compression, DCT is two-dimensional, as opposed to single-dimensional DCT which would be used in sound wave compression, for example. In JPEG compression, an original image of raw data (such as a bitmap file) is divided into blocks of 8x8 pixel values, which represent a colour with an unsigned integer from 0-255. Once the blocks are segmented, DCT is applied to them to obtain coefficients in the frequency domain. The 8x8 block size was determined by the DCT algorithm which was able to be implemented in VLSI (very large scale integration).The formula which describes the forward discrete cosine transform is:

(Eq. 1.1)

The formula which describes the inverse discrete cosine transform is:

(Eq. 1.2)

In both above cases,

The best way to understand this formula is to analyze a simple mathematical example. Observe the 8x8 block of pixels below:

** * * * * * *

* * * * * * * *

* * * * * * * *

* * * * * * * *

* * * * * * * *

* * * X * * * *

* * * * * * * *

* * * * * * * *

v

u

Figure 1.1In the above 8x8 table which represents an 8x8 block of pixels, each asterisk (*) represents a pixel with a value from 0-255. To get an idea of how the DCT formula works, an X has been placed in the table in the 4th column and 3rd row, and this specific pixel will be analyzed in the formula:

So in this case, u = 3, and v = 2 because the eight columns and rows are from values 0 to 7 (not 1 to 8). Since u and v are not equal to zero, both Cu and Cv are both equal to 1. This yields the equation below:

(Eq. 1.3)

Solving the DCT formula would provide the DCT coefficient which is desired at this location of the 8x8 block. The position of DCT coefficients in the new 8x8 block of values in the frequency domain can be represented as a high frequency or low frequency value. Below is an image provided by XIL Programmers Guide August 1994 and gives an illustration to explain the frequency distribution or DCT coefficient distribution in any given 8x8 block

Figure 1.2

The inverse DCT would provide the coefficient which is desired in the original state of the raw image in the spatial domain.DCT in Basis VectorsAn alternate way of viewing the DCT formula is in terms of basis vectors. Below is the formula for this method:(Eq. 1.4)

Where: , , And The P matrix is the raw image matrix of spatial domain values.

The D matrix is represented by:

(Eq. 1.5)

Where,

And,

Observing these matrices, a comparison can be made to the original DCT equation (Eq. 1.1).MATLAB code may be useful in understanding the methodology of the DCT in basis vectors. The example below will go through the process of obtaining DCT coefficients from a raw image 8x8 block of values in the spatial domain:%This is an example of an 8x8 block of spatial domain values. Each value %represents an unsigned integer from 0 - 255 to represent a colour. P0 = [139 144 149 153 155 155 155 155; 144 151 153 156 159 156 156 156; 150 155 160 163 158 156 156 156; 159 161 162 160 160 159 159 159; 159 160 161 162 162 155 155 155; 161 161 161 161 160 157 157 157; 162 162 161 163 162 157 157 157; 162 162 161 161 163 158 158 158]; %The line below subtracts 128 from each element in the above matrix to %make an signed integer to work with for the DCT algorithm. P = P0 - 128; %The two lines below are to produce the C(omega) term in Eq. 1.5. S = eye(8)/2;S(1,1) = 1/2/sqrt(2); %Below, the DCT vectors are arranged and then multiplied by S to complete%equation Eq 1.5.

D = zeros(8,8); for t = [0:7] for w = [0:7] %where w is u and v D(w+1, t+1) = cos((2*t+1)*w*pi/16); end end %Equation Eq. 1.5 therefore is: D = S * D; %Perform forward DCT to obtain the 8x8 matrix of DCT coefficients.F = D * P * D';

This program outputs the 8x8 matrix value of F (which would further be rounded to 0 decimal places):

This is one possible way to achieve DCT coefficients in MATLAB, and is simply just an explanation of the formula. In the case of this project, the MATLAB functions dct2 and idct2 are utilized.

MATLAB Functions for Discrete Cosine Transform dct2 and idct2

The algorithm used in this project is generated through MATLAB. From the help option, the following information is found which explains the dct2 algorithm in minor detail:

Zig Zag Transformation

After the 8x8 matrix of values has been quantized by the source device, the information must be sent as a string of bits to the receiver. In order to do this, we first use the Zig Zag Transform to change the two-dimensional matrix into a one-dimensional array of entries.Starting from the top-left corner, the transform scans in an alternating up-right and down-left pattern, gathering the values from the matrix and inserting them one at a time into the one-dimensional output array. The code first checks if the scanning is moving in the up-right or bottom-left direction by checking the modulo 2 sum of the current X and Y coordinate, that is to say that if the sum of the X and Y coordinates divided by two has a remainder of zero, then the scan is moving in the up-right direction and otherwise, it is moving in the down-left direction. This process is shown in Figure 2.1. Note that this process of checking the modulo 2 sum, all entries on a diagonal have the same sum and therefore have the same base rules as the rest of the nodes in that diagonal.Figure 2.1Once the basic direction of the node has been determined, additional rules are required for the proper functionality of the Zig Zag transformation. For the case where the modulo 2 sum equals zero, the diagonals have even numbered coordinate sums. When the X coordinate is at the minimum (1) and Y is at the maximum (8), the current node is the top-right and it must move down-left one space. If the X coordinate is at the minimum elsewhere, the code must move one space to the right. If the Y coordinate is at the maximum and the X coordinate is anywhere but the minimum, the code moves down one space. Any other spaces with even coordinate sums result in the code reading the value and move up-right.For cases where the modulo 2 sum is not zero, the diagonals have odd numbered coordinate sums. When the X coordinate is at the maximum and the Y coordinate is not at the maximum, the entry is at the lower left corner and the code moves one space to the right. If the Y coordinate is at the minimum and the X coordinate is at any value less than the maximum, the code moves down one space. Any other spaces with odd coordinate sums result in the code reading the value and move down-left.Lastly, then the code reads the val