Multimedia University: EMM3126 Digital Image and Video Compression Lab Experiment 2 EMM3126 Digital Image and Video Compression Lab Experiment 2 Generating a JPEG Encoder and Decoder Objective To learn the fundamentals of JPEG encoder and decoder using MATLAB. Equipment MATLAB software. Introduction JPEG is an image compression standard developed by the Joint Photographic Experts Group, which was formally accepted as an international standard in 1992. The JPEG standard allows for both lossy and lossless compression of still images. The algorithm for lossy coding is a DCT-based coding scheme. This is the baseline of JPEG and is sufficient for many applications. However, to meet the needs of applications that cannot tolerate loss, e.g., compression of medical images, a lossless coding scheme is also provided and is based on predictive coding scheme. From the algorithm point of view, JPEG includes four distinct modes of operation, namely, sequential DCT-based mode, progressive DCT-based mode, lossless mode and hierarchical mode. In the sequential DCT-based mode, an image if first partitioned into blocks of 8 x 8 pixels. The blocks are processed from left to right and top to bottom. The 8 x 8 twodimensional forward DCT is applied to each block and the 8 x 8 DCT coefficients are quantized. Finally, the quantized DCT coefficients are entropy encoded and output as part of the compressed image data. In this lab experiment, we will carry out a simple experiment based on sequential DCT-based mode, with the encoder and decoder block diagram of a typical sequential DCT-based mode JPEG shown in Figure 1 below. Since the entropy encoder and entropy decoder are complicated, it will not be implemented in this lab experiment. This experiment will be carried out by using a gray scale image. Revised September 2010 by Dr. Chang Yoong Choon 1 Multimedia University: EMM3126 Digital Image and Video Compression Lab Experiment 2 Figure 1. Block diagram of a typical sequential mode JPEG encoder and decoder. Procedures: Part 1: Importing Image to MATLAB Type the command below at MATLAB command prompt: I = imread('cameraman.tif'); Then type size(I) What does the size of I corresponds to? Now type imfinfo('cameraman.tif') With the information obtained from iminfo, answer the following questions: a. b. c. d. e. f. g. How many bits are used for every pixel intensity? What is the height of the image? What is the width of the image? What is the color type of the image? What is the minimum value of the pixel intensity? What is the maximum value of the pixel intensity? Why do we have these minimum and maximum values? Revised September 2010 by Dr. Chang Yoong Choon 2 Multimedia University: EMM3126 Digital Image and Video Compression Lab Experiment 2 Part 2: Shifting Down Pixel The first step in this JPEG experiment is you have to shift the pixel value by -2P-1, where P is the number of bits used. Construct a MALAB to carry out this operation. Hints: You may use MATLAB command ones(). function h = shift_down(array,bit) % Write the code yourself You should be able to execute the command below I = shift_down(I,8); Part 3: Forward Discrete Cosine Transform (FDCT) The next step is to carry out Forward Discrete Cosine Transform (FDCT) 8-by-8 block of matrix I. However, in this part of the experiment, we will be using a simplified FDCT that is used for an 8-by-8 matrix. The transform matrix T is given by T pq 1 8 0.25 cos p 0, 0 q 7 ( 2q 1) p 16 1 p 7, 1 q 7 For an 8-by-* matrix A, T * A is an 8-by-8 matrix whose columns contain the onedimensional DCT of the columns of A. The two dimensional DCT of A can be computed as B= T * A * T’ ( T Transpose). The inverse two-dimensional DCT of B is given by T’ * A * T. The matrix T can be invoked with the command T = dctmtx(8) To process 8-by-8 block of matrix A, we will use the command blkproc. C = blkproc(I ,[8 8], fun, P1, P2 .. ) processes the image I by applying the function fun to each distinct 8-by-8 block of I, padding I with zeros if necessary. fun can be an inline function, a text string containing the name of a function, or a string containing an expression. fun should operate on an 8-by-8 block, x, and return a matrix, vector, or scalar y: y = fun(x) P1 and P2 are the additional parameters that are passed to fun. To test blkproc, type the command below blkproc(I, [8 8] , 'P1 + x' , ones(8,8)) You are now required to construct a MATLAB program to carry out FDCT on 8-by-8 block of I. Revised September 2010 by Dr. Chang Yoong Choon 3 Multimedia University: EMM3126 Digital Image and Video Compression Lab Experiment 2 function h = FDCT(matrix) % Write the code yourself Part 4: Quantization Before we implement the quantization, we need to implement the quantization table. The formula to generate the quantization table is given by Qij 1 (1 i j ) * quality where 0 i 7, 0 j 7 The parameter quality specifies the quality factor and the recommended range is from 1 to 25, where quality =1 gives the best quality but the lowest compression rate. Notice that the quantization table is an 8-by-8 matrix. The quantization output is such that I ij Iˆij round Qij where Qij is the quantization table, and I ij is the FDCT matrix from Part 3. You are now required to construct a MATLAB code to implement the quantization process. function h = quantization(matrix, quality) %write the code yourself Part 5: Putting the Whole JPEG Encoder Now, try putting the whole encoder together, starting from shifting down the pixel intensity value, to FDCT, and then implementing the quantization. You are now required to construct a MATLAB code to perform this operation. function h = jpeg_encoder(picture, quality) %write the code yourself Part 6: Creating the JPEG Decoder In order to construct JPEG decoder, first you have to dequantize the encoded matrix. To dequantize, you can use Iij Qij Iˆij where Qij is the quantization table from Part 4, and Iˆij is the dequantized matrix. Then, we need to carry out the IFDCT, remember that the 8-by-8 transform matrix T is given in Part 3. The IFDCT of matrix Iij is J T * I * T . The final stage will be shifting up J by 2P-1 where P = 8 is used for representing an intensity value of a pixel. Construct a MATLAB code to perform the decoder operation. Revised September 2010 by Dr. Chang Yoong Choon 4 Multimedia University: EMM3126 Digital Image and Video Compression Lab Experiment 2 function h = jpeg_decoder(matrix, quality) %write the code yourself Part 7: Creating the Whole JPEG Operation You are now required to construct a MATLAB code to integrate the JPEG encoder and decoder you have just constructed. function h = jpeg_operation(picture,quality) %write the code yourself Part 8: Analysis of Reconstructed Image Quality In the last part of this experiment, you are required to analyse the reconstructed image quality by changing the quality factor to 2, 5, 7, 20 and 50. Discuss your observations in the lab report when the quality factor is changed to 2, 5, 7, 20 and 50. Compute also the PSNR value of the reconstructed image when you change the quality value. Marking Scheme Evaluation for this lab experiment is through written lab report. The lab report should be handwritten (MATLAB code and results can be computer generated) and follow the standard report format, i.e. begin with Introduction and end with Conclusion. Students should answer all questions and discuss the observations. Marks will be given in the scale of 0 to 100, based on the following marking scheme: i) Lab experiment overview (10 marks) Introduction to the experiment Summary of the lab experiment Procedure ii) Results and observation (50 marks) Explain the results gathered from the experiment Answer all questions listed in the experiment iii) Discussion and conclusion (10 marks) Conclusive remarks on the experiment iv) Written communication skill (30 marks) Revised September 2010 by Dr. Chang Yoong Choon 5