JPEG IMAGE COMPRESSION AND DECOMPRESSION WITH MODELING OF DCT COEFFICIENTS ON THE TEXAS INSTRUMENT VIDEO PROCESSING BOARD TMS320DM6437 Viranchi Dwivedi B.E., Gujarat University, India, 2006 PROJECT Submitted in partial satisfaction of the requirements for the degree of MASTER OF SCIENCE in ELECTRICAL AND ELECTRONIC ENGINEERING at CALIFORNIA STATE UNIVERSITY, SACRAMENTO SUMMER 2010 JPEG IMAGE COMPRESSION AND DECOMPRESSION WITH MODELING OF DCT COEFFICIENTS ON THE TEXAS INSTRUMENT VIDEO PROCESSING BOARD TMS320DM6437 A Project by Viranchi Dwivedi Approved by: __________________________________, Committee Chair Jing Pang, Ph.D. __________________________________, Second Reader Fethi Belkhouche, Ph.D. ____________________________ Date ii Student: Viranchi Dwivedi I certify that this student has met the requirements for format contained in the University format manual, and that this project is suitable for shelving in the Library and credit is to be awarded for the project. __________________________, Department Chair _____________________ Suresh Vadhva, Ph.D. Date iii Abstract of JPEG IMAGE COMPRESSION AND DECOMPRESSION WITH MODELING OF DCT COEFFICIENTS ON THE TEXAS INSTRUMENT VIDEO PROCESSING BOARD TMS320DM6437 by Viranchi Dwivedi Image compression has become one of the most important disciplines in digital electronics because of the ever-growing popularity and usage of the internet and multimedia systems combined with the high requirements of the bandwidth and storage space. Various image compression algorithms exist in today’s commercial market. This project mainly emphasis on the Joint Photographic Experts Group (JPEG) image compression standard, which is currently a worldwide standard for the still image compression. The main aim of the project was to write C codes to implement various steps of JPEG compression on the TI’s Video development Platform DM6437 DVDP. Images were captured using digital camera and the resulting images after each successive step of the JPEG algorithm were displayed on a television. Various JPEG decompression steps such as inverse DCT, dequantization, inverse zigzag and run length decoding were also implemented on the Texas Instrument’s DM6437 board to verify the correctness of the corresponding compression steps by reconstructing the original image and displaying it on the television. iv In JPEG algorithm, the image is first divided into small 8x8 blocks and DCT and quantization is applied on each individual blocks. DCT is critical part of the JPEG algorithm and hence it is studied extensively over the past two decades. It is very useful to predict the statistical distribution of the DCT coefficients to test quantization effectiveness. Many well-known probability distribution functions such as Gaussian, Gamma, Laplacian and Rayleigh are assumed to model the DCT coefficients according the Kolomogorov-Smirnov (KS) goodness to fit test. In this project, the MATLAB code was written to apply KS test on the DCT- coefficients of five different still images and resulting graphs were used to find out the best Probability Distribution Functions for accurate modeling of DC and some of the high-energy AC coefficients. The lossy compression schemes such as JPEG standard may exhibit unwanted image artifacts to appear in the images and may blur the reconstructed images. This may take form of the ‘blocky’ effect found in an image, which is basically caused by the coarse quantization of DCT coefficients. Many image-filtering approaches have been proposed to smooth out the discontinuities that appear across the DCT block boundaries. While some of these approaches can effectively decrease the severity of these unwanted artifacts to some extent, other approaches may cause excessive blurring of high-contrast edges in the image due to the lack of proper image segmentation methods prior to applying the filtering. The image de-blocking filter presented in this project can reduce these discontinuities by means of employing local segmentation method prior to applying the moving average filter to a local neighborhood of 3x3 DCT coefficients. The de- v blocking filter presented in this project can improve the image quality and Peak Signal to Noise Rations (PSNR). _______________________, Committee Chair Jing Pang, Ph.D. _______________________ Date vi ACKNOWLEDGEMENTS Firstly, I heartily thank my project advisor Dr. Jing Pang for providing me this wonderful opportunity to work on this JPEG image compression project and giving me a great exposure to the field of image and video processing. This project would not have been possible without the continuous support and encouragement from Dr Pang. Dr. Pang was always there to listen, to give advice and to share her knowledge and expertise in the field of Image and video processing. I would also like to thank Dr. Pang for always being patient and helpful towards the successful demonstration of JPEG algorithm on Texas Instrument DAVINCI EVM320DM6437 board, which is very popular platform in the industry for audio/video processing. I would also like to thank second reader of my project, Dr. Fethi Belkhouche, for reviewing my report and providing me his valuable guidance to improve my project report. Finally, the continuous strength and warmth I got from my family members and friends were invaluable and they drove me on through the critical phase of the project. vii TABLE OF CONTENTS Page Acknowledgements………………………………………………………………………vii List of Tables.…………………………………………………………………………….xi List of Figures...………………………………………………………………………….xii Chapter 1. INTRODUCTION…………………………………………………………………….1 2. JPEG IMAGE COMPRESSION STANDARD……………………………………....3 2.1 Introduction………………………………………...………………………….3 2.2 Steps of JPEG Lossy Algorithm………………………. ……………………..3 2.2.1 4:2:2 YUV Color Format……….…………….……………………………4 2.2.2 8x8 Image Blocks…………..………………………………………………5 2.2.3 Discrete Cosine Transform (DCT)………………………………………….5 2.2.4 Quantization……………………………………….………………………...8 2.2.5 ZigZag Sequence…………………………………………………………..10 2.2.6 Run Length Encoding……………………………………………………...12 3. TMS320DM6437 DVDP PLATFORM OVERVIEW AND IMPLEMENTATION OF JPEG IMAGE COMPRESSION ALGORITHM………..………………………….15 3.1 TMS320DM6437 DVDP Overview……………..………….…………………..15 3.2 VPFE and VPBE drivers on the TMS320DM6437 Platform…………………...17 3.3 JPEG Implementation on TMS320DM6437……………………………………20 viii 3.3.1 Capturing Image on TMS320DM6437…………………………………….…20 3.3.2 Converting Image from YUV format to Gray Scale……………………...…..20 3.3.3 Applying DCT and IDCT on 8x8 Grey Scale Blocks...………………………21 3.3.4 Implementation of Quantization and Dequantization on the TMS320DM6437……………………………………….……………………...25 3.3.5 Zigzag and Inverse Zigzag Sequencing of the Image Pixels.....……...………27 3.3.6 Run Length Encoding and Decoding Implementation on TMS320DM6437...29 4. MODELING OF DCT COEFFICIENTS USING KS GOODNESS TO FIT TEST…32 4.1 Introduction to Different Types of Probability Distribution Functions………....33 4.1.1 Gaussian Probability Distribution Function……..……………………………33 4.1.2 Laplacian Probability Distribution Function……….….……………………...35 4.1.3 Gamma Probability Distribution Function…………………………………...36 4.1.4 Rayleigh Probability Distribution Function………………...…………………37 4.2 Methodology for Applying KS Goodness to Fit Test.………………………….38 4.3 Analysis of the KS Test Results for Five Different Images……………………40 5. DESIGN METHODOLOGY OF DEBLOCKING FILTER TO IMPROVE THE QUALITY OF RECONSTRUCTED DIGITAL IMAGES …….………………...….44 5.1 Introduction to Image Sub Bands and Causes of Image Artifacts ………...........45 5.2 Background and Design Methodology of De-blocking Filter …………………48 5.3 Improvement Achieved in Image Quality Using De-blocking Filter...…………51 6. CONCLUSION AND FUTURE WORK…………………………………………….55 Appendix A C Code for JPEG Compression Implementation on TMS320DM6437…...57 Appendix B MATLAB Code for Applying KS Goodness to Fit Test……....…………..81 ix Appendix C MATLAB Code for Designing De-blocking Filter......…..………………..89 Bibliography……………………………………………………………………………..95 x LIST OF TABLES Page 1. Table 2.3.1: Input Image Pixel Matrix of Size 8x8…………………………...…...7 2. Table 2.3.2: Output DCT Coefficient Matrix…………………..…………………7 3. Table 2.4.1: Sample Matrix of Quantization Coefficients………………………...9 4. Table 2.4.2: DCT Matrix Before Quantization…………………………………....9 5. Table 2.4.3: DCT Matrix After Dequantization…………………..………………9 6. Table 3.3.3.1: Original Cosine Coefficient Matrix….…………………………...22 7. Table 3.3.3.2: Transpose Cosine Coefficient Matrix……………………………22 8. Table 3.3.3.3: Performance Profile for The DCT Implementation…...………….24 9. Table 3.3.4.1: Quantization Matrix for Quality Factor q =2…………………….25 10. Table 3.3.4.1: Performance Profile for The Quantization Implementation……...26 11. Table 3.3.5.1: Performance Profile for The Zigzag Implementation…...………..28 12 Table 3.3.6.1: Performance Profile for The RLE Implementation………………30 13 Table 5.3.1: PSNR Results for Images couple.gif and woman.png………...…..54 xi LIST OF FIGURES Page 1. Figure 2.2.1: Different Steps of JPEG Algorithm………………………………....4 2. Figure 2.2.1.1: 4:2:2 Subsample YUV Format..………………………….……….5 3. Figure 2.5.1: Differential DC Encoding………..……………...………………...11 4. Figure 2.5.2: Zigzag Scan Procedure…………………………………………….11 5. Figure 2.6.1: RLE Encoder Design Flow……………...…………………………13 6. Figure 2.6.2: RLE Decoder Design Flow…...………...…………………………14 7. Figure 3.1.1: Functional Block Diagram of TMS320DM6437 DVDP Platform..17 8. Figure 3.2.1: VPSS Block Diagram……………………………………………...18 9. Figure: 3.3.2: Steps for Implementing JPEG Algorithm on TMS320DM6437 Platform…………………………………………………………………………..21 10. Figure 3.3.3.1: Original Image Captured Through Camera………….…………..23 11. Figure 3.3.3.2: Discrete Cosine Transformed Image..……………………...........23 12. Figure 3.3.3.3: Inverse Discrete Cosine Transformed Image…....……..………..24 13. Figure 3.3.4.1: Quantized DCT Image………………………………………….26 14. Figure 3.3.4.2: Reconstructed Image After Dequantization……...……………...27 15. Figure 3.3.5.1: Captured Image After Zigzag………..…………………………..28 16. Figure 3.3.5.2: Reconstructed Image After Inverse Zigzag………………...……28 17. Figure: 3.3.6.1: Flow Chart for RLE Encoder……………………………...……30 18. Figure: 3.3.6.2: Flow Chart for RLE Decoder……………...……………………31 19. Figure 3.3.6.3: Reconstructed Image After Inverse RLE………………………..31 xii 20. Figure:4.1: Histograms of the DCT coefficients…………………………………32 21. Figure: 4.1.1.1: Graphical Representation of Gaussian PDF…………………….34 22. Figure 4.2.1: Graphical Representation of Laplace PDF………………….……..35 23. Figure: 4.1.3.1 The Graphical Representation of Gamma Distribution……..…...37 24. Figure 4.1.4.1: The Graphical Representation of Rayleigh Distribution…….….38 25. Figure 4.3.1: Standard Test Images Used for This Project.………………...……41 26. Figure 4.3.2(a): KS Statistic Results for Image Ariel……..…….………..…...…42 27. Figure 4.3.2(b): KS Statistic Results for Image Woman..……...………………..42 28. Figure 4.3.2(c): KS Statistic Results for The Image Couple………...……..……43 29. Figure 4.3.2(d): KS Statistic Results for The Image Dog………………………..43 30. Figure 4.3.2(e): KS Statistic Results for The Image Girl…………………….….43 31. Figure 5.1.1: 64 Image Primitives After Applying DCT….……….…………….45 32. Figure 5.1.2: Original Image Couple.gif and Its DC Sub Band….…...…………..46 33. Figure 5.1.3: Original Image and Artifacts Observed in Its Reconstructed Image…………….……...……………………...…………………………………48 34. Figure 5.3.1: Reconstructed Images for Original Image “couple.gif” With and Without Using De-blocking Filter Respectively...…..……………………………52 35. Figure 5.3.2: Reconstructed Images for Original Image “woman .png” With and Without Using De-blocking Filter Respectively……………….…………………53 xiii 1 Chapter 1 INTRODUCTION Due to the advances in various aspects of digital electronics like image acquisition, data storage and display, many new applications of the digital imaging have emerged over the past decade. However, many of these applications are not widespread because of required large storage space. As a result, the image compression has grown tremendously over the last decade and various image compression algorithms have been proposed. Joint Photographic Experts Group (JPEG) is a worldwide standard for the compression of still images. This project provides a detailed study of various steps of JPEG standards such as Discrete Cosine Transform (DCT), quantization, zigzag and run length encoding. Knowledge of the statistical distribution of the DCT coefficients is necessary in order to design the efficient quantizer with high SNR. Four different probability distribution functions (Gaussian, Gamma, Laplacian and Rayleigh) are presented for the accurate modeling of the DCT coefficients. Methodology for applying KS goodness to fit test in order to determine the best-fitted distribution function for DC and AC coefficients of image is also explained in depth. Images may exhibit blocking artifacts and blurring effects after the JPEG reconstruction if they have been quantized very heavily. A de-blocking filter approach is presented in this project to effectively reduce these blocking artifacts from the reconstructed images. 2 Chapter 2 describes the fundamental JPEG image compression algorithm including DCT, quantization, zigzag and run length encoding in some detail. Chapter 3 discusses the useful information and different features of Texas Instrument image and video development platform TMS320DM6437. This chapter also contains the explanation of various steps of JPEG compression implemented on the same board. C program used to implement the related algorithm on TMS320DM6437 platform is included in Appendix A. Chapter 4 focuses on the methodology to design KS goodness to fit test for DC and some of the high-energy AC coefficients for five different still digital images. MATLAB code written to generate the DCT coefficients and to apply KS test to find the Probability Distribution that best models the coefficients is included in Appendix B. The resulting graphs of the KS statistic distance for different probability distribution functions are also included in the same chapter. Chapter 5 describes the blocking artifacts and suggests a way of implementing the de-blocking filter to improve PSNR and the quality of reconstructed images. MATLAB code written to design the de-blocking filter is included in Appendix C. Chapter 6 gives conclusion and describes the future work on the JPEG image compression algorithm, modeling of DCT coefficients and implementation of deblocking filter. 3 Chapter 2 JPEG IMAGE COMPRESSION STANDARD 2.1 Introduction JPEG is the international standard for the effective compression of the still digital images. It includes specifications for both- lossless and lossy compression algorithm. This project concentrates mainly on lossy encoding. JPEG lossy standard is designed to reduce the high frequency component of the image frame that human eye cannot detect easily. Because human eyes cannot detect slight change in the color space, but they can easily perceive slight change in the intensity (dark and light). JPEG standard tends to be more aggressive towards the compression of the color-part (chrominance) of the image instead of the gray-scale part of the image. Due to the quantization effect, JPEG Lossy compression algorithm results in the loss of some of the image information and hence degradation of image quality occurs every time the images are compressed and then retrieved back. Photographs, video stills and any other graphics that resembles natural subjects tend to have smooth variation in color and tone and can be easily compressed using JPEG standard while animations, black and white documents and vector graphics tend to have sharp contrast in the color and tone between the adjacent pixels cannot be compressed effectively using JPEG. 2.2 Steps of JPEG Lossy Algorithm Various steps of the JPEG lossy compression standard are shown in Figure 2.2.1 and are described in detail in this chapter. 4 8x8 image blocks DIGITAL STILL IMAGE ZIG ZAG SEQUENCES RLE (RUN LENGTH ENCODING) TRANSFORMATION TO APPROPIATE COLORSPACE QUANTIZATION FORWARD DESCRETE COSINE TRANSFORM 01110101 (COMPRESSED DATA) Figure 2.2.1: Different Steps of JPEG Algorithm 2.2.1 4:2:2 YUV Color Format The JPEG compression works best on the luminance/chrominance color space such as YUV color format. The grey-scale, low frequency luminance component (Y) of the YUV color format contains the visual information of the image to which human eyes are very sensitive. The chrominance components (U and V) contain high frequency color information to which human eyes are insensitive. Thus, most of high frequency information can be discarded in order to achieve high compression ratio. Texas Instrument image and video development platform TMS320DM6437 uses 4:2:2 YUV color format in which U and V components are sub sampled to be present in the interleaved fashion between every Y components. It is known as packed YUV color 5 format, which is shown below in figure 2.2.1.1. In packed YUV color format every other byte is Y and every fourth byte is either U or V in the format of Y U Y V Y U Y V. X OXOXOXO X OXOXOXO X OXOXOXO X=U/V component, O= Y component. Figure 2.2.1.1: 4:2:2 Subsample YUV Format 2.2.2 8x8 Image Blocks All subsequent steps of JPEG algorithm are performed on individual 8x8 blocks of the image in a sequential manner (one block after another). Thus, the compression of color image is achieved by compressing multiple gray scale images of 8x8 blocks individually. 2.2.3 Discrete Cosine Transform (DCT) Discrete Cosine transform is the most important step of the JPEG lossy image compression algorithm. DCT is applied on the on each 8x8 blocks of the image. The main objective of the DCT is to convert the spatial image representation into a frequency domain representation such that it is more convenient for discarding the high frequency components from the image matrix. DCT catch the spatial redundancy from the image and wraps up the signal energy into as few DCT coefficients as possible. 6 The mathematical formulae of DCT for the block size of N x N is given by C(u,v)= α(u) α(v) Σ x=0 to N-1 Σ y=0 to N-1 f(x,y) cos[(2x+1)μπ / 2N] cos[(2y+1)vπ / 2N] Where u,v=0,1,2,….N-1 α(u) = α(v) = (1/N)1/2 where u,v = 0 α(u) = α(v) = (2/N)1/2 where u,v = 1,2,3…N-1……………………………………….(2.1) The original samples from image can be recomputed again from DCT coefficients by applying the Inverse Discrete Cosine Transform (IDCT). The mathematical formulae of IDCT for the block size of N X N is given by f(x,y)= Σ u=0 to N-1 Σ v=0 to N-1 α(u) α(v) C(u,v) cos[(2x+1)μπ / 2N] cos[(2y+1)vπ / 2N] Where x,y=0,1,2,….N-1 α(u) = α(v) = (1/N)1/2 where u,v = 0 α(u) = α(v) = (2/N)1/2 where u,v = 1,2,3…N-1……………………………………… (2.2) JPEG standard uses block size of 8x8 and N=8 in above equations. DCT takes 64 point input signals and produce the unique set of the 64 two-dimensional basis signal amplitudes, which are also known as the DCT coefficients. The values of these DCT coefficients depend on the particular 64-point input signal. In the resulting 8x8 matrix after the DCT, the coefficient with zero frequency ([0,0] coefficient) is called DC coefficients and all other 63 coefficients are called AC coefficients. The DC term is situated at the left top of the resulting matrix. The sample input block of size 8x8 from the grey scale image and the resulting block after DCT are shown below in table 2.3.1 and table 2.3.2 respectively. 7 Table 2.3.1: Input Image Pixel Matrix of Size 8x8 140 144 152 168 162 147 136 148 144 152 155 145 148 167 156 155 147 140 136 156 156 140 123 136 140 147 167 160 148 155 167 155 140 140 163 152 140 155 162 152 155 148 162 155 136 140 144 147 179 167 152 136 147 136 140 147 175 179 172 160 162 162 147 136 Table 2.3.2: Output DCT Coefficient Matrix 186 21 -10 -8 -3 4 9 0 -18 -34 -24 -5 10 -2 1 -8 15 26 -2 14 8 -18 -3 -2 -9 -9 6 -15 1 8 4 2 23 -11 -18 -8 -11 8 -1 1 -9 11 3 -3 18 -4 -7 4 -14 14 -20 -3 18 1 -1 -6 19 7 -1 8 15 -7 -2 0 The main advantage of DCT lies in its property of aggregating the most of the signal energy in the low frequency part of the resulting DCT coefficient matrix. As shown in table 2.3.2, the magnitudes of the AC coefficients decrease with the increase in the frequencies. As information stored in high frequency coefficients cannot be detected by human eye, they can be discarded without compromising the quality of the image in the subsequent stages of the JPEG compression. 8 2.2.4 Quantization Quantization is done by dividing each of the DCT coefficients by a quantization coefficient value and then rounding off the resulting value to an integer. Higher quantization coefficient values produce more compact output data but the quality of the image degrades because of the DCT values are represented less accurately. The high frequency DCT coefficients are generally quantized more heavily than the low frequency coefficients as they contribute little to the representation of the graphical image and cannot be perceived by the human eyes. The quantum value is the indicator of the step size needed for the particular element in the compressed form of the picture with the values ranging from 1 to 255. The mathematical formulae of the Quantization and Dequantization steps are given by Quantized Value (u, v) = DCT (u, v)/ Quantum (u, v) Rounded to the nearest zero…………...……………………………………………...(2.3) Dequantization formulae: DCT (u, v) = Quantized (u, v) * Quantum (u, v)……………………………………..(2.4) Quantization is the major source of loosiness of the information in the JPEG based encoders. Large quantum coefficient values will yield excellent compression ratios but poor picture quality due to the higher probability of errors in the DCT output during the Dequantization. By choosing low quantization values, the compression ratios will be 9 modest but the image quality will be higher. This offers the users of JPEG great flexibility of choosing the picture quality based on imaging requirements and storage capacity. A sample quantization matrix is shown in table 2.4.1 and the output matrix after quantization is shown in table 2.4.3, while the input matrix before quantization is shown in table 2.4.2. Table 2.4.1: Sample Matrix of Quantization Coefficients 3 5 7 9 11 13 15 17 5 7 9 11 13 15 17 19 7 9 11 13 15 17 19 21 9 11 13 15 17 19 21 23 11 13 15 17 19 21 23 25 13 15 17 19 21 23 25 27 15 17 19 21 23 25 27 29 17 19 21 23 25 27 29 31 0 4 -5 -2 -2 -2 3 1 2 2 5 0 5 0 -1 3 0 0 0 0 0 0 0 0 Table 2.4.2: DCT Matrix Before Quantization 92 -39 -84 -52 -86 -62 -17 -54 3 -58 62 -36 -40 65 14 32 -9 12 1 -10 49 -12 -36 -9 -7 17 -18 14 -7 -2 17 -9 3 -2 3 -10 17 3 -11 22 -1 2 4 4 -6 -8 3 0 Table 2.4.3: DCT Matrix After Dequantization 90 -35 -84 -45 0 -56 54 -33 -7 -9 0 0 0 11 -13 0 0 0 0 0 0 0 0 0 10 -77 -52 -15 -51 -39 60 0 19 45 0 -19 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 The DC coefficient is 92 in the DCT matrix as shown in table 2.4.2 and it is divided by the quantization value of 3 and then rounded off to the nearest integer value of 30. At the decoder, 30 is multiplied by the quantization coefficient value of 3 to produce the dequantization-coefficient value of 90 as shown in table 2.4.3. Similarly, most of high frequency DCT coefficients are rounded off to zero after the quantization step. The DC and high-energy AC coefficients may have been modified by small values but these changes are barely noticeable by Human eyes and the image can be compressed as much as 50 to 60 % of the original image size after this step. 2.2.5 ZigZag Sequence After the quantization step of JPEG algorithm, the nature of the quantized DCT coefficients and the random occurrence of zeros in the high frequency coefficients lead to the further need of the compression with the use of the lossless encoding. DC and AC coefficients are treated in different manner during the zigzag step. Due to the strong spatial correlation between the DC coefficients of consecutive 8x8 blocks, they are encoded as the difference between them and their counterparts in the previous blocks as shown in figure 2.5.1. This step takes advantage from the fact that DC coefficients represent a significant fraction of total image energy. 11 Figure 2.5.1: Differential DC Encoding All 63 AC coefficients are scanned by traversing the quantized 8x8 array in a zigzag fashion as shown in the figure 2.5.2. This zigzag scan traverse through the DCT coefficients in the increasing order of their spatial frequency. Scanning in this order results in a bit stream that contains a large number of trailing zeros as there is a high probability of the high frequency AC coefficients being zeros after quantization. Thus, the resulting bit stream will have large numbers of consecutive zeros or “Runs of Zeros” which makes it suitable for Run Length Encoding. Since more than 60% of the DCT coefficients are zero after quantization for many real life images, Run Length Encoding gives an opportunity of achieving very high compression ratio. Figure 2.5.2: Zigzag Scan Procedure 12 2.2.6 Run Length Encoding Run length-encoding works best if the resulting bit stream after zigzag scan has large numbers of “Runs of Zeros”. If the consecutive occurrence of the zeros in the bit stream after zigzag is few, the Run Length Encoding yields poor compression ratios. Thus, RLE will produce high compression ratio for the bit 5,0,0,0,0,0,0,0,6,3,1,7,0,0,0,0,0,0,0. However, the bit stream like stream such as 9,0,4,0,8,0,1,0,3,0,5,0,7,0,1,0,0,1 will result in poor compression ratio after RLE. Many variation of RLE exists in today’s world, but this project mainly emphasis on the simple and efficient form of it as described in the next paragraph. The main idea underlying RLE algorithm is to replace the Runs of the any data (“000000000”) with the first two repeating bytes of the original data and then a counter (“007”) to count the continuous zeros in the resulting RLE encoded stream and the remaining repeating zero value bytes except the first two are discarder in the resulting RLE encoded bit stream. Thus, 16,0,0,0,0,0,0,0,0,8,9,0,0,0,0,0,0,0,0,0 16,0,0,6,8,9,0,0,7 after RLE encoding. will the result data into stream the after compact zigzag stream of The design flows for the RLE encoder and decoder are shown below in figure 2.6.1 and 2.6.2 and are explained in the next paragraph. 13 Get Two Bytes No Yes Are The two bytes Equal? 1-Output the first byte 2-Put second byte as first 3- get next byte and set it as the second 4- update pointer to the next byte 1-Output both of them 2-Count number of repetitions 3- Output the value of the repetition counter 4-Update the pointer to the next byte 5-Get the next two bytes Figure 2.6.1: RLE Encoder Design Flow For RLE encoding, the first and second bytes of the Zigzag sequence are fetched first and then they are compared with each other. If they are equal, both of them are output to the RLE encoded stream followed by the repetition counter, which suggests the total number of repetitions. This step is repeated once again on the next set of bytes. If the two bytes are not equal, then we output the first byte, compare the second byte with the next byte, and repeat the entire process again. 14 Get Two Bytes No Yes Are The two bytes Equal? 1-Output the first byte 2-Put second byte as first 3- get next byte and set is as the second 4- update pointer to the next byte 1- Output both the bytes 2-Get the next byte (repetition counter ) 3- Output “Repetition counter times” the last byte 4-Get the next two bytes Figure 2.6.2: RLE Decoder Design Flow For RLE decoding, the first byte from the RLE encoded stream is fetched first. Then the next byte is fetched and then compared with the previous byte. If both of them are same, the next byte representing the repetition counter is fetched. It is then, will be replaced by the “repetition counter” times previous byte to the decoded stream. If the first two bytes are not equal, both of them will be output to the decoded stream directly. Such process is repeated continuously until the end of the RLE encoded string is reached. 15 Chapter 3 TMS320DM6437 DVDP PLATFORM OVERVIEW AND IMPLEMENTATION OF JPEG IMANE COMPRESSION ALGORITHM Texas Instrument’s TMS320DM6437 DVDP platform for digital multimedia application offers high quality video ports and peripherals with industry leading performance proven with the TMS320C64x family of DSP processor for many of the key image and video processing industry applications such as automotive, image and video codec, video surveillance and set top boxes. It also offers faster time to market advantage through its ready to use software applications and reduced system cost for image and video processing application through system on chip integration of the important audio/video and connectivity peripherals. As DM64X generation provides high level versatility and flexibility to the end users. For example, one video application can be easily upgraded from MPEG 2 codec to H.264 codec by customers to provide improved channel capacity and hard drive storage capacity. Its video, memory and network interfaces combined with IDE tool offer the most flexible way to develop the real world video/audio and image applications. It also provides additional headroom for the complex video and image algorithms running on the DSP processor. 3.1 TMS320DM6437 DVDP OVERVIEW The DM6437 EVM is a PCI based or standalone development platform that enables users to evaluate and develop applications for the Texas Instrument’s DaVinci 16 TM processor family. TheDM6437 EVM platform is a package of all common features required to develop industry specific image and video applications. Software development component such as Code Composer Studio v3.3 provides Integrated Development Platform (IDE) tool for implementing and debugging different image and video applications. TMS320DM6437 DVDP platform has the following key features: 1. One Texas Instruments DM6437 processor operating up to 600 Mhz. 2. 4 video DAC outputs-supporting component, RGB and composite formats 3. 128 Mbytes of DDR2 DRAM 4. 16 Mbyte of non-volatile Flash memory, 64 Mbyte NAND Flash, 2 Mbytes SRAM 5. 80 KB L1D,32 KB L1P cache/SRAM and 128 KB L2 cache SRAM memory 6. Two 32-bit,133-MHz extended memory interfaces(EMIFs) 7. Single voltage power supply (+5V) Functional block diagram of TMS320DM6437 DVDP platform is shown in figure 3.1.1. DM6437 interfaces to on-board peripheral devices through integrated device interfaces and an 8-bit wide EMIF bus. The EMIF bus can be connected to the Flash, SRAM and NAND with the use of selection jumpers. Video streams are interfaced to the DM6437 processor through on chip video decoders and encoders. Functions for displaying video stream on screen can be implemented on the DM6437 processor using Code Composer Studio Tool. The board has Standard Video In and Standard Video Out 17 ports for the video input and output. It also has a USB EMU port in order to connect the board to the Code Composer Studio application running on the Host PC. Figure 3.1.1: Functional Block Diagram of TMS320DM6437 DVDP Platform For this project, the input video frame was captured on the Board by connecting the video output port of digital camera to the Standard Video Input port of the board via RCA type connector. The JPEG algorithm was applied on one particular frame and resulting frame after the compression or decompression was displayed on television by connecting the Standard Video Output port of the board to the Video In port of television. 3.2 VPFE and VPBE Drivers on the TMS320DM6437 Platform Figure 3.2.1 shows the VPSS (Video Processing Sub System) block level diagram, which mainly consists of VPFE and VPBE subsystems. 18 Figure 3.2.1: VPSS Block Diagram The VPSS provides an input interface Video Processing Front End (VPFE) on the DM6437 platform for external imaging peripherals such as image sensors, video decoders and digital camera in order to capture the image and an output interface Video Processing Back End (VPBE) for display devices such as digital LCD panels and television in order to display the processed image. The common buffer memory and direct memory access (DMA) controls ensure the efficient use of the DDR2 memory controller burst bandwidth and other peripherals. The shared buffer memory logic provides primary source/sink to all of VPFE and VPBE modules. 3.2.1 VPFE (Video Processing Front End) The VPFE block is comprised of charge-coupled device (CCD) controller (CCDC), resizer, preview, hardware 3A statistic generator (H3A) and histogram. The 19 main task of CCDC is to accept unprocessed image/video from a CMOS or CCD sensor. The CCDC also accepts YUV video data from video decoder devices. The captured image can be resized to the 720x480 pixels per frame using Resizer. Then it is submitted to the Preview Engine Pipe for the further processing. The H3A module supports the control loops for Auto Focus and Auto Exposure by gathering metrics for the image/video data, which are used to adjust the various tuning parameters for the further image/video processing. Histogram allows luminance intensity distribution of the pixels of the image/frame to be represented. 3.2.2 VPBE (Video Processing Back End) VPBE is responsible for displaying the processed image on different display devices such as TV, LCD or HDTV. The VPBE block contains the on-screen display (OSD) module and the video encoder (VENC) module. The OSD graphic accelerator is responsible for displaying images in various formats on the output devices. The OSD can be programmed via control and parameter registers. It provides the flexibility of resizing images to either National Television System committee (NTSC) format or PAL format (640x480 to 720x576). VENC converts the display frame from the OSD into the correctly formatted, desired output signals in order to interface it to different display devices. VENC is comprised of analog video encoder, digital LCD controller and timing generator. Analog video encoder produces the required signals to interface to the NTSC/PAL television systems. Timing generator generates timing signals required for various analog and/or digital output display devices. 20 3.3 JPEG Implementation on TMS320DM6437 The block diagram of different steps for implementing JPEG image compression algorithm on TMS320DM6437 platform is shown in figure 3.3.1 on next page. 3.3.1 Capturing Image on TMS320DM6437 An image frame can be captured on the TMS320DM6437 platform from the standard video input device like digital camera by connecting the Video Out port of the camera to the Standard Video In port of the board. The VPFE standard library function FVID_exchange is used in C code to capture the image. This function grabs an image frame, stores it in the internal board memory buffer and assigns a buffer pointer to the start location of the memory. This facilitates further processing of the image. 3.3.2 Converting Image from YUV Format to Gray Scale An image in YUV format can be converted to grey scale format by selectively discarding the blue and red difference chrominance components. Human eyes are more sensitive to the luminance component than that of chrominance. The image captured via FVID_exchange function is in the 4:2:2 YUV format, which is explained in the section 2.2.1. In this project, only Y components were selected for further processing. U and V components were discarded for effectively converting the YUV image to the Grey scale. 21 CAPTURING IMAGE THROUGH DIGITAL CAMERA IN YUV FORMAT CONVERTING IMAGE FROM YUV TO GREY SCALE FORMAT APPLYING DCT(Discrete Cosine Transform) ON 8X8 GREY SCALE BLOCKS APPLYING IDCT(inverse Discrete Cosine Transform) ON 8X8 BLOCKS APPLYING QUANTIZATION ON 8*8 BLOCKS OF IMAGE APPLYING DEQUANTIZATION ON 8*8 BLOCKS OF IMAGE SCANNING THE 8X8 BLOCKS OF IMAGE INTO ZIGZAG SEQUENCE SCANNING THE 8X8 BLOCKS OF IMAGE INTO ZIGZAG SEQUENCE CODING 8X8 BLOCKS OF THE IMAGE USING RUN LENGTH ENCODING CODING 8X8 BLOCKS OF THE IMAGE USING RUN LENGTH ENCODING DISPLAYING THE RECONSTRUCTED IMAGE ON THE TELEVISION SCREEN Figure: 3.3.2: Steps for Implementing JPEG Algorithm on TMS320DM6437 Platform 3.3.3 Applying DCT and IDCT on 8x8 Grey Scale Blocks After converting the entire image from YUV format to Grey Scale format, the DCT is applied sequentially on individual 8X8 grey scale block in order to convert the image from spatial domain into the frequency domain. The equation for DCT explained in the section 2.2.3 can be used to apply the DCT on 8x8 blocks but they require lots of mathematical computation and hence higher execution time, which in turn degrades the 22 performance of the system. Therefore, I opted for another popular approach to implement DCT in this project, which is described in next paragraph. In this approach, the image matrix of size 8x8 is first multiplied with cosine transform matrix shown in table 3.3.3.1 and then with its transposed matrix of same size shown in table 3.3.3.2 in order to achieve the DCT 8x8 matrix. The transpose matrix is generated by transposing rows and columns of the cosine transform matrix. Thus, every 8x8 block of image is multiplied by cosine transform matrix and then by its transposed matrix in order to convert the entire image from spatial domain to frequency domain. The original image captured on TMS320DM6437 platform and the image displayed on television after applying DCT are shown in figure 3.3.3.1 and 3.3.3.2 respectively. Table 3.3.3.1: Original Cosine Coefficient Matrix 0.353553 0.490393 0.461978 0.414818 0.353694 0.277992 0.191618 0.097887 0.353553 0.415818 0.191618 -0.097106 -0.353131 -0.490246 -0.462282 -0.278653 0.353553 0.277992 -0.190882 -0.490246 -0.354256 0.096324 0.461366 0.4167 0.353553 0.097887 -0.461673 -0.278653 0.352567 0.4167 -0.189409 -0.490862 0.353553 -0.097106 -0.462282 0.276667 0.354819 -0.414486 -0.193822 0.489771 0.353553 -0.277329 -0.192353 0.49071 -0.352001 -0.100228 0.463187 -0.413593 0.353553 -0.415375 0.190145 0.099448 -0.355378 0.491013 -0.46044 0.274008 0.353553 -0.490246 0.461366 -0.414486 0.351435 -0.274673 0.187195 -0.092414 Table 3.3.3.2: Transpose Cosine Coefficient Matrix 0.353553 0.353553 0.353553 0.353553 0.353694 0.353553 0.353553 0.353553 0.490393 0.415818 0.277992 0.097887 -0.097106 -0.277329 -0.415375 -0.490246 0.461978 0.191618 -0.190882 -0.461673 -0.462282 -0.192353 0.190145 0.461366 0.414818 -0.097106 -0.490246 -0.278653 0.276667 0.49071 0.099448 -0.414486 0.353694 -0.353131 -0.354256 0.352567 0.354819 -0.352001 -0.355378 0.351435 0.277992 -0.490246 0.096324 0.4167 -0.414486 -0.100228 0.491013 -0.274673 0.191618 -0.462282 0.461366 -0.189409 -0.193822 0.463187 -0.46044 0.187195 0.097887 -0.278653 0.4167 -0.490862 0.489771 -0.413593 0.274008 -0.092414 23 Figure 3.3.3.1: Original Image Captured Through Camera Figure 3.3.3.2: Discrete Cosine Transformed Image In order to verify the correctness of the implementation of DCT on TMS320DM6437 platform, Inverse Discrete Cosine Transform was applied on all 8x8 blocks of the image. The same approach for DCT was used to implement IDCT but the order of the multiplications was reversed. Thus, each 8x8 DCT matrix was first multiplied by the transposed cosine matrix shown in table 3.3.3.2 and then bye the 24 original cosine transform matrix of the same size shown in 3.3.3.1. The image was converted back to the spatial domain from the frequency domain after the application of IDCT. The original image was regenerated and successfully displayed on television, which is shown in the figure 3.3.3.3. The performance analysis of DCT and IDCT implementation is shown in table 3.3.3.3, which includes average code size, execution time and total number of clock cycles. Figure 3.3.3.3: Inverse Discrete Cosine Transformed Image Table 3.3.3.3: Performance Profile for The DCT Implementation Average Average Average number of clock code time(seconds) cycles size(bytes) 0.016891201 10114492 155744 25 3.3.4 Implementation of Quantization and Dequantization on the TMS320DM6437 The image matrix after DCT occupies more space than the original image matrix. The main goal of quantization is to discard the excessive amount of information and reduce the number of bits needed to store the output values. This is done by dividing each of the output DCT coefficients by a quantization value and then rounding the resulting value to an integer. The high frequency coefficients are quantized more heavily than the DC and some of the high-energy AC coefficients. Quantization takes advantage of the fact that human eye is not sensitive to the change in the high frequency coefficients and hence the minor change in the image quality goes unnoticed. Every 8x8 DCT matrix was divided by the quantized matrix of the same size in order to quantize the high frequency DCT coefficients to zero. The quantized matrix with the quality factor of two was used for this project and it is shown in table 3.3.4.1. By dividing every 8x8 matrix with quantization matrix, most of the high frequency DCT coefficients with pixel values smaller than corresponding quantization matrix values were reduced to zeros. This step contributes most to the loosiness of the JPEG algorithm as the finer detail of the image is lost due to these truncations of the high frequency DCT coefficients to zeros. The original values cannot be restored back in the decompression step. The image captured after quantization is shown in figure 3.3.4.1. Table 3.3.4.1: Quantization Matrix for Quality Factor q =2 3 5 7 9 11 5 7 9 11 13 7 9 11 13 15 9 11 13 15 17 11 13 15 17 19 13 15 17 19 21 15 17 19 21 23 17 19 21 23 25 26 13 15 17 15 17 19 17 19 21 19 21 23 21 23 25 23 25 27 25 27 29 27 29 31 Figure 3.3.4.1: Quantized DCT Image The Dequantization function was also written in C language and was implemented on the TMS320DM6437 platform to test the correctness of Quantization function. The image reconstructed after Dequantization is shown in figure 3.3.4.2. The performance profile of the quantization and Dequantization modules are depicted in table 3.4.4.1. Table 3.3.4.1: Performance Profile for The Quantization Implementation Average Average Average number of clock code time(seconds) cycles size(bytes) 0.02077246 1243860 155648 27 Figure 3.3.4.2: Reconstructed Image After Dequantization 3.3.5 Zigzag and Inverse zigzag sequencing of the image pixels After the quantization step, every 8x8 matrix are scanned in the zigzag manner (in order of their frequencies) one after another in a way that it will produce the output data stream consisting of many consecutive zeros. These zeros are discarded in the Run Length Encoding in order to store the entire image in as few storage space as possible. The image after zigzag and image reconstructed after applying inverse zigzag function are shown in figure 3.3.5.1 and figure 3.3.5.2 respectively. The performance profile for zigzag and inverse zigzag functions is shown in table 3.3.5.1. 28 Figure 3.3.5.1: Captured Image After Zigzag Figure 3.3.5.2: Reconstructed Image After Inverse Zigzag Table 3.3.5.1: Performance Profile for The Zigzag Implementation Average Average Average number of clock code time(seconds) cycles size(bytes) 0.0333094 19945789 15984 29 3.3.6 Run Length Encoding and Decoding Implementation on TMS320DM6437 The Run Length Encoding step follows the zigzag scan step in the JPEG compression algorithm. One of the standard data compression algorithms such as RLE (Run Length Encoding) or Huffman Encoding can be applied to the resulting zigzag sequences. I opted for the Rung Length Encoding for this project for the simplicity of the computation. The flow charts for performing Run Length Encoding and Decoding on TMS320DM6437 platform are shown in figures 3.3.6.1 and 3.3.6.2 I was able to encode the zigzag sequences using RLE encoder and then successfully implementing RLE decoding the on TMS320DM6437 platform. The image was reconstructed and displayed on television after performing the JPEG decompressing steps including RLE decoding, Inverse zigzagging , Dequantization and Inverse DCT. This proved the correctness of my JPEG compression implementation. The reconstructed image after applying the JPEG decompression is shown in figure 3.3.6.3 and the performance profile of RLE and Inverse RLE implementation is shown in table 3.3.6.1. 30 START rle6[0]=zigzag[0] j = 1,i=1 cnt2=0; No rle6[j]=zigzag[i] Enccout++, j++ Cnt2,counter3=0 i++ No counter3=cnt2, i++ Yes Is Zigzag[i1]=zigzag[i]? Cnt2++ Yes Is cnt2<2 Is Counter3 == 2 No Yes Rle6[j] = zigzag[i] Count3=cnt2 enccout++, j++ , i++ Rle6[j] = 0 Counter3=o enccout++, j++ , i++ Rle6[j] = counter3-2 Rle6[j+1] = zigzag[i] Cnt2=0; counter3=o enccout++++, j++++ , i++ Yes Is Cnt2 > 2 Figure: 3.3.6.1: Flow Chart for RLE Encoder Table 3.3.6.1: Performance Profile for The RLE Implementation Average Average Average number of clock code time(seconds) cycles size(bytes) 0.0415442 24876786 15984 31 Start i=1,m=0;hit=0; invrle[0]=rle9[0]; No invrle[i+m] = rle9[i]; hit =0; i++; Yes rle9[i]== rle9[i1]) && hit == 0 counter = rle9[i+1]; for(j=0;j <= counter; j++) invrle[i+j+m] = rle9[i]; m += (counter -1 ); hit = 1;counter = 0; i++; Figure: 3.3.6.2: Flow Chart for RLE Decoder Figure 3.3.6.3: Reconstructed Image After Inverse RLE 32 Chapter 4 MODELING OF DCT COEFFICIENTS USING KS GOODNESS TO FIT TEST With the increased use of internet and multimedia systems, JPEG standard implemented in this project is gaining widespread popularity for lossy compression of still digital images and videos. Images are divided into small blocks of 8x8 or 16x16 size and each block is then subjected to DCT and then quantization. There have been keen interests in understanding the distribution profile of these coefficients as such knowledge would be of great use for designing efficient quantizers and for noise mitigation. DCT coefficient histograms of one sample image used in this project are shown in figure 4.1. The histogram at the top left corner of the image matrix is related to DC coefficient and all the remaining coefficients are related to AC coefficients. Figure:4.1: Histograms of the DCT Coefficients Distribution of these DCT coefficients has been studied and researched extensively in last decade. Many researchers differ in their opinions. The DC coefficients 33 are believed to have Gaussian distribution as per some researchers while Rayleigh or Gamma distribution according to others. Some researchers conclude that the Laplacian Distribution best approximates AC coefficients while others hold the opinion that they are best approximated by either Gamma or Gaussian distribution. The different assumptions have led me to perform KS goodness to fit test and to find out the best distribution of the images I used that accurately approximates the statistics of the DC and AC coefficients after quantization. The Gaussian, Gamma, Laplace and Rayleigh distributions are considered in this project for KS test. These distributions are described first and then methodology used to perform KS test is discussed later. The last section of the chapter contains the MATLAB graphs of the KS test results. 4.1 Introduction to Different Types of Probability Distribution Functions A probability distribution function of a set of data (DCT Coefficients) is a function that describes the relative likelihood for this set of data to occur at a given point in the given limits. Gaussian, Laplace, Gamma and Rayleigh distributions function are described in this section with the equations for Probability and cumulative distribution functions and parameter estimations. 4.1.1 Gaussian Probability Distribution Function Gaussian probability distribution function is given by the following equation where x is the random set of data, μ is the mean of the given data set and σ is the variance of the data set: 34 fX(x) = 1/ (2πσ)1/2 exp [- ( (x-u) 2 / 2 σ2] , -α < x < α…………………………(4.1) The graphical representation of the Gaussian PDF is shown in figure 4.1.1.1. Any increase in the value of the variance σ will result in the increase in the width of the PFD and decrease in the steepness. The formulas for calculating mean and variance are given below where n is the total number of sample data and x1, x2…xn are given data. [11] Mean μ = [(x1+x2+x3+…...+xn) /n]……………………………………….(4.2) Variance σ = [((x1- μ)2 +(x2- μ)2 +………+ (xn- μ)2)/n]………….………….(4.3) The Gaussian cumulative distribution function denotes the probability of the given data to fall within a certain range with the values between -1 and 1. It is given by: F(x; u, σ2) = Ф ((x-u) /σ) = ½ [1+ erf ((x-u) / σ 21/2)], x Ɛ R …...…………….(4.4) Figure: 4.1.1.1: Graphical Representation of Gaussian PDF 4.1.2 Laplacian Probability Distribution Function 35 Laplacian PDF is a method of illustrating the variance of the data occurrence around the mean. Laplacian PDF is also called the Double Exponential function as its shape is like two exponential function distributions connected back to back to each other. It is given by the function as shown below: f(x| Θ, λ) = (1/2 λ) exp (- (|x- Θ |) / λ)...…………………….………………..(4.5) Where Θ is the location (Mean) which determines the occurrence of the peak and Lambda (λ) is the scale parameter, which determines the width of the LPDF. Width is proportional to λ. The comparison between Two PDFS are shown in figure 4.1.2.1, where for the same mean position the LPDF is symmetric and has higher spike as well as thicker tail than that of the GPDF. Note that mean (μ) is 4 and variance (σ) is 2 for GPDF curve in figure 4.2.1, while mean (Θ) and scale parameter (λ) are 4 and 8 respectively for LPDF curve. Figure 4.2.1: Graphical Representation of Laplace PDF The formulae for estimating the Scale parameter λ is shown below: A = (0.5 * Variance) 1/2 ………………………………………………………………..(4.6) 36 The Laplacian Cumulative Distribution function is given below: F (x| Θ, λ) = 0.5 exp (- (|x- Θ |) / λ), when x<= Θ. = 1- 0.5 exp (- (| Θ-x |) / λ), when x> Θ.……………………..……(4.7) 4.1.3 Gamma Probability Distribution Function The Gamma distribution function is defined in terms of two parameters, Scale parameter θ and shape parameter k. It is given by the equation as shown below and both θ and k are positive values: f(x;k, θ)= xk-1 (e (-x / θ ) / θk Γ(k) ) for x>0 and k, θ>0…………………….…(4.8) The graphical representation of Gamma PDF is shown figure 4.1.3.1. The scale parameter θ determines the width of the distribution, which increases by decreasing the value of θ. k has the effect on stretching or compressing the range of the Gamma distributions and hence it controls the shape of the distribution. A Gamma distribution with k = 1 is called the standard Gamma distribution and is shown in figure 4.1.3.1. The equations for the parameter estimation of the Gamma PDF are shown below: Scale parameter θ= (Variance/Mean); …………………………...…………..(4.9) Shape parameter k=( (Mean)1/2 * (Variance))……………………………….(4.10) 37 Figure: 4.1.3.1 The Graphical Representation of Gamma Distribution The Gamma Cumulative distribution function is given by the following equation: F( x:k, θ )= ʃ f(u; k, θ) du = (ɤ (k, x/θ) / Γ(k))………...…………………….(4.11) 4.1.4 Rayleigh Probability Distribution Function In the probability theory and statistics, Rayleigh distribution is a continuous distribution, which can be defined in terms of the Scale parameter σ with the equation shown below. P(x) = x/ σ2 e ^ (-x2 / 2 σ2)…………...………………………………………..(4.12) The graphical representation of the Rayleigh Distribution function is shown below in the Figure 4.1.4.1 with the positive values of σ. The scale parameter σ determines the steepness of the distribution as shown in the figure. The width of the distribution increases with the increase in the value of the σ. 38 Figure 4.1.4.1: The Graphical Representation of Rayleigh Distribution The equation for the parameter estimation for the scale parameter σ is shown below: Scale parameter σ= ( (2/ π)1/2 * Mean)……………………………………................(4.13) The Rayleigh cumulative distribution function is given by the following equation for the positive values of σ: F(X, σ) = 1- e ^ (-x2 / 2 σ2)................................................................................(4.14) 4.2 Methodology for Applying KS Goodness to Fit Test The Kolmogorov-smirnov (KS) test is one of the most popular Goodness to fit tests. KS test can be performed to measure the test statistics distance for different distribution functions such as GPDF, LPDF and Gamma and Rayleigh. Test statistic is 39 the distance (variation) measured between the Empirical Cumulative distribution and actual Cumulative distribution. The test statistic distance of different Distributions such as Gaussian, Gamma, Laplace and Rayleigh are measured and compare against each other for individual DCT coefficients in order to determine the best-fitted distribution. The distribution with the lowest test statistic values is called the best fitted distribution for the given set of DCT coefficients. The most important information of the image is contained in the DC coefficient [(0.0)] and some of the high-energy AC coefficients [(0, 1), (0, 2), (1, 0) and (2, 0)]. Therefore I have preferred to apply the KS test on five different standard test images for finding out the best distribution to accurately model the coefficients (0, 0), (0, 1) and (0, 2) in this project. For the given set of data, the KS test compares the empirical distribution function Fx(.) to the given distribution function F(.). The empirical Cumulative distribution function is given by the following equation where X= (x1, x2,x3…..,Xn) is the given set of data and X(n), n=1,2,3,4,5……M is the order statistic of the data X. M is the total number of blocks in the Image matrix. FX(z) = 0 if z < x(1) = n/M if x(n) <= z < x(n+1) , n= 1,2,…..M-1 =1, z >= x(M) …………………………………………………………(4.15) I have used standard images with the 256*256 blocks in this project. Depending on the block size N the total number of the blocks M can be given by, M=(256/N) 2 . The given cumulative distribution functions for Gaussian, Gamma, Laplace and Rayleigh 40 distributions can be calculated by using the CDF equation and different parameter estimation equations described in section 4.1.1. The KS statistic distance t is then defined by the following equation: t= max | FX (xi) – F(xi) | , where i= 1,2,….M ………………………….……(4.16) MATLAB code was written to implement the KS test methodology and to apply it on five different standard test images for the DC coefficient (0, 0) and two of the high energy AC coefficients (0, 1) and (0, 2). The MATLAB code is included in Appendix B. The resulting graphs of the KS test statistics for different distributions were created using MATLAB figure tools and they are displayed and analyzed in the next section. 4.3 Analysis of the KS Test Results for Five Different Images The KS test was applied on five different standard test images including- couple, girl, dog, Ariel and woman. These images are shown in figure 4.3.1. The first three images (couple, girl and dog) are standard real life digital images, while the last two images (Ariel and woman) are busy images with many fine details. The KS Statistics measured for DCT coefficients (0, 0), (0, 1) and (0, 2) for all these images are shown in the bar charts in figure 4.3.2. The DC and first two of the high-energy AC coefficients were selected for applying KS test as they contain most useful information of the images. 41 Couple Dog Girl Ariel Woman Figure 4.3.1: Standard Test Images Used for This Project It can be seen from figure 4.3.2(a) and 4.3.2(b) that the Gaussian KS statistic distance is smaller than all other KS statistic distance for the DC coefficients (0,0) as well as two of the high energy AC coefficients (0,1) and (0,2) . Therefore, it can be concluded as the distribution of the DCT coefficients (Both DC and AC) for the busy images such as Ariel and Woman is Gaussian in nature. The Gamma KS statistic distance is highest for these busy images than any other methods and hence it proves to 42 be poor choice for accurately modeling DCT coefficients for busy images. Thus, Gaussian PDF can best approximate all of DCT coefficients for busy images. KS TEST DATA FOR C00 IMAGE: ARIEL.JPG KS TEST DATA FOR C01 IMAGE: ARIEL.JPG KS TEST DATA FOR C02 IMAGE: ARIEL.JPG 0.35 KS STATISTIC VALUES KS STATISTIC VALUES 0.4 0.3 0.2 0.1 0.3 KS STATISTIC VALUES 0.3 0.5 0 0.35 0.25 0.2 0.15 0.1 0.05 Gaussian Laplace Gamma TYPES OF CUMULATIVE FUNCTIONS 0 Rayleigh 0.25 0.2 0.15 0.1 0.05 Gaussian Laplace Gamma TYPES OF CUMULATIVE FUNCTIONS 0 Rayleigh Gaussian Laplace Gamma TYPES OF CUMULATIVE FUNCTIONS Rayleigh Figure 4.3.2(a): KS Statistic Results for Image Ariel KS TEST DATA FOR C00 IMAGE: WOMAN.PNG KS TEST DATA FOR C01 IMAGE: WOMAN.PNG 0.5 KS TEST DATA FOR C02 IMAGE: WOMAN.PNG 0.35 0.35 0.45 KS STATISTIC VALUES KS STATISTIC VALUES 0.3 0.25 0.2 0.15 0.3 KS STATISTIC VALUES 0.3 0.4 0.35 0.25 0.2 0.15 0.1 0.25 0.2 0.15 0.1 0.1 0.05 0.05 0 Gaussian Laplace Gamma TYPES OF CUMULATIVE FUNCTIONS Rayleigh 0 0.05 Gaussian Laplace Gamma TYPES OF CUMULATIVE FUNCTIONS Rayleigh 0 Gaussian Laplace Gamma TYPES OF CUMULATIVE FUNCTIONS Rayleigh Figure 4.3.2(b) : KS Statistics Results for Image Woman In KS test statistic results for the DCT coefficients of the general real life images shown in the figures 4.3.2(c), 4.3.2(d) and 4.3.2(e) respectively, it is obvious that the Gaussian KS statistic distance for the DC coefficients are smallest for all images. Thus, it can be stated that DC coefficients of most of digital images have Gaussian distribution. However, Laplacian KS statistic distances for the AC coefficients are smaller than that 43 got from the Gaussian and any other distribution functions. Therefore, it is clear that all of AC coefficients of the general real life images can be best approximated by the Laplacian distribution. Again, it is clear from the results that Rayleigh and Gamma distributions are poor choices to approximate the DCT coefficients of almost all above images. KS TEST DATA FOR C01 IMAGE: COUPLE.GIF KS TEST DATA FOR C00 IMAGE: COUPLE.GIF 0.4 0.5 0.4 0.3 0.2 0.1 0.35 0.3 KS STATISTIC VALUES KS STATISTIC VALUES KS STATISTIC VALUES 0.45 0.35 0.6 0 KS TEST DATA FOR C02 IMAGE: COUPLE.GIF 0.4 0.7 0.25 0.2 0.15 0.1 0.05 0 Laplace Gamma TYPES OF CUMULATIVE FUNCTIONS 0.2 0.15 0.1 0.05 Gaussian 0.3 0.25 Gaussian Rayleigh Laplace Gamma TYPES OF CUMULATIVE FUNCTIONS 0 Rayleigh Gaussian Laplace Gamma TYPES OF CUMULATIVE FUNCTIONS Rayleigh Figure: 4.3.2(c): KS Statistics Results for Image couple KS TEST DATA FOR C00 IMAGE: DOG.GIF KS TEST DATA FOR C01 IMAGE: DOG.GIF 0.7 KS TEST DATA FOR C02 IMAGE: DOG.GIF 0.25 0.35 0.6 0.3 0.3 0.2 KS STATISTIC VALUES KS STATISTIC VALUES 0.4 0.15 0.1 0.25 0.2 0.15 0.1 0.05 0.1 0 0.05 Gaussian Laplace Gamma TYPES OF CUMULATIVE FUNCTIONS 0 Rayleigh Gaussian Laplace Gamma TYPES OF CUMULATIVE FUNCTIONS 0 Rayleigh Gaussian Laplace Gamma TYPES OF CUMULATIVE FUNCTIONS Rayleigh Figure 4.3.2(d): KS Statistics Results for Image Dog KS TEST DATA FOR C00 IMAGE: GIRL.JPG KS TEST DATA FOR C01 IMAGE: GIRL.JPG KS STATISTIC VALUES 0.6 0.5 0.4 0.3 0.2 0.1 0 KS TEST DATA FOR C02 IMAGE: GIRL.JPG 0.35 0.4 0.3 0.35 KS STATISTIC VALUES 0.7 KS STATISTIC VALUES KS STATISTIC VALUES 0.2 0.5 0.25 0.2 0.15 0.1 0.05 Gaussian Laplace Gamma TYPES OF CUMULATIVE FUNCTIONS Rayleigh 0 0.3 0.25 0.2 0.15 0.1 0.05 Gaussian Laplace Gamma TYPES OF CUMULATIVE FUNCTIONS Rayleigh 0 Gaussian Laplace Gamma TYPES OF CUMULATIVE FUNCTIONS Figure 4.3.2(e): KS Statistic Results for Image Girl Rayleigh 44 Chapter 5 DESIGN METHODOLOGY OF DEBLOCKING FILTER TO IMPROVE THE QULAITY OF RECONSTRUCTED DIGITAL IMAGES Block-based JPEG compression standards are widely used to compress the images and videos due to its ease of implementation and energy compacting property combined with the low bandwidth and storage. One of the biggest drawbacks of block-based JPEG compression standard is reduction in the quality of the reconstructed images with compared to the original images because of the coarse quantization of the DCT coefficients in the quantization step of the JPEG standard. The coarse quantization of DCT coefficients results in the visible “blocking artifacts” at the boundary of the N X N size blocks as each block of image is processed individually and existing correlation among the block pixels is often neglected in this standard. Many image-filtering approaches have been proposed to smooth out the discontinuities and artifacts at the DCT block boundaries, but this project mainly focuses on the image de-blocking filter. The image de-blocking filter presented in this chapter targets the filtering of the DC-sub band of the image after the DCT and quantization of each individual 8X8 block of the image. A local segmentation technique was developed before applying a simple value-averaging model to the local mask of each 3x3 DCT coefficients of the DC sub band. The 3x3 mask of the DCT coefficients was then split into two segments (segment-0 and segment-1) depending on its pixel value and mask average. The “Do no harm” policy was also developed to discard the filtered values of DCT- coefficients if they fall outside of their quantization interval level. The final filtered values were written back to the 45 quantization matrix and then the following JPEG decompression steps were applied on this filtered quantization matrix. The qualities of the reconstructed images generated with and without using de-blocking filter were compared by means of peak signal to noise ratio (PSNR). 5.1 Introduction to Image Sub Bands and Causes of Image Artifacts This project mainly emphasis on lossy JPEG image compression standard. It is more popular than the Loose-less standard because of the large storage requirements of digital images and video and limited bandwidth availability to transmit them over the internet. They provide very high compression ratios by voluntarily allowing a certain amount of user controlled image information loss. The JPEG compression standard splits the input image into 8x8 pixel blocks and applies DCT to each block individually to generate a series of 64 DCT coefficients, which represents the scaling factor of each of the 64 image primitives represented by the DCT. These 64 image primitives are shown in figure 5.1.1. Figure: 5.1.1: 64Iimage Primitives After Applying DCT 46 If every DCT coefficient from each block is fetched and arranged in the same way that the blocks are arranged in the original image, each DCT coefficient produces an image with the dimensions 1/8 of that of original image. This image is called a “sub band”. Thus, DCT of an image will result in total 64 sub bands. The first sub band is called DC sub band and remaining 63 sub bands are called AC sub bands. The deblocking filter methodology presented in this chapter only filter the DC sub band and method for extracting the DC sub band from the image matrix is shown in the MATLAB code included in Appendix – C. The original image used for this project and its DC sub band are shown in figure 5.1.2. It has been discussed in previous section of this report that the distribution of the DC co-efficient is Gaussian in nature, while that of AC coefficient tends to be Laplacian. Figure: 5.1.2: Original Image Couple.gif and Its DC Sub Band DCT transform is high-energy compact with a minimal loss of the image information. However, most of information loss occurs in the quantization step where each 8X8 block is divided by the quantization matrix and the results are rounded to the 47 nearest of integers. The JPEG algorithm allows user to set a quality parameter in order to control the loss of the information and hence image quality. The formulae for generating the quantization matrix is shown below where “Q” is the quality factor with values ranging from two to hundred. The i and j represent the indices of the image matrix in horizontal and vertical directions respectively. Quantization matrix (i,j)=1+((i+j)* Q) ………………….(5.1) JPEG decoder must first de-quantize and then apply Inverse DCT to reconstruct the image. As the fractional part of the division was lost, the image decoder must now make a choice as to what value the DCT coefficient should be within a known quantization interval. JPEG standard suggests choosing the mid-point of this quantization interval, which is not as precise as that of the original image. Thus, reconstructing DCT coefficients incorrectly results in various image artifacts, which can be categorized as edge artifacts and grid noise. These artifacts can be seen in figure 5.1.3 which is reconstructed image of Couple.gif without using the de-blocking filter. The quality factor Q was set to 25. The higher the value of the quality factor Q, the more coarse the quantization and the more loss of the information of the image are. Thus, high values of Q produce images with worse quality but more compactness. Users of the JPEG standard can set the value of Q as per the quality and bandwidth/storage requirements. 48 Figure: 5.1.3: Original Image on Left and Artifacts Observed in Its Reconstructed Image 5.2 Background And Design Methodology of De-blocking filter Many approaches have been proposed to filter the pixel values of the image in order to smooth out the resulting artifacts and hence to improve the quality of reconstructed images. Some of them filter the sub bands of the image (i.e. DC sub band and few of the high-energy AC sub bands) after applying the DCT, while others filter the pixel values of the reconstructed image. Most of them use basic weighted, valueaveraging filters by averaging the pixel values of a small mask (i.e. 3x3 masks in this project). They take the advantage of the fact that for any pixel in an image, its neighboring pixels have quite similar values. This is true for most of pixels in image except for ones at the edges where the inter pixel correlation is very low. This results in the reconstructed image with the blurred edges even though the smooth regions of the image might have been filtered correctly. Similarly, approaches, which were able to remove these blurred edges, could not effectively filter the low activity regions of the image. 49 Most approaches locally segment with a filtering mask before applying the moving-averaging filter hence they fail to recognize the real edges of the image. They work best on the smooth areas of the image but change sharp-edge pixel values wrongly and results in the blurred edges. Thus, detecting edges before applying segmentation to the image with a 3X3 filtering mask would yield a better result. Most information loss of a JPEG-compressed image occurs in the quantization of the DCT coefficients. Thus, the first step in the implementation of the de-blocking filter should be the filtering of the DCT sub bands. As DC co-efficients are the average of the pixel values of an 8X8 pixel block, there are high correlations among neighboring DC coefficients in the DC sub band. In this project, the DCT DC sub band was first filtered using a 3X3 filter mask. “Do no harm” policy was implemented to ensure the filtered values falling well out side the allowable quantization intervals of the DC sub bands were discarded. In that case, the original unfiltered values were passed in order to ensure the image quality. The local segmentation ensures that only sub band pixels, which belong to the same smooth area of the image, are included in the averaging process. PSNR of the reconstructed image with and without using de-blocking filter were compared to measure the improvement in the image quality. A MATLAB code was written to read the image pixels into a 256X256 matrix. Then, 2-D DCT was applied to transfer the image into the frequency domain followed by the quantization of the DCT- coefficients. The quantization matrix was generated using the formulae described in the previous section with quality factor Q set to 25. After that, 50 the DC sub band was read from the 256X256 Quantization matrix into a 32X32 matrix and all DC- coefficients in that matrix were arranged in the same way as their respective blocks in the image matrix. The filtering module was then applied to the DC sub band with a 3X3 neighborhood mask. The pixels in the mask are first segmented and then each segment is filtered independently. The pixel being filters is kept at the centre of the 3x3mask neighborhood of pixels. Each of the total nine pixels in a 3X3 mask is first segmented in two different classes, class zero and class one using the following formulae, where mask average is the average of all nine pixels in the given 3X3 mask. Segment= 0 = 1 if pixel value < mask average else…………………………………………….……………....(5.2) In this project, the average value of the pixels that belong to the same segment is defined as the segment representative. The pixel being filtered is replaced by the segment representative. The “do no harm” (DNH) policy is enforced to ensure that the new filtered value falls within the original value’s quantization interval. If it falls outside of the quantization interval then the unfiltered value is used to replace the filtered result. If it is well within the quantization interval of the original value, the filtered value is written to the filtered sub band storage. The pixel then moves by 3X3 mask and the filtering process is repeated again until all image segments are processed. After the filtered DC sub band replaces the original one in the DCT coefficient storage, the inverse-DCT is applied to reconstruct the final image. 51 The PSNR is calculated for the reconstructed images with and without using the de-blocking respectively. PSNR is a logarithmic scale of the mean squared difference (MSE) between two sets of pixel values. It is used as a general measure of image quality. PSNR of the reconstructed images in both cases (with and without the filtered values) was calculated with respect to the original image matrix using the following formulae, where Bi is the ith pixel value of the original image matrix and Ai is the ith pixel value of the reconstructed image matrix. Total number of pixels in the respective image matrix is represented by n. PSNR = 20 log10 (255/ (MSE)1/2) Where MSE = Σ i=0 to n ((Bi - Ai )2)/ n………………………..…………………(5.3) The PSNR calculated for both reconstructed images were then compared with each other to measure the improvement in the image quality. 5.3 Improvement Achieved in Image Quality Using De-blocking filter The original images used for this project (couple.gif and woman.png) along with the respective set of images reconstructed with and without using de-blocking filters are shown in figure 5.3.1 and 5.3.2. The reconstructed image of couple.gif after applying JPEG compression and decompression is shown in figure 5.3.1(b). It is a blurry image with many artifacts in it. The fineness of image is lost near the face of the woman and the wall/furniture in between the man and the woman due to the blocking effect caused by 52 the coarse quantization of DCT coefficients. However, the image reconstructed by using the new-filtered values of the DC sub band is shown in figure 5.3.1 (c) more closely matches with the original image. The face of the woman and the wall/furniture between them is clearly visible and less blurry with compared to figure 5.3.1 (b). As shown in table 5.3.1, the PSNR ration is improved by 1.328 db. Figure 5.3.1(a): Original Image couple.gif Figure: 5.3.1(b): Reconstructed image without using De-blocking filter Figure 5.3.1(c): Reconstructed image by Using De-blocking filter Figure 5.3.1: Reconstructed Images for Original Image “couple.gif” With and Without Using De-blocking Filter Respectively 53 Similar result was obtained for the image woman.png as shown in figure 5.3.2. Compared with the reconstructed image using the unfiltered values of DC sub band shown in figure 5.3.2(c), the reconstructed image by using the new- filtered DC sub band values shown in figure 5.3.2(b) more closely matches with the original image shown in figure 5.3.2(a). The difference in the image quality is visible in the face and the hands of the woman. Around 0.791 db improvement in the PSNR was achieved for the image woman.png as shown in table 5.3.1. Though implementation of the de-blocking Figure 5.3.2(a): Original Image Woman.png Figure 5.3.2(c): Reconstructed image with The use of de-blocking filter Figure 5.3.2(b): Reconstructed image without de-blocking filter 54 Figure 5.3.2: Reconstructed Images for Original Image “woman .png” With and Without Using De-blocking Filter Respectively filter certainly improves the PSNR and image quality, it still leaves some blurs and artifacts in the reconstructed images. I think more improvement in the image quality and PSNR can be achieved by filtering some of the high-energy AC sub bands (first 10 to 12) along with the DC sub bands. This is suggested for the future implementation because of the limited time and complexity of this project. Table 5.3.1 PSNR Results for Images couple.gif and woman.png Original PSNR of the reconstructed image PSNR of the reconstructed image Image before applying filtering(db) after applying filtering(db) Couple.gif 39.007 40.335 Woman.png 38.037 38.828 55 Chapter 6 CONCLUSION AND FUTURE WORK The JPEG image compression algorithm was studied in much detail and the various steps of the JPEG including DCT , quantization, Zigzag scanning and run length encoding were implemented on the Texas Instruments TMS320DM6437 platform successfully. Each step of the JPEG algorithm was verified for correctness by successfully implementing the Inverse functions such as IDCT, dequantization, Inverse Zigzag scanning and run length decoding on the same board and displaying the reconstructed images on the television. This project gave me the thorough knowledge and understanding of JPEG standard. KS test methodology was also implemented in MATLAB in order to predict the distributions of the DC and some of the high-energy AC coefficients. It is quite clear from the results of the KS test that for most of the real life digital images, the DC coefficients have the Gaussian distribution while the AC coefficients have the Laplacian distribution. However, for a few busy images like Ariel and Woman, it can be concluded that the distribution of all the coefficients tends to be Gaussian by nature. The same results can be also obtained by implementing X2 Goodness to fit test, which is suggested for the future implementation due to the tight time line of this project. The causes of blocking artifacts and blurs in the reconstructed images were studied in detail. The contribution of the sub bands to the construction of an image was also described. The possible solution to improve the quality of the reconstructed images by means of filtering the values of the DCT co-efficients after the quantization was 56 proposed and implemented. The results obtained in chapter 5 clearly shows that even only reconstructing the smoothed DC sub band would yield a large increase in the image quality. The image quality was measured by the means of Peak Signal to Noise Ratio (PSNR). Implementing the de-blocking filter module after the DCT and quantization module in JPEG standard would improve the PSNR by around 0.8 to 1.1 db. More improvement in image quality and PSNR can be achieved by effectively, carefully filtering some of the high-energy AC sub bands along with the DC sub band, which is suggested for the future implementation due to the limited time constraint for this project. 57 APPENDIX A C Code for JPEG Compression Implementation on TMS320DM6437 /* * ======== JPEG_complete.c ======== * */ /* runtime include files */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdarg.h> /* BIOS include files */ #include <std.h> #include <gio.h> #include <tsk.h> #include <trc.h> /* PSP include files */ #include <psp_i2c.h> #include <psp_vpfe.h> #include <psp_vpbe.h> #include <fvid.h> #include <psp_tvp5146_extVidDecoder.h> /* CSL include files */ #include <soc.h> #include <cslr_sysctl.h> /* BSL include files */ #include <evmdm6437.h> #include <evmdm6437_dip.h> /* Video Params Defaults */ #include <vid_params_default.h> /* This example supports either PAL or NTSC depending on position of JP1 */ #define STANDARD_PAL 0 #define STANDARD_NTSC 1 58 #define FRAME_BUFF_CNT 6 static int read_JP1(void); static CSL_SysctlRegsOvly sysModuleRegs = (CSL_SysctlRegsOvly )CSL_SYS_0_REGS; //******************************************************* // USER DEFINED FUNCTIONS // details: All functions for JPEG compression steps //******************************************************* void copy_z (void * currentFrame); void copy_y (void * currentFrame); void dct(); void multiply1(); void multiply2(); void multiply3(); void multiply4(); void quantization(); void multiply_coefficient(); void zig_zag_pattern(); void rle_invrle(); void invzig_zag_pattern(); void multiply_coefficient_reverse(); void idct (); //******************************************************* // VARIABLE ARRAYS //******************************************************* unsigned char y[720 * 480]; float z[720 * 480]; unsigned char x[720 * 480]; int quantized_720x480[720 * 480]; int zigzag_720x480[720*480]; int invzigzag_720x480[720*480]; int flag = 1; FILE * fp1; FILE * fp2; FILE * fp3; FILE * fp4; //******************************************************* 59 /* * ======== main ======== */ void main() { printf("Video Preview Application\n"); fflush(stdout); /* Initialize BSL library to read jumper switches: */ EVMDM6437_DIP_init(); /* VPSS PinMuxing */ /* CI10SEL - No CI[1:0] */ /* CI32SEL - No CI[3:2] */ /* CI54SEL - No CI[5:4] */ /* CI76SEL - No CI[7:6] */ /* CFLDSEL - No C_FIELD */ /* CWENSEL - No C_WEN */ /* HDVSEL - CCDC HD and VD enabled */ /* CCDCSEL - CCDC PCLK, YI[7:0] enabled */ /* AEAW - EMIFA full address mode */ /* VPBECKEN - VPBECLK enabled */ /* RGBSEL - No digital outputs */ /* CS3SEL - LCD_OE/EM_CS3 disabled */ /* CS4SEL - CS4/VSYNC enabled */ /* CS5SEL - CS5/HSYNC enabled */ /* VENCSEL - VCLK,YOUT[7:0],COUT[7:0] enabled */ /* AEM - 8bEMIF + 8bCCDC + 8 to 16bVENC */ sysModuleRegs -> PINMUX0 &= (0x005482A3u); sysModuleRegs -> PINMUX0 |= (0x005482A3u); /* PCIEN = 0: PINMUX1 - Bit 0 */ sysModuleRegs -> PINMUX1 &= (0xFFFFFFFEu); sysModuleRegs -> VPSSCLKCTL = (0x18u); return; } /* * ======== video_preview ======== */ void video_preview(void) { 60 FVID_Frame *frameBuffTable[FRAME_BUFF_CNT]; FVID_Frame *frameBuffPtr; GIO_Handle hGioVpfeCcdc; GIO_Handle hGioVpbeVid0; GIO_Handle hGioVpbeVenc; int status = 0; int result; int i; int standard; int width; int height; /* Set video display/capture driver params to defaults */ PSP_VPFE_TVP5146_ConfigParams tvp5146Params = VID_PARAMS_TVP5146_DEFAULT; PSP_VPFECcdcConfigParams vpfeCcdcConfigParams = VID_PARAMS_CCDC_DEFAULT_D1; PSP_VPBEOsdConfigParams vpbeOsdConfigParams = VID_PARAMS_OSD_DEFAULT_D1; PSP_VPBEVencConfigParams vpbeVencConfigParams; standard = read_JP1(); /* Update display/capture params based on video standard (PAL/NTSC) */ if (standard == STANDARD_PAL) { width = 720; height = 576; vpbeVencConfigParams.displayStandard = PSP_VPBE_DISPLAY_PAL_INTERLACED_COMPOSITE; } else { width = 720; height = 480; vpbeVencConfigParams.displayStandard = PSP_VPBE_DISPLAY_NTSC_INTERLACED_COMPOSITE; } vpfeCcdcConfigParams.height = vpbeOsdConfigParams.height = height; vpfeCcdcConfigParams.width = vpbeOsdConfigParams.width = width; vpfeCcdcConfigParams.pitch = vpbeOsdConfigParams.pitch = width * 2; /* init the frame buffer table */ for (i=0; i<FRAME_BUFF_CNT; i++) { 61 frameBuffTable[i] = NULL; } /* create video input channel */ if (status == 0) { PSP_VPFEChannelParams vpfeChannelParams; vpfeChannelParams.id = PSP_VPFE_CCDC; vpfeChannelParams.params = (PSP_VPFECcdcConfigParams*)&vpfeCcdcConfigParams; hGioVpfeCcdc = FVID_create("/VPFE0",IOM_INOUT,NULL,&vpfeChannelParams,NULL); status = (hGioVpfeCcdc == NULL ? -1 : 0); } /* create video output channel, plane 0 */ if (status == 0) { PSP_VPBEChannelParams vpbeChannelParams; vpbeChannelParams.id = PSP_VPBE_VIDEO_0; vpbeChannelParams.params = (PSP_VPBEOsdConfigParams*)&vpbeOsdConfigParams; hGioVpbeVid0 = FVID_create("/VPBE0",IOM_INOUT,NULL,&vpbeChannelParams,NULL); status = (hGioVpbeVid0 == NULL ? -1 : 0); } /* create video output channel, venc */ if (status == 0) { PSP_VPBEChannelParams vpbeChannelParams; vpbeChannelParams.id = PSP_VPBE_VENC; vpbeChannelParams.params = (PSP_VPBEVencConfigParams *)&vpbeVencConfigParams; hGioVpbeVenc = FVID_create("/VPBE0",IOM_INOUT,NULL,&vpbeChannelParams,NULL); status = (hGioVpbeVenc == NULL ? -1 : 0); } /* configure the TVP5146 video decoder */ if (status == 0) { result = FVID_control(hGioVpfeCcdc, VPFE_ExtVD_BASE+PSP_VPSS_EXT_VIDEO_DECODER_CONFIG, &tvp5146Params); status = (result == IOM_COMPLETED ? 0 : -1); } 62 /* allocate some frame buffers */ if (status == 0) { for (i=0; i<FRAME_BUFF_CNT && status == 0; i++) { result = FVID_allocBuffer(hGioVpfeCcdc, &frameBuffTable[i]); status = (result == IOM_COMPLETED && frameBuffTable[i] != NULL ? 0 : -1); } } /* prime up the video capture channel */ if (status == 0) { FVID_queue(hGioVpfeCcdc, frameBuffTable[0]); FVID_queue(hGioVpfeCcdc, frameBuffTable[1]); FVID_queue(hGioVpfeCcdc, frameBuffTable[2]); } /* prime up the video display channel */ if (status == 0) { FVID_queue(hGioVpbeVid0, frameBuffTable[3]); FVID_queue(hGioVpbeVid0, frameBuffTable[4]); FVID_queue(hGioVpbeVid0, frameBuffTable[5]); } /* grab first buffer from input queue */ if (status == 0) { FVID_dequeue(hGioVpfeCcdc, &frameBuffPtr); } /* loop forever performing video capture and display */ while ( status == 0 ) { /* grab a fresh video input frame */ FVID_exchange(hGioVpfeCcdc, &frameBuffPtr); copy_y ((frameBuffPtr->frame.frameBufferPtr)); dct(); quantization(); idct(); copy_z((frameBuffPtr->frame.frameBufferPtr)); /* display the video frame */ FVID_exchange(hGioVpbeVid0, &frameBuffPtr); 63 status=1; } } //----------------------------------------------------------------------------------------------void copy_y (void * currentFrame) { int m, n; m = 0; //fp2 = fopen("y_components_image.txt","w"); for (n = 1; n < 720 * 480 * 2; n = n+2) { y[m++] = * (((unsigned char * )currentFrame)+n); //fprintf(fp2, "%d ", y[m-1]); if((m%720) == 0) { // fprintf(fp2, "\n"); } } } //----------------------------------------------------------------------------------------------void copy_z (void * currentFrame) { int m, n; //fp3 = fopen ("idct.txt","w"); m = 0; for (n = 1; n < 720 * 480 * 2; n = n+2) { * (((unsigned char * )currentFrame)+n) = x[m++]; // fprintf(fp3, "%d ", x[m-1]); // // } } if((m%720) == 0) { fprintf(fp3, "\n"); } * (((unsigned char * )currentFrame)+n+1) = 0x80; 64 //********************************************************************** // DCT ///********************************************************************** // Details: 2D DCT transform using cosine matrix and it’s transposed matrix //********************************************************************** float a[8][8]; float ct[8][8]; float c[8][8]; //unsigned char z[720 * 480]; // CO-EFF Matrix float b[8][8] = { {0.353553, 0.353553, 0.353553, 0.353553, 0.353553, 0.353553, 0.353553, 0.353553}, {0.490393, 0.415818, 0.277992, 0.097887, -0.097106, -0.277329, -0.415375, 0.490246}, {0.461978, 0.191618, -0.190882, -0.461673, -0.462282, -0.192353, 0.190145, 0.461366}, {0.414818, -0.097106, -0.490246, -0.278653, 0.276667, 0.490710, 0.099448, 0.414486}, {0.353694, -0.353131, -0.354256, 0.352567, 0.354819, -0.352001, -0.355378, 0.351435}, {0.277992, -0.490246, 0.096324, 0.4167, -0.414486, -0.100228, 0.491013, 0.274673}, {0.191618, -0.462282, 0.461366, -0.189409, -0.193822, 0.463187, -0.460440, 0.187195}, {0.097887, -0.278653, 0.4167, -0.490862, 0.489771, -0.413593, 0.274008, 0.092414} }; float bt[8][8] = { {0.353553, 0.490393, 0.461978, 0.414818, 0.353694, 0.277992, 0.191618, 0.097887}, {0.353553, 0.415818, 0.191618, -0.097106, -0.353131, -0.490246, -0.462282, 0.278653}, {0.353553, 0.277992, -0.190882, -0.490296, -0.354256, 0.096324, 0.461366, 0.4167 }, 65 {0.353553, 0.097887, -0.461673, -0.278653, 0.352567, 0.4167, -0.189409, 0.490862}, {0.353553, -0.097106, -0.462282, 0.276667, 0.354819, -0.414486, -0.193822, 0.489771}, {0.353553, -0.277329, -0.192353, 0.490710, -0.352001, -0.100228, 0.463187, 0.413593}, {0.353553, -0.415375, 0.190145, 0.099448, -0.355378, 0.491013, -0.460440, 0.274008}, {0.353553, -0.490246, 0.461366, -0.414486, 0.351435, -0.274673, 0.187195, 0.092414} }; void dct() { int i, j; int m, n; int k, l; //fp1 = fopen ("dct_image.txt", "w"); for (i=0; i<480; i = i+8) { for(j=0; j<720; j = j+8) { k = 0; l = 0; for(m = i; m<(i+8); m++) { for(n = j; n<(j+8); n++) { a[k][l++] = y[(m*720) + n]; } l = 0; k++; } multiply1(); multiply2(); k = 0; l = 0; 66 for (m = i; m < (i+8); m++) { for(n = j; n < (j+8); n++) { //* (((unsigned char * )currentFrame)+(i * 720) + j) = c[m][n]; z[(m * 720) + n] = c[k][l++]; //fprintf(fp1, "%d ", z[(m * 720) + n]); if(l == 8) { l = 0; k++; } } } } } } void multiply1() { int i, j, k; float sum; for ( i = 0; i < 8; i++) { for ( j = 0; j < 8; j++) { sum = 0; for (k = 0; k < 8; k++) { sum = sum + a[i][k] * bt[k][j]; } ct[i][j] = sum; } } } void multiply2() { int i, j, k; 67 int sum; for ( i = 0; i < 8; i++) { for ( j = 0; j < 8; j++) { sum = 0; for (k = 0; k < 8; k++) { sum = sum + b[i][k] * ct[k][j]; } c[i][j] = sum; } } } //********************************************************************** // IDCT //********************************************************************** // Details : 2D IDCT transform using cosine matrix and it’s transposed matrix //********************************************************************** void idct() { int i, j; int m, n; int k, l; //fp3 = fopen ("idct7.txt","w"); for (i=0; i<480; i = i+8) { for(j=0; j<720; j = j+8) { k = 0; l = 0; for(m = i; m<(i+8); m++) { for(n = j; n<(j+8); n++) { a[k][l++] = quantized_720x480[(m*720) + n]; // a[k][l++] = z[(m*720) + n]; 68 } l = 0; k++; } multiply3(); multiply4(); k = 0; l = 0; for (m = i; m < (i+8); m++) { for(n = j; n < (j+8); n++) { x[(m * 720) + n] = c[k][l++]; //* (((unsigned char * )currentFrame)+(m * 720) + n) = 0x80; //*(((unsigned char * )currentFrame)+(m * 720) + n+1) = c[k][l++]; //fprintf(fp3, "%d ", x[(m * 720) + n]); if(l == 8) { l = 0; k++; } } } } } } void multiply3() { int i, j, k; float sum; for ( i = 0; i < 8; i++) { for ( j = 0; j < 8; j++) { sum = 0; 69 for (k = 0; k < 8; k++) { sum = sum + a[i][k] * b[k][j]; } ct[i][j] = sum; } } } void multiply4() { int i, j, k; int sum; for ( i = 0; i < 8; i++) { for ( j = 0; j < 8; j++) { sum = 0; for (k = 0; k < 8; k++) { sum = sum + bt[i][k] * ct[k][j]; } c[i][j] = sum; } } } //********************************************************************** // QUANTIZATION AND ZIGZAG //********************************************************************** //********************************************************************** // Details: Quantization and ZigZag sequencing and RLE encoding/ decoding on the image //********************************************************************** int q[8][8] = { {3, 5, 7, 9, 11, 13, 15, 17}, {5, 7, 9, 11, 13, 15, 17, 19}, {7, 9, 11, 13, 15, 17, 19, 21}, {9, 11, 13, 15, 17, 19, 21, 23}, {11, 13, 15, 17, 19, 21, 23, 25}, {13, 15, 17, 19, 21, 23, 25, 27}, 70 {15, 17, 19, 21, 23, 25, 27, 29}, {17, 19, 21, 23, 25, 27, 29, 31} }; int quantized_8x8[8][8]; int dequantized_8x8[8][8]; int zig_zag[8][8]; int invzig_zag[8][8]; int invrle_8x8 [8][8]; /* code for quantization */ void quantization() { int i, j; int m, n; int k, l; //fp4 = fopen("quantization.txt","w"); for (i=0; i<480; i = i+8) { for(j=0; j<720; j = j+8) { k = 0; l = 0; for(m = i; m<(i+8); m++) { for(n = j; n<(j+8); n++) { a[k][l++] = z[(m*720) + n]; } l = 0; k++; } multiply_coefficient(); //multiply_coefficient_reverse(); zig_zag_pattern(); rle_invrle(); invzig_zag_pattern(); multiply_coefficient_reverse(); k = 0; l = 0; 71 //p = 0; for (m = i; m < (i+8); m++) { for(n = j; n < (j+8); n++) { quantized_720x480[(m * 720) + n] = dequantized_8x8[k][l++]; //quantized_720x480[(m * 720) + n] = invzig_zag[k][l++]; //quantized_720x480[(m * 720) + n] = quantized_8x8[k][l++]; //zigzag_720x480[(m * 720) + n]= zig_zag[k][p++]; //invzigzag_720x480[(m * 720) + n]=invzig_zag[k][p++]; //* (((unsigned char * )currentFrame)+(m * 720) + n) = 0x80; //* (((unsigned char * )currentFrame)+(m * 720) + n+1) = c[k][l++]; //fprintf(fp4, "%d ", quantized_720x480[(m * 720) + n]); if(l == 8 /*|| p==8*/) { l = 0; //p = 0; k++; } } } } } } void multiply_coefficient() { int i, j; for ( i = 0; i < 8; i++) { for ( j = 0; j < 8; j++) { quantized_8x8[i][j] = (a[i][j] / q[i][j]); 72 } } } /*void multiply_coefficient_reverse() { int i, j; for ( i = 0; i < 8; i++) { for ( j = 0; j < 8; j++) { dequantized_8x8[i][j] = (quantized_8x8[i][j] * q[i][j]); } } } */ void zig_zag_pattern() { zig_zag[0][0] = quantized_8x8[0][0]; zig_zag[0][1] = quantized_8x8[0][1]; zig_zag[0][2] = quantized_8x8[1][0]; zig_zag[0][3] = quantized_8x8[2][0]; zig_zag[0][4] = quantized_8x8[1][1]; zig_zag[0][5] = quantized_8x8[0][2]; zig_zag[0][6] = quantized_8x8[0][3]; zig_zag[0][7] = quantized_8x8[1][2]; zig_zag[1][0] = quantized_8x8[2][1]; zig_zag[1][1] = quantized_8x8[3][0]; zig_zag[1][2] = quantized_8x8[4][0]; zig_zag[1][3] = quantized_8x8[3][1]; zig_zag[1][4] = quantized_8x8[2][2]; zig_zag[1][5] = quantized_8x8[1][3]; zig_zag[1][6] = quantized_8x8[0][4]; zig_zag[1][7] = quantized_8x8[0][5]; zig_zag[2][0] = quantized_8x8[1][4]; zig_zag[2][1] = quantized_8x8[2][3]; zig_zag[2][2] = quantized_8x8[3][2]; zig_zag[2][3] = quantized_8x8[4][1]; 73 zig_zag[2][4] = quantized_8x8[5][0]; zig_zag[2][5] = quantized_8x8[6][0]; zig_zag[2][6] = quantized_8x8[5][1]; zig_zag[2][7] = quantized_8x8[4][2]; zig_zag[3][0] = quantized_8x8[3][3]; zig_zag[3][1] = quantized_8x8[2][4]; zig_zag[3][2] = quantized_8x8[1][5]; zig_zag[3][3] = quantized_8x8[0][6]; zig_zag[3][4] = quantized_8x8[0][7]; zig_zag[3][5] = quantized_8x8[1][6]; zig_zag[3][6] = quantized_8x8[2][5]; zig_zag[3][7] = quantized_8x8[3][4]; zig_zag[4][0] = quantized_8x8[4][3]; zig_zag[4][1] = quantized_8x8[5][2]; zig_zag[4][2] = quantized_8x8[6][1]; zig_zag[4][3] = quantized_8x8[7][0]; zig_zag[4][4] = quantized_8x8[7][1]; zig_zag[4][5] = quantized_8x8[6][2]; zig_zag[4][6] = quantized_8x8[5][3]; zig_zag[4][7] = quantized_8x8[4][4]; zig_zag[5][0] = quantized_8x8[3][5]; zig_zag[5][1] = quantized_8x8[2][6]; zig_zag[5][2] = quantized_8x8[1][7]; zig_zag[5][3] = quantized_8x8[2][7]; zig_zag[5][4] = quantized_8x8[3][6]; zig_zag[5][5] = quantized_8x8[4][5]; zig_zag[5][6] = quantized_8x8[5][4]; zig_zag[5][7] = quantized_8x8[6][3]; zig_zag[6][0] = quantized_8x8[7][2]; zig_zag[6][1] = quantized_8x8[7][3]; zig_zag[6][2] = quantized_8x8[6][4]; zig_zag[6][3] = quantized_8x8[5][5]; zig_zag[6][4] = quantized_8x8[4][6]; zig_zag[6][5] = quantized_8x8[3][7]; zig_zag[6][6] = quantized_8x8[4][7]; zig_zag[6][7] = quantized_8x8[5][6]; zig_zag[7][0] = quantized_8x8[6][5]; zig_zag[7][1] = quantized_8x8[7][4]; zig_zag[7][2] = quantized_8x8[7][5]; zig_zag[7][3] = quantized_8x8[6][6]; zig_zag[7][4] = quantized_8x8[5][7]; zig_zag[7][5] = quantized_8x8[6][7]; zig_zag[7][6] = quantized_8x8[7][6]; 74 zig_zag[7][7] = quantized_8x8[7][7]; } void rle_invrle() { int i=0,counter,cnt2=0,j,enccout = 0,k,counter3,hit=0; static int zigzag[64]; int invrle[64]; static int rle6[64] ; static int m; int rle9[64]; int encodedrle[64]; //int invrle_8x8 [8][8]; for(i=0;i<8;i++) { for(j=0;j<8; j++) { zigzag[(i*8)+j]= zig_zag[i][j]; } } rle6[0] = zigzag[0]; j=1; for (i=1;i<64;i++) { while(zigzag[i-1] == zigzag[i]) { cnt2 = cnt2 +1; if(cnt2 <2) 75 { rle6[j] = zigzag[i]; counter3 = cnt2; enccout = enccout + 1; j = j +1; } else { counter3 = cnt2; i = i +1; } } if(counter3 == 2) { rle6[j] = 0; enccout = enccout + 1; j = j +1; counter3 = 0; } if (cnt2 > 2) { rle6[j] = cnt2 -2 ; rle6[j+1] = zigzag[i]; enccout = enccout +2; j = j+2; cnt2 = 0; counter3 = 0; } else { 76 rle6[j] = zigzag[i]; enccout = enccout + 1; j = j +1; cnt2 = 0; counter3 = 0; } } for(k=0 ; k < enccout ;k++) { encodedrle[k] = rle6[k]; } ///////////INVERSE RLE/////////////////////////// i=0; m=0; for(k=0;k< enccout; k++) { rle9[k] = encodedrle[k]; } invrle[0] = rle9[0]; for(i=1; i< enccout; i++) { if((rle9[i]== rle9[i-1]) && hit == 0) { counter = rle9[i+1]; for(j=0;j <= counter; j++) { invrle[i+j+m] = rle9[i]; } 77 m += (counter -1 ); counter = 0; hit = 1; i = i + 1; } else { invrle[i+m] = rle9[i]; hit =0; } } //invrle[63]= zigzag[63]; for(i=0;i<8;i++) { for(j=0;j<8;j++) { invrle_8x8 [i][j]= invrle[(i*8)+j]; } } } void invzig_zag_pattern() { invzig_zag[0][0] = invrle_8x8[0][0]; invzig_zag[0][1] = invrle_8x8[0][1]; invzig_zag[0][2] = invrle_8x8[0][5]; invzig_zag[0][3] = invrle_8x8[0][6]; invzig_zag[0][4] = invrle_8x8[1][6]; invzig_zag[0][5] = invrle_8x8[1][7]; 78 invzig_zag[0][6] = invrle_8x8[3][3]; invzig_zag[0][7] = invrle_8x8[3][4]; invzig_zag[1][0] = invrle_8x8[0][2]; invzig_zag[1][1] = invrle_8x8[0][4]; invzig_zag[1][2] = invrle_8x8[0][7]; invzig_zag[1][3] = invrle_8x8[1][5]; invzig_zag[1][4] = invrle_8x8[2][0]; invzig_zag[1][5] = invrle_8x8[3][2]; invzig_zag[1][6] = invrle_8x8[3][5]; invzig_zag[1][7] = invrle_8x8[5][2]; invzig_zag[2][0] = invrle_8x8[0][3]; invzig_zag[2][1] = invrle_8x8[1][0]; invzig_zag[2][2] = invrle_8x8[1][4]; invzig_zag[2][3] = invrle_8x8[2][1]; invzig_zag[2][4] = invrle_8x8[3][1]; invzig_zag[2][5] = invrle_8x8[3][6]; invzig_zag[2][6] = invrle_8x8[5][1]; invzig_zag[2][7] = invrle_8x8[5][3]; invzig_zag[3][0] = invrle_8x8[1][1]; invzig_zag[3][1] = invrle_8x8[1][3]; invzig_zag[3][2] = invrle_8x8[2][2]; invzig_zag[3][3] = invrle_8x8[3][0]; invzig_zag[3][4] = invrle_8x8[3][7]; invzig_zag[3][5] = invrle_8x8[5][0]; invzig_zag[3][6] = invrle_8x8[5][4]; invzig_zag[3][7] = invrle_8x8[6][5]; invzig_zag[4][0] = invrle_8x8[1][2]; invzig_zag[4][1] = invrle_8x8[2][3]; invzig_zag[4][2] = invrle_8x8[2][7]; invzig_zag[4][3] = invrle_8x8[4][0]; invzig_zag[4][4] = invrle_8x8[4][7]; invzig_zag[4][5] = invrle_8x8[5][5]; invzig_zag[4][6] = invrle_8x8[6][4]; invzig_zag[4][7] = invrle_8x8[6][6]; invzig_zag[5][0] = invrle_8x8[2][4]; invzig_zag[5][1] = invrle_8x8[2][6]; invzig_zag[5][2] = invrle_8x8[4][1]; invzig_zag[5][3] = invrle_8x8[4][6]; invzig_zag[5][4] = invrle_8x8[5][6]; invzig_zag[5][5] = invrle_8x8[6][3]; invzig_zag[5][6] = invrle_8x8[6][7]; invzig_zag[5][7] = invrle_8x8[7][4]; invzig_zag[6][0] = invrle_8x8[2][5]; 79 invzig_zag[6][1] = invrle_8x8[4][2]; invzig_zag[6][2] = invrle_8x8[4][5]; invzig_zag[6][3] = invrle_8x8[5][7]; invzig_zag[6][4] = invrle_8x8[6][2]; invzig_zag[6][5] = invrle_8x8[7][0]; invzig_zag[6][6] = invrle_8x8[7][3]; invzig_zag[6][7] = invrle_8x8[7][5]; invzig_zag[7][0] = invrle_8x8[4][3]; invzig_zag[7][1] = invrle_8x8[4][4]; invzig_zag[7][2] = invrle_8x8[6][0]; invzig_zag[7][3] = invrle_8x8[6][1]; invzig_zag[7][4] = invrle_8x8[7][1]; invzig_zag[7][5] = invrle_8x8[7][2]; invzig_zag[7][6] = invrle_8x8[7][6]; invzig_zag[7][7] = invrle_8x8[7][7]; } void multiply_coefficient_reverse() { int i, j; for ( i = 0; i < 8; i++) { for ( j = 0; j < 8; j++) { dequantized_8x8[i][j] = (invzig_zag[i][j] * q[i][j]); } } } /* * ======== read_JP1 ======== * Read the PAL/NTSC jumper. * * Retry, as I2C sometimes fails: */ static int read_JP1(void) { int jp1 = -1; 80 while (jp1 == -1) { jp1 = EVMDM6437_DIP_get(JP1_JUMPER); TSK_sleep(1); } return(jp1); } 81 APPENDIX B MATLAB Code for Applying KS Goodness to Fit Test clear; % clear the workspace, remove all existing variables close all; % close all figures % reads and displays an image, now image is a 3-D array img=imread('woman.png'); figure; imshow(img); title('uncompressed image'); % Display the uncompressed image % Define the heigth and width of the Image array [height, width]=size(img) %apply the dct transform to every 8*8 blocks of the image array for a=1:8:height for b=1:8:width imgdct_8(a:a+7,b:b+7)=dct2(img(a:a+7,b:b+7)); end end for a=1:8:height for b=1:8:width img_8(a:a+7,b:b+7)=img(a:a+7,b:b+7); end end % Display the DCT coefficients of the Image array figure; imshow(imgdct_8,[]); title('DCT coefficents after 8*8 DCT transform'); % add ofset of 200 for the elimination of the negative pixel values img_os= imgdct_8+200; 82 %Display the DCT coefficients of the Image array after adding the Offset figure; imshow(img_os,[]); title('DCT coefficents after adding offset of 200'); %Generating 8*8 quantization array using the standard formula % 1+((i+j)* quality). Setting quality=2 for instant here for i=1:8 for j=1:8 qtz_mtrx(i,j)=1+((i+j)* 2); end end %Replicating 8*8 Quantization matrix into a 256*256 Quantization matrix to %generate matri qtz_mtrx256 qtz_mtrx256=repmat(qtz_mtrx,32,32) % Quantizing the 256*256 Matrix img_qtz using the qtz_mtrx256 of same size for c=1:256 for d=1:256 img_qtz(c,d)=round(img_os(c,d)/qtz_mtrx256(c,d)); end end %Display the DCT coefficients of the Image array after quantization figure; imshow(img_qtz,[]); title('DCT coefficents after the quantization'); %----------------------------------------------------%----------------------------------------------------% APPLYIN KS TEST img_qtzvector = img_qtz( : ); % Convert the Matrix into Single dimension vector % Fetching the COO pixels from the given matrix C00_triple=[img_qtzvector(1:8:256),img_qtzvector(4097:8:4352),img_qtzvector(8193:8: 8448),img_qtzvector(12289:8:12544),img_qtzvector(16385:8:16640),img_qtzvector(204 81:8:20736),img_qtzvector(24577:8:24832),img_qtzvector(28673:8:28928)]; 83 C00= reshape (C00_triple, 1, prod(size(C00_triple))); %end C00sort=sort(C00); % Implementing the function for Emperical CDF for i= 1:256 for j= 1:256 if(C00(i)>=C00sort(256)) Empcdf(i)=1; elseif(C00(i)== C00sort(j)) Empcdf(i)=(j/256) elseif(C00(i)<C00sort(1)) Empcdf(i)=0; end end end % Finding the Mean and variance Mean=mean(C00); variance=std(C00); % Implementing the Gaussian CDF function Gaussiancdf= normcdf(C00,Mean,variance); KS_Gaussian= max(Empcdf-Gaussiancdf); % Implementing the Laplace CDF function % Calculating scale parameter Lambda of the Laplace CDF 84 Lambda= ((0.5 * variance)^0.5); for i= 1:256 if(C00(i)< Mean) Laplacecdf(i)= (0.5 * (2.7182^((C00(i)- Mean)/ Lambda))); elseif (C00(i)>= Mean) Laplacecdf(i)= 1-(0.5 * (2.7182^((-1)*(C00(i)- Mean)/ Lambda))); end end KS_Laplace= max(Empcdf-Laplacecdf); %Estimating parameters for the Gamma CDF: Scale_Gamma= 1.05*(variance/Mean); Shape_Gamma= ( (Mean*Mean)/variance ); Gammacdf= gamcdf(C00,Shape_Gamma,Scale_Gamma); KS_Gamma= max(Empcdf-Gammacdf); % Estimating Scale parameter B parameter of rayleigh CDF B= (Mean/1.25); Raycdf= raylcdf(C00,B); KS_Ray= max(Empcdf-Raycdf); % KS Test for C01 % Fetching the C01 pixels from the given matrix C01_triple=[img_qtzvector(2:8:257),img_qtzvector(4098:8:4353),img_qtzvector(8194:8: 8449),img_qtzvector(12290:8:12545),img_qtzvector(16386:8:16641),img_qtzvector(204 82:8:20737),img_qtzvector(24578:8:24833), img_qtzvector(28674:8:28929)]; C01= reshape (C01_triple, 1, prod(size(C01_triple))); C01sort=sort(C01); 85 % Generating the empirical CDF function for C01 for i= 1:256 for j= 1:256 if(C01(i)>=C01sort(256)) EmpcdfC01(i)=1; elseif(C01(i)== C01sort(j)) EmpcdfC01(i)=(j/256) elseif(C01(i)<C01sort(1)) EmpcdfC01(i)=0; end end end % Finding the mean and variance MeanC01=mean(C01); varianceC01=std(C01); % Implementing the Gaussian CDF function GaussiancdfC01= normcdf(C01,MeanC01,varianceC01); KS_GaussianC01= max(EmpcdfC01-GaussiancdfC01); % Implementing the Laplace CDF function % Calculating scale parameter Lambda of the Laplace CDF LambdaC01= ((0.5 * varianceC01)^0.5); for i= 1:256 if(C01(i)< MeanC01) LaplacecdfC01(i)= (0.5 * (2.7182^((C01(i)- MeanC01)/ LambdaC01))); elseif (C01(i)>= MeanC01) LaplacecdfC01(i)= 1-(0.5 * (2.7182^(-(C01(i)- MeanC01)/ LambdaC01))); 86 end end KS_LaplaceC01= max(EmpcdfC01-LaplacecdfC01); %Estimating parameters for the Gamma CDF: Scale_GammaC01= 1.05*(varianceC01/MeanC01); Shape_GammaC01= ( (MeanC01*MeanC01)/varianceC01 ); GammacdfC01= gamcdf(C01,Shape_GammaC01,Scale_GammaC01); %% Implementing the Gamma CDF function KS_GammaC01= max(EmpcdfC01-GammacdfC01); % Estimating Scale parameter B parameter of rayleigh CDF BC01= (MeanC01/1.25); % Implementing the Rayleigh CDF function RaycdfC01= raylcdf(C01,BC01); KS_RayC01= max(EmpcdfC01-RaycdfC01); % KS Test for C02 C02_triple=[img_qtzvector(3:8:258),img_qtzvector(4099:8:4354),img_qtzvector(8195:8: 8450),img_qtzvector(12291:8:12546),img_qtzvector(16387:8:16642),img_qtzvector(204 83:8:20738),img_qtzvector(24579:8:24834), img_qtzvector(28675:8:28930)]; C02= reshape (C02_triple, 1, prod(size(C02_triple))); C02sort=sort(C02); for i= 1:256 for j= 1:256 87 if(C02(i)>=C02sort(256)) EmpcdfC02(i)=1; elseif(C02(i)== C02sort(j)) EmpcdfC02(i)=(j/256) elseif(C02(i)<C02sort(1)) EmpcdfC02(i)=0; end end end MeanC02=mean(C02); varianceC02=std(C02); GaussiancdfC02= normcdf(C02,MeanC02,varianceC02); KS_GaussianC02= max(EmpcdfC02-GaussiancdfC02); % Implementing the Laplace CDF function % Calculating scale parameter Lambda of the Laplace CDF LambdaC02= ((0.5 * varianceC02)^0.5); for i= 1:256 if(C02(i)< MeanC02) LaplacecdfC02(i)= (0.5 * (2.7182^((C02(i)- MeanC02)/ LambdaC02))); elseif (C02(i)>= MeanC02) LaplacecdfC02(i)= 1-(0.5 * (2.7182^(-(C02(i)- MeanC02)/ LambdaC02))); end end KS_LaplaceC02= max(EmpcdfC02-LaplacecdfC02); %Estimating parameters for the Gamma CDF: 88 Scale_GammaC02= 1.05 * (varianceC02/MeanC02); Shape_GammaC02= ( (MeanC02*MeanC02)/varianceC02 ); GammacdfC02= gamcdf(C02,Shape_GammaC02,Scale_GammaC02); KS_GammaC02= max(EmpcdfC02-GammacdfC02); % Estimating Scale parameter B parameter of rayleigh CDF BC02= (MeanC02/1.25); RaycdfC02= raylcdf(C02,BC02); KS_RayC02= max(EmpcdfC02-RaycdfC02); %%%%---------------------------------------------------------------------%%%%%% Generating graphs for KS Statistics for different tests %%%%----------------------------------------------------------------------- KS_00= [KS_Gaussian,KS_Laplace,KS_Gamma,KS_Ray]; KS_01= [KS_GaussianC01,KS_LaplaceC01,KS_GammaC01,KS_RayC01]; KS_02= [KS_GaussianC02,KS_LaplaceC02,KS_GammaC02,KS_RayC02]; 89 APPENDIX C MATLAB Code for Designing De-blocking Filter clear; close all % clear the workspace, remove all existing variables % close all figures % reads and displays an image, now image is a 3-D array img=imread('couple.gif'); figure; imshow(img); title('original image'); % Display the uncompressed image % Define the heigth and width of the Image array [height, width]=size(img) %apply the dct transform to every 8*8 blocks of the image array for a=1:8:height for b=1:8:width imgdct_8(a:a+7,b:b+7)=dct2(img(a:a+7,b:b+7)); end end %Generating 8*8 quantization array using the standard formula % 1+((i+j)* quality). Setting quality=10 for instant here for i=1:8 for j=1:8 qtz_mtrx(i,j)=1+((i+j)* 15); end end %Replicating 8*8 Quantization matrix into a 256*256 Quantization matrix to %generate matri qtz_mtrx256 qtz_mtrx256=repmat(qtz_mtrx,32,32) % Quantizing the 256*256 Matrix img_qtz using the qtz_mtrx256 of same size 90 for c=1:256 for d=1:256 img_qtz(c,d)=round((imgdct_8(c,d))/qtz_mtrx256(c,d)); end end % Dequantizing the 256*256 Matrix img_qtz using the qtz_mtrx256 of same size for c=1:256 for d=1:256 img_deqtz(c,d)=round((img_qtz(c,d))*qtz_mtrx256(c,d)); end end %--------------------------------------------------------------------------------------------------------% Reconsturcting the image using IDCT %--------------------------------------------------------------------------------------------------------for a=1:8:height for b=1:8:width imgidct_8(a:a+7,b:b+7)=idct2(img_deqtz(a:a+7,b:b+7)); end end figure; imshow(imgidct_8,[]); % display image reconstructed title('reconstructed image without deblocking filter'); %--------------------------------------------------------------------------------------------------------% Designing the Deblocking Filter %--------------------------------------------------------------------------------------------------------- % Creating DC subband by extrecting each of the DC coefficient from the % 256*256 matrix img_qtz to the 32*32 matrix DC_subband m=1; n=1; for k= 1:32 for l= 1:32 dc_subband(k,l)= img_qtz(m,n); 91 if n<249 n=n+8; else n=1; end end if m<249 m=m+8; else m=1; end end for i= 2:31 for j= 2:31 mask_avg=(dc_subband(i-1,j-1)+dc_subband(i-1,j)+dc_subband(i1,j+1)+dc_subband(i,j1)+dc_subband(i,j)+dc_subband(i,j+1)+dc_subband(i+1,j1)+dc_subband(i+1,j)+dc_subb and(i+1,j+1))/ 9; counter_zero= 0; counter_one= 0; segment_zero= 0; segment_one= 0; for a= i-1 : i+1 92 for b= j-1 : j+1 if dc_subband(a,b)< mask_avg segment_zero= segment_zero + dc_subband(a,b); counter_zero= counter_zero+1; else segment_one= segment_one + dc_subband(a,b); counter_one=counter_one+1; end end end if dc_subband(i,j)< mask_avg dc_subbandfiltered(i-1,j-1)= segment_zero/ counter_zero; else dc_subbandfiltered(i-1,j-1)= segment_one/ counter_one; end end end %Generating the filtered quantization matrix to reflect the new values: for c=1:256 for d=1:256 img_qtz1(c,d)=((imgdct_8(c,d))/qtz_mtrx256(c,d)); end end 93 m=9; n=9; for i=1:30 for j=1:30 img_qtz1(m,n)=dc_subbandfiltered(i,j); if n<241 n=n+8; else n=9; end end if m<241 m=m+8; else m=1; end end % Dequantizing the 256*256 filtered Matrix img_qtz1 using the qtz_mtrx256 of same size for e=1:256 for f=1:256 img_deqtz1(e,f)=((img_qtz1(e,f))*qtz_mtrx256(e,f)); end end 94 %--------------------------------------------------------------------------------------------------------% Reconsturcting the image using IDCT %--------------------------------------------------------------------------------------------------------for a=1:8:height for b=1:8:width imgidct1_8(a:a+7,b:b+7)=idct2(img_deqtz1(a:a+7,b:b+7)); end end figure; imshow(imgidct1_8,[]); % display image reconstructed title('reconstructed image with deblocking filter'); %--------------------------------------------------------------------------------------------------------% Calculating the PSNR of both of the DC subbands of both the recounstructed images imgidct_8 and % imgidct1_8 %--------------------------------------------------------------------------------------------------------accumulation_original = 0; accumulation_reconstructed =0; for k1=1:256 for k2=1:256 accumulation_original = accumulation_original + ((imgdct_8(k1,k2) img_deqtz(k1,k2) )^2); accumulation_reconstructed= accumulation_reconstructed + ((imgdct_8(k1,k2) img_deqtz1(k1,k2) )^2); end end mse_original = (accumulation_original/(256*256)) ^ 0.5; mse_reconstructed = (accumulation_reconstructed/(256*256)) ^ 0.5; mse1_original= mse_original ^ 0.5; mse1_reconstructed= mse_reconstructed ^0.5; psnr_original = (20)*log10((255)/(mse1_original)); psnr_reconstructed = (20)*log10((255)/(mse1_reconstructed)); 95 BIBLIOGRAPHY 1. Edmund Y. Lam and Joseph W. Goodman, “A Mathematical Analysis of the DCT Coefficient distributions for Images”, IEEE Transactions on Image Processing, vol. 9, NO. 10, October 2000 2. Bonnie L. Stephens, Student Thesis on “Image Compression Algorithms”, California State University, Sacramento, August 1996 3. Al Bovik, Department of Electrical and Computer Engineering, UTA Texas, “Handbook of Image & Video Processing”, Academic Press Series, 1999 4. M. I. H. Bhuiyan1 and Rubaiya Rahman, “Modelling of the Video DCT Coefficients”, 5th International Conference on Electrical and Computer Engineering, Dhaka, Bangladesh, ICECE 2008, 20-22 December 2008 5. Gregory K. Wallace, “The JPEG Still Picture Compression Standard”, Submitted for publication in IEEE Transactions on Consumer Electronics, December 1991 6. Arturo San Emeterio Campos, “Run Length Encoding”, 1999 http://www.arturocampos.com/ac_rle.html 7. Texas Instruments Inc., "How to Use the VPBE and VPFE Driver on TMS320DM643x". Dallas, Texas, November 2007 8. Texas Instruments, “TMS320DM64x Digital Media Processor – Product Bulletin (Rev. C)”, 2005 9. Texas Instruments technical Reference, “TMS320DM6437 Evaluation Module”, Spectrum Digital , 2006 10. Randall C. Reiningek and Jerry D. Gibson, “Distributions of the Two-Dimensional DCT Coefficients for Images”, June, 1983 11. Wikipedia, “Gamma Distribution Function” http://en.wikipedia.org/wiki/Gamma_distribution 12. Wikipedia, “Rayleigh Distribution function” http://en.wikipedia.org/wiki/Rayleigh_distribution 96 13. Ying Luo and Rabab K. Ward, “Removing the Blocking Artifacts ofBlock-Based DCT Compressed Images”, IEEE Transaction On Image Processing, Vol. 12, No. 7, July 2003 14. Lukasz Kizewski, Student Thesis on “Image Deblocking Using Local Segmentation”, Monash University, November 2004