CS148: Introduction to Computer Graphics and Imaging...

45
CS148: Introduction to Computer Graphics and Imaging Compression

Transcript of CS148: Introduction to Computer Graphics and Imaging...

Page 1: CS148: Introduction to Computer Graphics and Imaging ...graphics.stanford.edu/.../cs148-09/lectures/compression.pdfCS148 Lecture 16 Pat Hanrahan, Winter 2009 Image and Video Data Rates

CS148: Introduction to Computer Graphics and Imaging

Compression

Page 2: CS148: Introduction to Computer Graphics and Imaging ...graphics.stanford.edu/.../cs148-09/lectures/compression.pdfCS148 Lecture 16 Pat Hanrahan, Winter 2009 Image and Video Data Rates

CS148 Lecture 16 Pat Hanrahan, Winter 2009

Key Concepts

Lossless vs. lossy compressionKolmolgorev complexityPredictive coding / Huffman codingJPEG / Discrete cosine transform (DCT)JPEG2000 / Wavelets

Page 3: CS148: Introduction to Computer Graphics and Imaging ...graphics.stanford.edu/.../cs148-09/lectures/compression.pdfCS148 Lecture 16 Pat Hanrahan, Winter 2009 Image and Video Data Rates

CS148 Lecture 16 Pat Hanrahan, Winter 2009

Image and Video Data Rates

Image 640x480x24b = ~3/4 MB

Full screen Image 1024x768x24b = ~2.5MB

DVD 720x480x24bx30f/s = ~30 MB/s

High Definition DVD 1920x1080x24bx30f/s = ~178MB/s

Film 4000x3000x36bx30f/s = ~1.5GB/s 8 TB for one 90 minute movie!

Page 4: CS148: Introduction to Computer Graphics and Imaging ...graphics.stanford.edu/.../cs148-09/lectures/compression.pdfCS148 Lecture 16 Pat Hanrahan, Winter 2009 Image and Video Data Rates

CS148 Lecture 16 Pat Hanrahan, Winter 2009

Lossless vs. Lossy Compression

Lossless All information stored Exact original can be reconstructed

Lossy Some information discarded Goal: discard information humans won’t

notice Much higher compression ratios possible

Page 5: CS148: Introduction to Computer Graphics and Imaging ...graphics.stanford.edu/.../cs148-09/lectures/compression.pdfCS148 Lecture 16 Pat Hanrahan, Winter 2009 Image and Video Data Rates

CS148 Lecture 16 Pat Hanrahan, Winter 2009

Kolmogorov Complexity

What is the shortest program that can generate the data?

17 KB JPEG

Re(c), Im(c), xmin, xmax, ymin, ymax (24B)

z0 = x + iy

zn+1 = z2n + c

Page 6: CS148: Introduction to Computer Graphics and Imaging ...graphics.stanford.edu/.../cs148-09/lectures/compression.pdfCS148 Lecture 16 Pat Hanrahan, Winter 2009 Image and Video Data Rates

CS148 Lecture 16 Pat Hanrahan, Winter 2009

Run Length Encoding

BWBBBBBBBBBBBBWWWWWWBBBW

BW\{12}B\{6}W\{3}BW

Page 7: CS148: Introduction to Computer Graphics and Imaging ...graphics.stanford.edu/.../cs148-09/lectures/compression.pdfCS148 Lecture 16 Pat Hanrahan, Winter 2009 Image and Video Data Rates

CS148 Lecture 16 Pat Hanrahan, Winter 2009

Code Book

Alphabet: A, B, C, DFrequencies: ¼, ¼, ¼, ¼ Code: 00, 01, 10, 11

ABCDBDAC = 8 characters00 01 10 11 01 11 00 11 = 16 bits

¼ * 2 + ¼ * 2 + ¼ * 2 + ¼ * 2 = 2 bits/ch on average

Page 8: CS148: Introduction to Computer Graphics and Imaging ...graphics.stanford.edu/.../cs148-09/lectures/compression.pdfCS148 Lecture 16 Pat Hanrahan, Winter 2009 Image and Video Data Rates

CS148 Lecture 16 Pat Hanrahan, Winter 2009

Code Book

Alphabet: A, B, C, DFrequencies: ½, ¼, 1/8, 1/8Code: 0, 10, 110, 111

ABACADAB = 8 characters0 10 0 110 0 111 0 10 = 14 bits

½ * 1 + ¼ * 2 +1/8 * 3 +1/8 * 3 = 1 ¾ bits/ch on average

Page 9: CS148: Introduction to Computer Graphics and Imaging ...graphics.stanford.edu/.../cs148-09/lectures/compression.pdfCS148 Lecture 16 Pat Hanrahan, Winter 2009 Image and Video Data Rates

CS148 Lecture 16 Pat Hanrahan, Winter 2009

Entropy

Page 10: CS148: Introduction to Computer Graphics and Imaging ...graphics.stanford.edu/.../cs148-09/lectures/compression.pdfCS148 Lecture 16 Pat Hanrahan, Winter 2009 Image and Video Data Rates

CS148 Lecture 16 Pat Hanrahan, Winter 2009

Huffman CodingA

(.10)

B (.15)

C(.30)

D(.16)

E(.29)

Page 11: CS148: Introduction to Computer Graphics and Imaging ...graphics.stanford.edu/.../cs148-09/lectures/compression.pdfCS148 Lecture 16 Pat Hanrahan, Winter 2009 Image and Video Data Rates

CS148 Lecture 16 Pat Hanrahan, Winter 2009

Huffman Coding

A (.10)

B (.15)

C(.30)

D(.16)

E(.29)

AB (.25)

0 1

Page 12: CS148: Introduction to Computer Graphics and Imaging ...graphics.stanford.edu/.../cs148-09/lectures/compression.pdfCS148 Lecture 16 Pat Hanrahan, Winter 2009 Image and Video Data Rates

CS148 Lecture 16 Pat Hanrahan, Winter 2009

Huffman Coding

A (.10)

B (.15)

C(.30)

D(.16)

E(.29)

AB (.25)

0 1

ABD(.41)

0 1

Page 13: CS148: Introduction to Computer Graphics and Imaging ...graphics.stanford.edu/.../cs148-09/lectures/compression.pdfCS148 Lecture 16 Pat Hanrahan, Winter 2009 Image and Video Data Rates

CS148 Lecture 16 Pat Hanrahan, Winter 2009

Huffman Coding

A (.10)

B (.15)

C(.30)

D(.16)

E(.29)

AB (.25)

0 1

ABD(.41)

0 1 0 1

CE(.59)

Page 14: CS148: Introduction to Computer Graphics and Imaging ...graphics.stanford.edu/.../cs148-09/lectures/compression.pdfCS148 Lecture 16 Pat Hanrahan, Winter 2009 Image and Video Data Rates

CS148 Lecture 16 Pat Hanrahan, Winter 2009

Huffman Coding

A (.10)

B (.15)

C(.30)

D(.16)

E(.29)

AB (.25)

0 1

ABD(.41)

0 1 0 1

CE(.59)

0 1

ABCDE(1.0)

Page 15: CS148: Introduction to Computer Graphics and Imaging ...graphics.stanford.edu/.../cs148-09/lectures/compression.pdfCS148 Lecture 16 Pat Hanrahan, Winter 2009 Image and Video Data Rates

CS148 Lecture 16 Pat Hanrahan, Winter 2009

Lossy Compression

Chroma subsamplingTransform Coding

Fourier / DCT (JPEG) Wavelets (JPEG2000)

Page 16: CS148: Introduction to Computer Graphics and Imaging ...graphics.stanford.edu/.../cs148-09/lectures/compression.pdfCS148 Lecture 16 Pat Hanrahan, Winter 2009 Image and Video Data Rates

CS148 Lecture 16 Pat Hanrahan, Winter 2009

Chroma Subsampling

+

Page 17: CS148: Introduction to Computer Graphics and Imaging ...graphics.stanford.edu/.../cs148-09/lectures/compression.pdfCS148 Lecture 16 Pat Hanrahan, Winter 2009 Image and Video Data Rates

CS148 Lecture 16 Pat Hanrahan, Winter 2009

Transform Coding

X = 2 2 2 2 3 4 5 6 6 6 6 H(X) = 2.0049

D(X) = 2 0 0 0 1 1 1 1 0 0 0 H(D(X)) = 1.3222

Difference Operator

Page 18: CS148: Introduction to Computer Graphics and Imaging ...graphics.stanford.edu/.../cs148-09/lectures/compression.pdfCS148 Lecture 16 Pat Hanrahan, Winter 2009 Image and Video Data Rates

CS148 Lecture 16 Pat Hanrahan, Winter 2009

Bases

Basis vectors b0, b1, … , bn

Express any vector as

a0 * b0 + a1 * b1 + … + an * bn

where the coefficients ai are scalars.

Page 19: CS148: Introduction to Computer Graphics and Imaging ...graphics.stanford.edu/.../cs148-09/lectures/compression.pdfCS148 Lecture 16 Pat Hanrahan, Winter 2009 Image and Video Data Rates

CS148 Lecture 16 Pat Hanrahan, Winter 2009

Pixel Basis

Page 20: CS148: Introduction to Computer Graphics and Imaging ...graphics.stanford.edu/.../cs148-09/lectures/compression.pdfCS148 Lecture 16 Pat Hanrahan, Winter 2009 Image and Video Data Rates

CS148 Lecture 16 Pat Hanrahan, Winter 2009

Another Basis

Page 21: CS148: Introduction to Computer Graphics and Imaging ...graphics.stanford.edu/.../cs148-09/lectures/compression.pdfCS148 Lecture 16 Pat Hanrahan, Winter 2009 Image and Video Data Rates

CS148 Lecture 16 Pat Hanrahan, Winter 2009

Two Observations on Images

Characterize images by the frequencies that are present

The frequencies in natural images fall off as 1/f That is, high frequencies are less common

The human visual system is less sensitive to high frequencies

That is, it is more important to preserve low frequencies than high frequencies

Page 22: CS148: Introduction to Computer Graphics and Imaging ...graphics.stanford.edu/.../cs148-09/lectures/compression.pdfCS148 Lecture 16 Pat Hanrahan, Winter 2009 Image and Video Data Rates

CS148 Lecture 16 Pat Hanrahan, Winter 2009

Discrete Cosine Transform (DCT)

Page 23: CS148: Introduction to Computer Graphics and Imaging ...graphics.stanford.edu/.../cs148-09/lectures/compression.pdfCS148 Lecture 16 Pat Hanrahan, Winter 2009 Image and Video Data Rates

CS148 Lecture 16 Pat Hanrahan, Winter 2009

DCT

Page 24: CS148: Introduction to Computer Graphics and Imaging ...graphics.stanford.edu/.../cs148-09/lectures/compression.pdfCS148 Lecture 16 Pat Hanrahan, Winter 2009 Image and Video Data Rates

CS148 Lecture 16 Pat Hanrahan, Winter 2009

DCT

262,144 pixels 43384 largest terms, 16%(dropped 218,760 terms)

Page 25: CS148: Introduction to Computer Graphics and Imaging ...graphics.stanford.edu/.../cs148-09/lectures/compression.pdfCS148 Lecture 16 Pat Hanrahan, Winter 2009 Image and Video Data Rates

CS148 Lecture 16 Pat Hanrahan, Winter 2009

DCT

262,144 pixels 8353 largest terms, 3.2%(dropped 253,791 terms)

Page 26: CS148: Introduction to Computer Graphics and Imaging ...graphics.stanford.edu/.../cs148-09/lectures/compression.pdfCS148 Lecture 16 Pat Hanrahan, Winter 2009 Image and Video Data Rates

CS148 Lecture 16 Pat Hanrahan, Winter 2009

Quantization

/

DCT Image Quantization Matrix

Quantized DCT Image

=

Page 27: CS148: Introduction to Computer Graphics and Imaging ...graphics.stanford.edu/.../cs148-09/lectures/compression.pdfCS148 Lecture 16 Pat Hanrahan, Winter 2009 Image and Video Data Rates

CS148 Lecture 16 Pat Hanrahan, Winter 2009

Storage Order

Page 28: CS148: Introduction to Computer Graphics and Imaging ...graphics.stanford.edu/.../cs148-09/lectures/compression.pdfCS148 Lecture 16 Pat Hanrahan, Winter 2009 Image and Video Data Rates

CS148 Lecture 16 Pat Hanrahan, Winter 2009

Error

error = abs(original – compressed) * 8

Page 29: CS148: Introduction to Computer Graphics and Imaging ...graphics.stanford.edu/.../cs148-09/lectures/compression.pdfCS148 Lecture 16 Pat Hanrahan, Winter 2009 Image and Video Data Rates

CS148 Lecture 16 Pat Hanrahan, Winter 2009

Peak Signal-to-Noise Ratio

� Original Image (I) and new image (I’)

MSE =1N

N!

i=0

!I(i)" I !(i)!2

RMSE =#

MSE

PSNR = 10 log10

"I2max

MSE

#

= 20 log10

"Imax

RMSE

#

Page 30: CS148: Introduction to Computer Graphics and Imaging ...graphics.stanford.edu/.../cs148-09/lectures/compression.pdfCS148 Lecture 16 Pat Hanrahan, Winter 2009 Image and Video Data Rates

!p" q!2 =

!"""#

"""$

(avg(pi)" avg(qi))2

(max(pi)"max(qi))2

(lum(p)" lum(q))2

· · ·

CS148 Lecture 16 Pat Hanrahan, Winter 2009

Error Metrics

Page 31: CS148: Introduction to Computer Graphics and Imaging ...graphics.stanford.edu/.../cs148-09/lectures/compression.pdfCS148 Lecture 16 Pat Hanrahan, Winter 2009 Image and Video Data Rates

CS148 Lecture 16 Pat Hanrahan, Winter 2009

Wavelets

Page 32: CS148: Introduction to Computer Graphics and Imaging ...graphics.stanford.edu/.../cs148-09/lectures/compression.pdfCS148 Lecture 16 Pat Hanrahan, Winter 2009 Image and Video Data Rates

CS148 Lecture 16 Pat Hanrahan, Winter 2009

Haar Wavelets

Scaling function = average

Wavelet = difference

Transform:S = (A + B)/2D = (A – B)/2

Invert:A = S + DB = S – D

Page 33: CS148: Introduction to Computer Graphics and Imaging ...graphics.stanford.edu/.../cs148-09/lectures/compression.pdfCS148 Lecture 16 Pat Hanrahan, Winter 2009 Image and Video Data Rates

CS148 Lecture 16 Pat Hanrahan, Winter 2009

Haar Wavelets

6 8 5 9 5 5 6 6

7 7 5 6

Page 34: CS148: Introduction to Computer Graphics and Imaging ...graphics.stanford.edu/.../cs148-09/lectures/compression.pdfCS148 Lecture 16 Pat Hanrahan, Winter 2009 Image and Video Data Rates

CS148 Lecture 16 Pat Hanrahan, Winter 2009

Haar Wavelets

6 8 5 9 5 5 6 6

7 7 5 6 -1 -2 0 0

Page 35: CS148: Introduction to Computer Graphics and Imaging ...graphics.stanford.edu/.../cs148-09/lectures/compression.pdfCS148 Lecture 16 Pat Hanrahan, Winter 2009 Image and Video Data Rates

CS148 Lecture 16 Pat Hanrahan, Winter 2009

Haar Wavelets

6 8 5 9 5 5 6 6

7 7 5 6 -1 -2 0 0

1 Transform Step

Averages• Smoothed version of signal• Lower resolution image

Differences• Local, high frequencies• Details missing from low resolution part

Page 36: CS148: Introduction to Computer Graphics and Imaging ...graphics.stanford.edu/.../cs148-09/lectures/compression.pdfCS148 Lecture 16 Pat Hanrahan, Winter 2009 Image and Video Data Rates

CS148 Lecture 16 Pat Hanrahan, Winter 2009

Haar Wavelets

6 8 5 9 5 5 6 6

7 7 5 6 -1 -2 0 0

-1 -2 0 0

Page 37: CS148: Introduction to Computer Graphics and Imaging ...graphics.stanford.edu/.../cs148-09/lectures/compression.pdfCS148 Lecture 16 Pat Hanrahan, Winter 2009 Image and Video Data Rates

CS148 Lecture 16 Pat Hanrahan, Winter 2009

Haar Wavelets

6 8 5 9 5 5 6 6

7 7 5 6 -1 -2 0 0

7 -1 -2 0 05.5

Page 38: CS148: Introduction to Computer Graphics and Imaging ...graphics.stanford.edu/.../cs148-09/lectures/compression.pdfCS148 Lecture 16 Pat Hanrahan, Winter 2009 Image and Video Data Rates

CS148 Lecture 16 Pat Hanrahan, Winter 2009

Haar Wavelets

6 8 5 9 5 5 6 6

7 7 5 6 -1 -2 0 0

7 0 -.5 -1 -2 0 05.5

Page 39: CS148: Introduction to Computer Graphics and Imaging ...graphics.stanford.edu/.../cs148-09/lectures/compression.pdfCS148 Lecture 16 Pat Hanrahan, Winter 2009 Image and Video Data Rates

CS148 Lecture 16 Pat Hanrahan, Winter 2009

Haar Wavelets

6 8 5 9 5 5 6 6

7 7 5 6 -1 -2 0 0

7 0 -1 -2 0 0

0 -1 -2 0 0

-.5

-.5

5.5

Page 40: CS148: Introduction to Computer Graphics and Imaging ...graphics.stanford.edu/.../cs148-09/lectures/compression.pdfCS148 Lecture 16 Pat Hanrahan, Winter 2009 Image and Video Data Rates

CS148 Lecture 16 Pat Hanrahan, Winter 2009

Haar Wavelets

6 8 5 9 5 5 6 6

7 7 5 6 -1 -2 0 0

7 5.5 0 -1 -2 0 0

6.25 0 -1 -2 0 0

-.5

-.5

Page 41: CS148: Introduction to Computer Graphics and Imaging ...graphics.stanford.edu/.../cs148-09/lectures/compression.pdfCS148 Lecture 16 Pat Hanrahan, Winter 2009 Image and Video Data Rates

CS148 Lecture 16 Pat Hanrahan, Winter 2009

Haar Wavelets

6 8 5 9 5 5 6 6

7 7 5 6 -1 -2 0 0

7 0 -1 -2 0 0

6.25.75 0 -1 -2 0 0

-.5

-.5

5.5

Page 42: CS148: Introduction to Computer Graphics and Imaging ...graphics.stanford.edu/.../cs148-09/lectures/compression.pdfCS148 Lecture 16 Pat Hanrahan, Winter 2009 Image and Video Data Rates

CS148 Lecture 16 Pat Hanrahan, Winter 2009

Haar Wavelets

6 8 5 9 5 5 6 6

6.25 0 -1 -2 0 0

Full Transform

High Res DetailsMedium Res DetailsLow Res DetailAverage Value

-.5.75

Page 43: CS148: Introduction to Computer Graphics and Imaging ...graphics.stanford.edu/.../cs148-09/lectures/compression.pdfCS148 Lecture 16 Pat Hanrahan, Winter 2009 Image and Video Data Rates

CS148 Lecture 16 Pat Hanrahan, Winter 2009

2D Wavelet Transform

Standard– Apply full transform horizontally, then full

transform vertically– Creates long, thin basis functions – bad for

image compressionNon-standard

– Repeatedly apply 1 step horizontally, then 1 step vertically

– Creates square basis functions – good for image compression

Page 44: CS148: Introduction to Computer Graphics and Imaging ...graphics.stanford.edu/.../cs148-09/lectures/compression.pdfCS148 Lecture 16 Pat Hanrahan, Winter 2009 Image and Video Data Rates

CS148 Lecture 16 Pat Hanrahan, Winter 2009

What else needs compression?

VideoTextures (different requirements than plain

images)Geometry (including animations)Anything else?

Page 45: CS148: Introduction to Computer Graphics and Imaging ...graphics.stanford.edu/.../cs148-09/lectures/compression.pdfCS148 Lecture 16 Pat Hanrahan, Winter 2009 Image and Video Data Rates

CS148 Lecture 16 Pat Hanrahan, Winter 2009

Things to Remember

LosslessNo error, but not lots of compression

LossyError can be driven by human perceptionMore error, but lots of compressionDifferent error metrics (max, avg, perceptual)

Different Basis FunctionsJPEG / Discrete cosine transform (DCT)JPEG2000 / Wavelets