33
CS559: Computer Graphics Lecture 4: Compositing and Resampling Li Zhang Spring 2008

CS559: Computer Graphics Lecture 4: Compositing and Resampling Li Zhang Spring 2008

Embed Size (px)

Citation preview

Page 1: CS559: Computer Graphics Lecture 4: Compositing and Resampling Li Zhang Spring 2008

CS559: Computer Graphics

Lecture 4: Compositing and ResamplingLi Zhang

Spring 2008

Page 2: CS559: Computer Graphics Lecture 4: Compositing and Resampling Li Zhang Spring 2008

Compositing• Compositing combines components from two or

more images to make a new image– Special effects are easier to control when done in

isolation– Even many all live-action sequences are more safely

shot in different layers

Page 3: CS559: Computer Graphics Lecture 4: Compositing and Resampling Li Zhang Spring 2008

Compositing• Compositing combines components from two or

more images to make a new image– Special effects are easier to control when done in

isolation– Even many all live-action sequences are more safely

shot in different layers

Page 4: CS559: Computer Graphics Lecture 4: Compositing and Resampling Li Zhang Spring 2008

Perfect Storm

Page 5: CS559: Computer Graphics Lecture 4: Compositing and Resampling Li Zhang Spring 2008

Animated Example

over =

Page 6: CS559: Computer Graphics Lecture 4: Compositing and Resampling Li Zhang Spring 2008

Mattes

• A matte is an image that shows which parts of another image are foreground objects

• Term dates from film editing and cartoon production

• How would I use a matte to insert an object into a background?

• How are mattes usually generated for television?

Page 7: CS559: Computer Graphics Lecture 4: Compositing and Resampling Li Zhang Spring 2008

Working with Mattes• To insert an object into a background

– Call the image of the object the source– Put the background into the destination– For all the source pixels, if the matte is white, copy the

pixel, otherwise leave it unchanged• To generate mattes:

– Use smart selection tools in Photoshop or similar• They outline the object and convert the outline to a

matte– Blue Screen: Photograph/film the object in front of a blue

background, then consider all the blue pixels in the image to be the background

Page 8: CS559: Computer Graphics Lecture 4: Compositing and Resampling Li Zhang Spring 2008

Compositing

• Compositing is the term for combining images, one over the other– Used to put special effects into live action

• Or live action into special effects

Page 9: CS559: Computer Graphics Lecture 4: Compositing and Resampling Li Zhang Spring 2008

Alpha• Basic idea: Encode opacity information in the image• Add an extra channel, the alpha channel, to each image

– For each pixel, store R, G, B and Alpha– alpha = 1 implies full opacity at a pixel– alpha = 0 implies completely clear pixels

• Images are now in RGBA format, and typically 32 bits per pixel (8 bits for alpha)

• All images in the project are in this format

Page 10: CS559: Computer Graphics Lecture 4: Compositing and Resampling Li Zhang Spring 2008

Pre-Multiplied Alpha• Instead of storing (R,G,B,), store (R,G,B,)• The compositing operations in the next several

slides are easier with pre-multiplied alpha• To display and do color conversions, must

extract RGB by dividing out – =0 is always black– Some loss of precision as gets small, but generally

not a big problem

Page 11: CS559: Computer Graphics Lecture 4: Compositing and Resampling Li Zhang Spring 2008

Basic Compositing Operation• At each pixel, combine the pixel data from f and

the pixel data from g with the equation:

gffo ccc )1(1

],,,[ ffffffff BGRc

],,,[ gggggggg BGRc

“Over” Operator

Page 12: CS559: Computer Graphics Lecture 4: Compositing and Resampling Li Zhang Spring 2008

“Over” Operator

• If there’s some f, get f, otherwise get g

over =

gffo ccc )1(1

Page 13: CS559: Computer Graphics Lecture 4: Compositing and Resampling Li Zhang Spring 2008

n T

nTxngxf )(sinc][)(then

,freq sampling the21 above freq no has f(x) If

Reconstruction theorem

][ng

.for sequence sampling thebe )(][Let f(x)nTfng

x

xx

)sin(

)sinc(

1 2 3 4-4 -3 -2 -1 0T 2T-3T 3T-2T -T

Page 14: CS559: Computer Graphics Lecture 4: Compositing and Resampling Li Zhang Spring 2008

Reconstruction theorem

n T

nTxngxf )(sinc][)(then

,freq sampling the21 above freq no has f(x) If

sequence. sampling thebe )(][Let nTfng

Page 15: CS559: Computer Graphics Lecture 4: Compositing and Resampling Li Zhang Spring 2008

Reconstruction filters• The sinc filter, while “ideal”, has two drawbacks:

– It has large support (slow to compute)– It introduces ringing in practice

• We can choose from many other filters…

Page 16: CS559: Computer Graphics Lecture 4: Compositing and Resampling Li Zhang Spring 2008

Cubic filters• Mitchell and Netravali (1988) experimented with

cubic filters of the following form:

• The choice of B or C trades off between being too blurry or having too much ringing. B=C=1/3 was their “visually best” choice.

• The resulting reconstruction filter is often called the “Mitchell filter.”

3 2

3 2

(12 9 6 ) ( 18 12 6 ) (6 2 ) 11

( ) (( 6 ) (6 30 ) ( 12 48 ) (8 24 ) 1 26

0

B C x B C x B x

r x B C x B C x B C x B C x

otherwise

Page 17: CS559: Computer Graphics Lecture 4: Compositing and Resampling Li Zhang Spring 2008

Practical upsampling• When resampling a function (e.g., when resizing

an image), you do not need to reconstruct the complete continuous function.

• For zooming in on a function, you need only use a reconstruction filter and evaluate as needed for each new sample.

• Here’s an example using a cubic filter:

n

nTxkngxf )(][)(

Page 18: CS559: Computer Graphics Lecture 4: Compositing and Resampling Li Zhang Spring 2008

Practical upsampling• This can also be viewed as:

1. putting the reconstruction filter at the desired location

2. evaluating at the original sample positions3. taking products with the sample values themselves4. summing it up

n

xnTkngxf )(][)(

Page 19: CS559: Computer Graphics Lecture 4: Compositing and Resampling Li Zhang Spring 2008

2D Fourier transform2 ( )( , ) ( , ) x yi s x s y

x yF s s f x y e dxdy

2 ( )( , ) ( , ) x yi s x s yx y x yf x y F s s e ds ds

Spatial domain Frequency domain

( , )x yF s s( , )f x y

High frequency coefficients are small.

Page 20: CS559: Computer Graphics Lecture 4: Compositing and Resampling Li Zhang Spring 2008

• We can also perform reconstruction in 2D…

Reconstruction filters in 2D

Page 21: CS559: Computer Graphics Lecture 4: Compositing and Resampling Li Zhang Spring 2008

Reconstruction filters in 2D1D example:

n n+1 n+2n-1 x

otherwise 0

1 1)(1

xxxk D

)1(][)( xngxf

nxx

g[n]

g[n+1]

)1()1(],[),(

,

yxmngyxf

myynxx

otherwise 0

1,1 )1()1(),(2

yxyxyxk D

g[n,m] g[n+1,m]

g[n,m+1] g[n+1,m+1]

(x,y)

2D example:

xng ]1[

)1(],1[ yxmng yxmng )1(]1,[

yxmng ]1,1[

Page 22: CS559: Computer Graphics Lecture 4: Compositing and Resampling Li Zhang Spring 2008

Reconstruction filters in 2D

We’ve been looking at separable filters:

How might you use this fact for efficient resampling in 2D?

)()(),( 112 ykxkyxk DDD

Page 23: CS559: Computer Graphics Lecture 4: Compositing and Resampling Li Zhang Spring 2008

Image Downsampling

Throw away every other row and column to create a 1/2 size image

- called image sub-sampling

1/4

1/8

Page 24: CS559: Computer Graphics Lecture 4: Compositing and Resampling Li Zhang Spring 2008

Image sub-sampling

1/4 (2x zoom) 1/8 (4x zoom)

Why does this look so crufty?

1/2

Minimum Sampling requirement is not satisfied – resulting in Aliasing effect

Page 25: CS559: Computer Graphics Lecture 4: Compositing and Resampling Li Zhang Spring 2008

Practical downsampling• Downsampling is similar, but filter has larger

support and smaller amplitude.• Operationally:

– given the downsampling rate, d, ratio of new sampling rate to old sampling rate

1. Choose reconstruction filter2. Stretch the filter by 1/d and scale it down by d3. Follow upsampling procedure (previous slides) to compute new

values

Page 26: CS559: Computer Graphics Lecture 4: Compositing and Resampling Li Zhang Spring 2008

Subsampling with Gaussian pre-filtering

G 1/4 G 1/8Gaussian 1/2

• Solution: filter the image, then subsample

Page 27: CS559: Computer Graphics Lecture 4: Compositing and Resampling Li Zhang Spring 2008

Compare with...

1/4 (2x zoom) 1/8 (4x zoom)1/2

Page 28: CS559: Computer Graphics Lecture 4: Compositing and Resampling Li Zhang Spring 2008

Explanation using Fourier Transform

Convolution

Page 29: CS559: Computer Graphics Lecture 4: Compositing and Resampling Li Zhang Spring 2008

Explanation using Fourier Transform

*

f(x,y)

h(x,y)

g(x,y)

|F(sx,sy)|

|H(sx,sy)|

|G(sx,sy)|

Page 30: CS559: Computer Graphics Lecture 4: Compositing and Resampling Li Zhang Spring 2008

Convolution Definition')'()'()()()( dxxxgxfxgxfxg

Page 31: CS559: Computer Graphics Lecture 4: Compositing and Resampling Li Zhang Spring 2008

')'()'()()()( dxxxgxfxgxfxg

9/23/04© University of Wisconsin, CS559 Spring 2004

Convolution Example

Result

Filter Function

http://www.cs.brown.edu/exploratories/freeSoftware/repository/edu/brown/cs/exploratories/applets/specialFunctionConvolution/special_function_convolution_java_browser.html

Page 32: CS559: Computer Graphics Lecture 4: Compositing and Resampling Li Zhang Spring 2008

• Convolution theorem: Convolution in the spatial domain is equivalent to multiplication in the frequency domain.

• Symmetric theorem: Convolution in the frequency domain is equivalent to multiplication in the spatial domain.

Convolution theorems

f h F H

f h F H

Page 33: CS559: Computer Graphics Lecture 4: Compositing and Resampling Li Zhang Spring 2008

Explanation using Fourier Transform

Convolution