CS148: Introduction to Computer Graphics and Imaging Basic...

of 26 /26
CS148: Introduction to Computer Graphics and Imaging Basic Signal Processing: Sampling, Aliasing, Antialiasing No Jaggies CS148 Lecture 13 Pat Hanrahan, Fall 2011 Key Concepts Frequency space Filters and convolution Sampling and the Nyquist frequency Aliasing and Antialiasing

Embed Size (px)

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

Page 1: CS148: Introduction to Computer Graphics and Imaging Basic ...graphics.stanford.edu/courses/cs148-11-fall/lectures/sampling.pdf · Sampling and aliasing Image generation involves

CS148: Introduction to Computer Graphics and Imaging

Basic Signal Processing:Sampling, Aliasing, Antialiasing

No Jaggies

CS148 Lecture 13 Pat Hanrahan, Fall 2011

Key Concepts

Frequency spaceFilters and convolutionSampling and the Nyquist frequencyAliasing and Antialiasing

Page 2: CS148: Introduction to Computer Graphics and Imaging Basic ...graphics.stanford.edu/courses/cs148-11-fall/lectures/sampling.pdf · Sampling and aliasing Image generation involves

Frequency Space

sin 2πxCS148 Lecture 13 Pat Hanrahan, Fall 2011

Sines and Cosines

cos 2πx

Page 3: CS148: Introduction to Computer Graphics and Imaging Basic ...graphics.stanford.edu/courses/cs148-11-fall/lectures/sampling.pdf · Sampling and aliasing Image generation involves

Frequencies

cos 4πx

cos 2πfx

f = 1f =

1

T

f = 2

CS148 Lecture 13 Pat Hanrahan, Fall 2011

cos 2πx

Euler’s Formula

Odd (-x)

Therefore

Hence, use complex exponentials for sines/cosines

Recall Complex Exponentials

CS148 Lecture 13 Pat Hanrahan, Fall 2011

e

jx = cosx+ j sinx

e

−jx = cos−x+ j sin−x = cosx− j sinx

cosx =e

jx + e

−jx

2sinx =

e

jx − e

−jx

2j

Page 4: CS148: Introduction to Computer Graphics and Imaging Basic ...graphics.stanford.edu/courses/cs148-11-fall/lectures/sampling.pdf · Sampling and aliasing Image generation involves

CS148 Lecture 13 Pat Hanrahan, Fall 2011

Constant

Spatial Domain Frequency Domain

sin(2π/32)x

CS148 Lecture 13 Pat Hanrahan, Fall 2011

Spatial Domain Frequency DomainFrequency = 1/32; 32 pixels per cycle

Page 5: CS148: Introduction to Computer Graphics and Imaging Basic ...graphics.stanford.edu/courses/cs148-11-fall/lectures/sampling.pdf · Sampling and aliasing Image generation involves

sin(2π/16)x

CS148 Lecture 13 Pat Hanrahan, Fall 2011

Spatial Domain Frequency Domain

sin(2π/16)y

CS148 Lecture 13 Pat Hanrahan, Fall 2011

Spatial Domain Frequency Domain

Page 6: CS148: Introduction to Computer Graphics and Imaging Basic ...graphics.stanford.edu/courses/cs148-11-fall/lectures/sampling.pdf · Sampling and aliasing Image generation involves

sin(2π/32)x× sin(2π/16)y

CS148 Lecture 13 Pat Hanrahan, Fall 2011

Spatial Domain Frequency Domain

e

−r

2/162

CS148 Lecture 13 Pat Hanrahan, Fall 2011

Spatial Domain Frequency Domain

Page 7: CS148: Introduction to Computer Graphics and Imaging Basic ...graphics.stanford.edu/courses/cs148-11-fall/lectures/sampling.pdf · Sampling and aliasing Image generation involves

e

−r

2/322

CS148 Lecture 13 Pat Hanrahan, Fall 2011

Spatial Domain Frequency Domain

e

−x

2/322 × e

−y

2/162

CS148 Lecture 13 Pat Hanrahan, Fall 2011

Spatial Domain Frequency Domain

Page 8: CS148: Introduction to Computer Graphics and Imaging Basic ...graphics.stanford.edu/courses/cs148-11-fall/lectures/sampling.pdf · Sampling and aliasing Image generation involves

Rotate 45

CS148 Lecture 13 Pat Hanrahan, Fall 2011

Spatial Domain Frequency Domain

e

−x

2/322 × e

−y

2/162

Filtering

Page 9: CS148: Introduction to Computer Graphics and Imaging Basic ...graphics.stanford.edu/courses/cs148-11-fall/lectures/sampling.pdf · Sampling and aliasing Image generation involves

CS148 Lecture 13 Pat Hanrahan, Fall 2011

My Humble Frequencies

Spatial Domain Frequency Domain

CS148 Lecture 13 Pat Hanrahan, Fall 2011

Remove Low Frequencies (Edges)

Spatial Domain Frequency Domain

Page 10: CS148: Introduction to Computer Graphics and Imaging Basic ...graphics.stanford.edu/courses/cs148-11-fall/lectures/sampling.pdf · Sampling and aliasing Image generation involves

CS148 Lecture 13 Pat Hanrahan, Fall 2011

Remove High Frequencies (Blur)

Spatial Domain Frequency Domain

CS148 Lecture 13 Pat Hanrahan, Fall 2011

Remove Low and High Frequencies

Spatial Domain Frequency Domain

Page 11: CS148: Introduction to Computer Graphics and Imaging Basic ...graphics.stanford.edu/courses/cs148-11-fall/lectures/sampling.pdf · Sampling and aliasing Image generation involves

CS148 Lecture 13 Pat Hanrahan, Fall 2011

Remove Low and High Frequencies

Spatial Domain Frequency Domain

Filters = Convolution

Page 12: CS148: Introduction to Computer Graphics and Imaging Basic ...graphics.stanford.edu/courses/cs148-11-fall/lectures/sampling.pdf · Sampling and aliasing Image generation involves

CS148 Lecture 13 Pat Hanrahan, Fall 2011

Convolution

1 3 0 4 2 1

1 2

1 * 1 + 3 * 2 = 7

CS148 Lecture 13 Pat Hanrahan, Fall 2011

Convolution

7

1 3 0 4 2 1

1 2

Page 13: CS148: Introduction to Computer Graphics and Imaging Basic ...graphics.stanford.edu/courses/cs148-11-fall/lectures/sampling.pdf · Sampling and aliasing Image generation involves

CS148 Lecture 13 Pat Hanrahan, Fall 2011

Convolution

3 * 1 + 0 * 2 = 3

1 3 0 4 2 1

1 2

7 3

CS148 Lecture 13 Pat Hanrahan, Fall 2011

Convolution

0 * 1 + 4 * 2 = 8

1 3 0 4 2 1

1 2

7 3 8

Page 14: CS148: Introduction to Computer Graphics and Imaging Basic ...graphics.stanford.edu/courses/cs148-11-fall/lectures/sampling.pdf · Sampling and aliasing Image generation involves

CS148 Lecture 13 Pat Hanrahan, Fall 2011

Convolution Theorem

A �lter can be implemented in the spatial domain using convolution

A �lter can also be implemented in the frequency domainConvert image to frequency domainConvert �lter to frequency domainMultiply �lter times image in frequency domainConvert result to the spatial domain

CS148 Lecture 13 Pat Hanrahan, Fall 2011

Box Filter

1 1

1 1

Page 15: CS148: Introduction to Computer Graphics and Imaging Basic ...graphics.stanford.edu/courses/cs148-11-fall/lectures/sampling.pdf · Sampling and aliasing Image generation involves

CS148 Lecture 13 Pat Hanrahan, Fall 2011

Box Filter = Low-Pass Filter

Spatial Domain Frequency Domain

CS148 Lecture 13 Pat Hanrahan, Fall 2011

Wider Filters, Lower Frequencies

Spatial Domain Frequency Domain

Page 16: CS148: Introduction to Computer Graphics and Imaging Basic ...graphics.stanford.edu/courses/cs148-11-fall/lectures/sampling.pdf · Sampling and aliasing Image generation involves

CS148 Lecture 13 Pat Hanrahan, Fall 2011

Size of Filter

As a �lter is localized in space, it spreads out in frequency

Conversely, as a �lter is localized in frequency, it spreads out in space

A box �lter is very localized in space; it has in�nite extent in frequency space

CS148 Lecture 13 Pat Hanrahan, Fall 2011

Ef�ciency?

When would it be faster to apply the �lter in the spatial domain?

When would it be faster to apply the �lter in the frequency domain?

Page 17: CS148: Introduction to Computer Graphics and Imaging Basic ...graphics.stanford.edu/courses/cs148-11-fall/lectures/sampling.pdf · Sampling and aliasing Image generation involves

Sampling

CS148 Lecture 13 Pat Hanrahan, Fall 2011

Image Generation = Sampling

Evaluating a function at a point is sampling

for( int x = 0; x < xmax; x++ )

for( int y = 0; y < ymax; y++ )

Image[x][y] = f(x,y);

Rasterization is equivalent to evaluating the function inside(triangle,x,y)

Page 18: CS148: Introduction to Computer Graphics and Imaging Basic ...graphics.stanford.edu/courses/cs148-11-fall/lectures/sampling.pdf · Sampling and aliasing Image generation involves

CS148 Lecture 13 Pat Hanrahan, Fall 2011

Sampling Causes Jaggies

Retort, by Don Mitchell

Staircase pattern or jaggies

CS148 Lecture 13 Pat Hanrahan, Fall 2011

Sampling in Computer Graphics

Artifacts due to sampling - AliasingJaggies – sampling in spaceWagon wheel effect – sampling in timeTemporal strobing – sampling in space-timeMoire – sampling texture coordinatesSparkling highlights – sampling normals

Preventing these artifacts - Antialiasing

Page 19: CS148: Introduction to Computer Graphics and Imaging Basic ...graphics.stanford.edu/courses/cs148-11-fall/lectures/sampling.pdf · Sampling and aliasing Image generation involves

Aliasing

Wagon Wheel Effect

http://www.michaelbach.de/ot/mot_wagonWheel/

Page 20: CS148: Introduction to Computer Graphics and Imaging Basic ...graphics.stanford.edu/courses/cs148-11-fall/lectures/sampling.pdf · Sampling and aliasing Image generation involves

CS148 Lecture 13 Pat Hanrahan, Fall 2011

“Aliases”

These two sine waves are indistinguishable Indistinguishable frequencies are called “aliases”

CS148 Lecture 13 Pat Hanrahan, Fall 2011

Nyquist Frequency

De�nition: The Nyquist frequency is ½ the sampling frequency (1/Ts)

Frequencies above the Nyquist frequency appear as aliases

No aliases appear if the function being sampled has no frequencies above the Nyquist frequency

Page 21: CS148: Introduction to Computer Graphics and Imaging Basic ...graphics.stanford.edu/courses/cs148-11-fall/lectures/sampling.pdf · Sampling and aliasing Image generation involves

Antialiasing

CS148 Lecture 13 Pat Hanrahan, Fall 2011

Antialiasing

Simple idea:

Remove frequencies above the Nyquist frequency before sampling

How? Filtering before sampling

Page 22: CS148: Introduction to Computer Graphics and Imaging Basic ...graphics.stanford.edu/courses/cs148-11-fall/lectures/sampling.pdf · Sampling and aliasing Image generation involves

CS148 Lecture 13 Pat Hanrahan, Fall 2011

Pre�ltering by Computing Coverage

A 1 pixel box �lter removes frequencies whose period is less than or equal to 1 pixel

Original

Filtered

CS148 Lecture 13 Pat Hanrahan, Fall 2011

Point- vs. Area-Sampled

Point Area

Checkerboard sequence by Tom Duff

Page 23: CS148: Introduction to Computer Graphics and Imaging Basic ...graphics.stanford.edu/courses/cs148-11-fall/lectures/sampling.pdf · Sampling and aliasing Image generation involves

CS148 Lecture 13 Pat Hanrahan, Fall 2011

Antialiasing

Jaggies Pre�lter

CS148 Lecture 13 Pat Hanrahan, Fall 2011

Antialiasing vs. Blurred Aliases

Blurred Jaggies Pre�lter

Page 24: CS148: Introduction to Computer Graphics and Imaging Basic ...graphics.stanford.edu/courses/cs148-11-fall/lectures/sampling.pdf · Sampling and aliasing Image generation involves

CS148 Lecture 13 Pat Hanrahan, Fall 2011

Things to Remember

Signal processingFrequency domain vs. spatial domainFilters in the frequency domainFilters in the spatial domain = convolution

Sampling and aliasingImage generation involves sampling

May also sample geometry, motion, …

Nyquist frequency is ½ the sampling rateFrequencies above the Nyquist frequency appear as other frequencies – aliasesAntialiasing – Filter before sampling

Extra Slides

Supersampling

Page 25: CS148: Introduction to Computer Graphics and Imaging Basic ...graphics.stanford.edu/courses/cs148-11-fall/lectures/sampling.pdf · Sampling and aliasing Image generation involves

CS148 Lecture 13 Pat Hanrahan, Fall 2011

Approximate a box �lter by taking more samples and averaging them together

Supersampling

4 x 4 supersampling

CS148 Lecture 13 Pat Hanrahan, Fall 2011

Point-sampling vs. Super-sampling

Point 4x4 Super-sampled

Checkerboard sequence by Tom Duff

Page 26: CS148: Introduction to Computer Graphics and Imaging Basic ...graphics.stanford.edu/courses/cs148-11-fall/lectures/sampling.pdf · Sampling and aliasing Image generation involves

CS148 Lecture 13 Pat Hanrahan, Fall 2011

Area-Sampling vs. Super-sampling

Exact Area 4x4 Super-sampled