13.07.2015 Views

Discrete Cosine Transform and JPEG

Discrete Cosine Transform and JPEG

Discrete Cosine Transform and JPEG

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

ColorsRGB:⎛ ⎞rp RGB = ⎝g⎠ ,bwhere r, g, b are the red, green, <strong>and</strong> blue components of the pixel p.YCbCr:⎛ ⎞yp YCbCr = ⎝c b⎠ ,c rwhere y is the luminance, <strong>and</strong> c b <strong>and</strong> c r are the chroma components.RGB <strong>and</strong> YCbCr are related by⎛⎞ ⎛ ⎞0.2990 0.5870 0.11400p YCbCr = ⎝−0.1687 −0.3313 0.5000 ⎠ p RGB + ⎝128⎠ .0.5000 −0.4187 −0.0813128L. Karlsson () <strong>Discrete</strong> <strong>Cosine</strong> <strong>Transform</strong> <strong>and</strong> <strong>JPEG</strong> 4 / 18


ImagesMATLAB comm<strong>and</strong>s:Load an image: A = imread(filename).◮ A is an m × n × 3 array of type uint8 (integers 0–255).◮ The pixels of A are in the RGB format.◮ A(i,j,:) is the pixel on row i <strong>and</strong> column j.◮ A(:,:,2) is the green component of each pixel.Display an image: image(A); axis equal; axis off.L. Karlsson () <strong>Discrete</strong> <strong>Cosine</strong> <strong>Transform</strong> <strong>and</strong> <strong>JPEG</strong> 5 / 18


Conversion between RGB <strong>and</strong> YCbCrMATLAB comm<strong>and</strong>s:RGB to YCbCr: B = rgb2ycbcr(A)YCbCr to RGB: A = ycbcr2rgb(A)L. Karlsson () <strong>Discrete</strong> <strong>Cosine</strong> <strong>Transform</strong> <strong>and</strong> <strong>JPEG</strong> 7 / 18


Part II<strong>Discrete</strong> <strong>Cosine</strong> <strong>Transform</strong> (DCT)L. Karlsson () <strong>Discrete</strong> <strong>Cosine</strong> <strong>Transform</strong> <strong>and</strong> <strong>JPEG</strong> 8 / 18


1-D DCTComponent-wise formInput: vector x ∈ R n .Output: vector y ∈ R n .<strong>Transform</strong> in component-wise form:√2n∑( )(2i − 1)(u − 1)πy u = C(u) x i cos,n2ni=1whereC(u){1/ √ 2 if u = 1,1 otherwise.L. Karlsson () <strong>Discrete</strong> <strong>Cosine</strong> <strong>Transform</strong> <strong>and</strong> <strong>JPEG</strong> 9 / 18


1-D DCTMatrix formInput: vector x ∈ R n .Output: vector y ∈ R n .<strong>Transform</strong> in matrix form:whereZ ∈ R n×n <strong>and</strong> z ij = C(i)In MATLAB: Z = dctmtx(n).Z is orthogonal, i.e., Z −1 = Z T .y = Zx,√2n cos ( (2j − 1)(i − 1)π2n).L. Karlsson () <strong>Discrete</strong> <strong>Cosine</strong> <strong>Transform</strong> <strong>and</strong> <strong>JPEG</strong> 10 / 18


2-D DCT(Forward) DCT:Input: matrix X ∈ R n×n .Output: matrix Y ∈ R n×n .<strong>Transform</strong> in matrix form:Y = ZXZ T .In MATLAB: Y = dct2(X).Inverse DCT:Input: matrix Y ∈ R n×n .Output: matrix X ∈ R n×n .<strong>Transform</strong> in matrix form:In MATLAB: X = idct2(Y).X = Z T YZ.L. Karlsson () <strong>Discrete</strong> <strong>Cosine</strong> <strong>Transform</strong> <strong>and</strong> <strong>JPEG</strong> 11 / 18


DCT basis functionsB ij = Z(e i e T j )Z TL. Karlsson () <strong>Discrete</strong> <strong>Cosine</strong> <strong>Transform</strong> <strong>and</strong> <strong>JPEG</strong> 12 / 18


Part III<strong>JPEG</strong>L. Karlsson () <strong>Discrete</strong> <strong>Cosine</strong> <strong>Transform</strong> <strong>and</strong> <strong>JPEG</strong> 13 / 18


<strong>JPEG</strong>Joint Photographic Experts Group (<strong>JPEG</strong>)A continuous tone image compression st<strong>and</strong>ard.Builds on the 2-D DCT of size 8 × 8.Uses YCbCr images, treated as three separate images.L. Karlsson () <strong>Discrete</strong> <strong>Cosine</strong> <strong>Transform</strong> <strong>and</strong> <strong>JPEG</strong> 14 / 18


<strong>JPEG</strong> compression algorithmConvert the image to YCbCr.Partition the image into blocks of size 8 × 8.For each component of each block:◮ Apply the 2-D DCT.◮ Quantize (discards information).Encode (compresses).L. Karlsson () <strong>Discrete</strong> <strong>Cosine</strong> <strong>Transform</strong> <strong>and</strong> <strong>JPEG</strong> 15 / 18


<strong>JPEG</strong> decompression algorithmDecode (decompresses).Partition the image into blocks of size 8 × 8.For each component of each block:◮ Dequantize.◮ Apply the 2-D inverse DCT.Convert the image to RGB.L. Karlsson () <strong>Discrete</strong> <strong>Cosine</strong> <strong>Transform</strong> <strong>and</strong> <strong>JPEG</strong> 16 / 18


QuantizationInput: matrix X ∈ R n×n <strong>and</strong> quantization table Q ∈ Z n×n .Output: quantized matrix Y ∈ Z n×n .<strong>Transform</strong>ation in component-wise form:y ij = round(x ij /q ij ).L. Karlsson () <strong>Discrete</strong> <strong>Cosine</strong> <strong>Transform</strong> <strong>and</strong> <strong>JPEG</strong> 17 / 18


DequantizationInput: quantized matrix Y ∈ Z n×n <strong>and</strong> quantization table Q ∈ Z n×n .Output: dequantized matrix X ∈ Z n×n .<strong>Transform</strong>ation in component-wise form:x ij = y ij q ij .L. Karlsson () <strong>Discrete</strong> <strong>Cosine</strong> <strong>Transform</strong> <strong>and</strong> <strong>JPEG</strong> 18 / 18

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!