Top Banner
1 Image Compression Dr. Md. Imdadul Islam Professor, Department of Computer Science and Engineering Jahangirnagar University
33
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • **Image CompressionDr. Md. Imdadul Islam Professor, Department of Computer Science and Engineering Jahangirnagar Universitywww.juniv.edu www.imdadul.webs.com

  • *Our eyes and ears cannot distinguish subtle change in signals parameter. In such cases, we can use a lossy data compression method.

    JPEG (Joint Photographic Experts Group) encoding is used to compress pictures and graphics, MPEG (Moving Picture Experts Group) encoding is used to compress video, and MP3 (MPEG audio layer 3) for audio compression.

  • *IntroductionThe goal of image compression is to reduce the amount of data required to represent a digital image.i.e., remove redundant data from the imageImage compression is very important for image storage and image transmission.

  • *ApproachesLosslessInformation preservingLow compression ratiosLossyDoes not preserve informationHigh compression ratios

    Tradeoff: image quality vs compression ratio

  • *Step-1 Formation of 88 blocks of Y, I and Q matrixStep-1Step-1 of encoding an image with JPEG is block preparation. Let us assume that JPEG input is a 640480 RGB image with 24 bis/pixel. Since using luminance and chrominance gives better compression, we first compute the luminance, Y and two chrominance I and Q (NTSC) using following formula. Y = 0.3R+0.59G+0.11BI = 0.6R-0.28G-0.32BQ = 0.21R-0.52G+0.31B

  • *Separate matrices are constructed for Y, I and Q each with elements in the range 0-255. Next, square blocks of four pixels are averaged in the I and Q matrices to reduce them to 320240. This reduction is lossy, but the eye barely notices it since eye responds to luminance's more than chrominanceY, I and Q matricesR, G and B matrices

  • *RGB640480Each pixel is of 24 bitsy640480Each pixel is of 8 bitsI320240Each pixel is of 8 bitsQ320240Each pixel is of 8 bits

  • *Finally each matrix is divided into 88 blocks

  • *The Y matrix has 4800 blocks and the other two have 1200 blocksy640480Each pixel is of 8 bitsI320240Each pixel is of 8 bitsQ320240Each pixel is of 8 bits

  • *Now 128 is subtracted from each element of all three matrices t0 put 0 in the middle range. Subtracting -128

  • *Step-2 Discrete Cosine TransformStep-2 of JPEG is to apply DCT to each of the 7200 blocks separately. The output of each DCT is 88 matrix of DCT coefficients. DCT element F(0,0) is the average value of the block called DC component. The other elements tell how much spectral power is presented at each spatial frequency

  • *Inverse DCT (IDCT)88 two-dimensional separable IDCT:

    IDCT can be computed using the same routine as DCT

  • *AC coefficientsDC coefficientDCT8x8 image blockDCT coefficientsLow frequenciesMedium frequenciesHigh frequencies8x8 DCTIncreasing frequency

  • *52 55 61 66 70 61 64 73

    63 59 66 90 109 85 69 72

    62 59 68 113 144 104 66 73

    63 58 71 122 154 106 70 69

    67 61 68 104 126 88 68 70

    79 65 60 70 77 68 58 75

    87 79 69 68 65 76 78 94 85 71 64 59 55 61 65 83

    DCT 609 - 29 - 62 25 55 - 20 - 1 3

    7 - 21 - 62 9 11 - 7 - 6 6

    - 46 8 77 - 25 - 30 10 7 - 5

    - 50 13 35 - 15 - 9 6 0 3

    11 - 8 - 13 - 2 - 1 1 - 4 1

    - 10 1 3 - 3 - 1 0 2 - 1

    - 4 - 1 2 - 1 2 - 3 1 - 2

    - 1 - 1 - 1 - 2 - 1 - 1 0 - 1IDCTPixel domainFrequency domainDCAC8x8 DCT

  • *DCTSubtracting -1288X8 block of pixel values taken from original image

  • *Step-3 QuantizationEach co-efficient of 88 DCT matrix is divided by a weight taken from a tablequantization(16 is the value of the first pixel from quantization matrix)

  • *using 415 (the DC coefficient) and rounding to the nearest integerDCTSubtracting -128quantization8X8 block of pixel values taken from original image(16 is the value of the first pixel from quantization matrix)

  • *Quantization of DCT CoefficientsTypical quantizationmatrices (may be altered depending on data)Quantized term = round( )Original termQuantization factorRestored term = Quantized term * Quantization factor

  • *Since DC values of each 88 matrix is the average value of the respective blocks therefore they change slowly. To obtain the small value we simply subtract the DC coefficient of the previously processed 8x8 pixel block from the DC coefficient of the current block. This value is called the "DPCM difference". No difference is computed from AC components.Step-4 DPCM and zigzag scanning

  • *Now zig-zag scanning is done on each 88 matrix and run-length encoding is applied which generates a list of numbers. Finally Huff-encodes the numbers for storage or transmission.

  • *26 3 0 3 2 6 2 4 1 4 1 1 5 1 2 1 1 1 2 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0Zig-zag scan

  • *JPEG compression

  • *JPEG 27:1

    Original Image

  • *JPEG Compression ExampleOriginal image512 x 512 x 8 bits = 2,097,152 bitsJPEG27:1 reduction=77,673 bits

  • *Transformation of imageIn this section first an RGB image will be read and it will be converted to an 8-bit grayscale and binary image. Finally both the images will be shown.%MATLAB codergb = imread('onion.png'); %read the image subplot(2,2,1)imshow(rgb)title('(a)')I = rgb2gray(rgb);%convert the RGB to gray scale imagesubplot(2,2,2)imshow(I)title('(b)')BW = dither(I);subplot(2,2,3)imshow(BW)title('(c)')The results of the code are shown in fig.1.

  • *

  • *A = im2double(imread('rice.png'));subplot(2,1,1)imshow(A);title('Original Image')D = dctmtx(size(A,1));dct = D*A*D';subplot(2,1,2)imshow(dct)title('Image spectrum after DCT')DCT of an image

  • *I = im2double(imread('rice.png')); fun = @dct2; J = blkproc(I,[8 8],fun); imshow(J)B = blkproc(A,[m n], fun) processes the image A by applying the function fun to each distinct m-by-n block of A, padding A with 0's if necessary DCT is applied on 8X8 block

  • *Huffman Coding:Let us consider four symbols 1, 2, 3 and 4; a random sequence of symbols is [1 3 1 3 4 3 3 4 2 3 2 1] and their probability of occurrence, P=[3/12 2/12 5/12 2/12]. The symbol dictionary is created by the function huffmandict which is used to encode and decode using the huffmanenco and huffmandeco functions.

    sig = repmat([1 3 1 3 4 3 3 4 2 3 2 1],1,5); % random sequence of symbols and will be repeated 5 timessymbols = [1 2 3 4]; % set of symbolsp = [3/12 2/12 5/12 2/12]; % Probability of each symboldict = huffmandict(symbols,p); % Create the dictionary.hcode = huffmanenco(sig,dict); % Encode the data.dhsig = huffmandeco(hcode,dict); % Decode the code.

  • *DCT and Image CompressionIn the JPEG image compression algorithm, the input image is divided into 8-by-8 or 16-by-16 blocks, and the two-dimensional DCT is computed for each block. The DCT coefficients are then quantized, coded, and transmitted. The JPEG receiver (or JPEG file reader) decodes the quantized DCT coefficients, computes the inverse two-dimensional DCT of each block, and then puts the blocks back together into a single image. For typical images, many of the DCT coefficients have values close to zero; these coefficients can be discarded without seriously affecting the quality of the reconstructed image.

  • *I = imread('cameraman.tif');I = im2double(I);T = dctmtx(8);% T is a DCT matrixdct = @(x)T * x * T'; % function of x i.e. dct(x)B = blkproc(I,[8 8],dct);mask = [1 1 1 1 0 0 0 0 1 1 1 0 0 0 0 0 1 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0];B2 = blkproc(B,[8 8],@(x)mask.* x);invdct = @(x)T' * x * T;I2 = blkproc(B2,[8 8],invdct);imshow(I), figure, imshow(I2)B = blkproc(A,[m n], fun) processes the image A by applying the function fun to each distinct m-by-n block of A, padding A with 0's if necessary

  • *

  • * [1 1 1 1 0 0 0 0 1 1 1 0 0 0 0 0 1 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0];

    *DCT base function picturemany fast algorithmsDCT/IDCT 2nd most computationally intensive fctEnergy packing, after Q, less no. to codeIDCT- almost original, almost losslessQ - lossy quantizationerror in a block