Computational Photography Lecture 3 – filtering and frequencies CS 590-134 (future 572) Spring 2016 Prof. Alex Berg (Credits to many other folks on individual slides) Today Today filtering and frequency analysis of images. Slides from Derek Hoiem Sampling and Reconstruction Sampled representations • How to store and compute with continuous functions? • Common scheme for representation: samples [FvDFH fig.14.14b / Wolberg] – write down the function’s values at many points © 2006 Steve Marschner • 5 Reconstruction • Making samples back into a continuous function [FvDFH fig.14.14b / Wolberg] – for output (need realizable method) – for analysis or processing (need mathematical method) – amounts to “guessing” what the function did in between © 2006 Steve Marschner • 6 1D Example: Audio low high frequencies Sampling in digital audio • Recording: sound to analog to samples to disc • Playback: disc to samples to analog to sound again – how can we be sure we are filling in the gaps correctly? © 2006 Steve Marschner • 8 Sampling and Reconstruction • Simple example: a sign wave © 2006 Steve Marschner • 9 Undersampling • What if we “missed” things between the samples? • Simple example: undersampling a sine wave – unsurprising result: information is lost © 2006 Steve Marschner • 10 Undersampling • What if we “missed” things between the samples? • Simple example: undersampling a sine wave – unsurprising result: information is lost – surprising result: indistinguishable from lower frequency © 2006 Steve Marschner • 11 Undersampling • What if we “missed” things between the samples? • Simple example: undersampling a sine wave – – – – unsurprising result: information is lost surprising result: indistinguishable from lower frequency also, was always indistinguishable from higher frequencies aliasing: signals “traveling in disguise” as other frequencies © 2006 Steve Marschner • 12 Aliasing in video Slide by Steve Seitz Aliasing in images What’s happening? Input signal: Plot as image: x = 0:.05:5; imagesc(sin((2.^x).*x)) Alias! Not enough samples Antialiasing • What can we do about aliasing? • Sample more often – Join the Mega-Pixel craze of the photo industry – But this can’t go on forever • Make the signal less “wiggly” – Get rid of some high frequencies – Will loose information – But it’s better than aliasing Preventing aliasing • Introduce lowpass filters: – remove high frequencies leaving only safe, low frequencies – choose lowest frequency in reconstruction (disambiguate) © 2006 Steve Marschner • 17 Linear filtering: a key idea • Transformations on signals; e.g.: – bass/treble controls on stereo – blurring/sharpening operations in image editing – smoothing/noise reduction in tracking • Key properties – linearity: filter(f + g) = filter(f) + filter(g) – shift invariance: behavior invariant to shifting the input • delaying an audio signal • sliding an image around • Can be modeled mathematically by 18 convolution © 2006 Steve Marschner • Moving Average • basic idea: define a new function by averaging over a sliding window • a simple example to start off: smoothing © 2006 Steve Marschner • 19 Weighted Moving Average • Can add weights to our moving average • Weights […, 0, 1, 1, 1, 1, 1, 0, …] / 5 © 2006 Steve Marschner • 20 Weighted Moving Average • bell curve (gaussian-like) weights […, 1, 4, 6, 4, 1, …] © 2006 Steve Marschner • 21 Moving Average In 2D 0 0 are 0 the 0 0 0 0 0 0 What weights H? 0 0 0 0 0 0 0 0 0 0 0 0 0 0 90 90 90 90 90 0 0 0 0 0 90 90 90 90 90 0 0 0 0 0 90 90 90 90 90 0 0 0 0 0 90 0 90 90 90 0 0 0 0 0 90 90 90 90 90 0 0 0 0 0 0 0 0 0 0 0 0 0 0 90 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 © 2006 Steve Marschner • 22 Slide by Steve Seitz Cross-correlation filtering • Let’s write this down as an equation. Assume the averaging window is (2k+1)x(2k+1): • We can generalize this idea by allowing different weights for different neighboring pixels: • This is called a cross-correlation operation and written: • H is called the “filter,” “kernel,” or “mask.” © 2006 Steve Marschner • 23 Slide by Steve Seitz Gaussian filtering •A Gaussian kernel gives less weight to pixels further from the0 center of the window 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 90 90 90 90 90 0 0 0 0 0 90 90 90 90 90 0 0 0 0 0 90 90 90 90 90 0 0 0 0 0 90 0 90 90 90 0 0 0 0 0 90 90 90 90 90 0 0 0 0 0 0 0 0 0 0 0 0 0 0 90 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 1 2 4 2 1 2 1 •This kernel is an approximation of a Gaussian function: Slide by Steve Seitz Mean vs. Gaussian filtering Slide by Steve Seitz Convolution • cross-correlation: • A convolution operation is a cross-correlation where the filter is flipped both horizontally and vertically before being applied to the image: • It is written: • Suppose H is a Gaussian or mean kernel. How does convolution differ from cross-correlation? Slide by Steve Seitz Convolution is nice! • • Notation: Convolution is a multiplication-like operation – – – – – • • commutative associative distributes over addition scalars factor out identity: unit impulse e = […, 0, 0, 1, 0, 0, …] Conceptually no distinction between filter and signal Usefulness of associativity – often apply several filters one after another: (((a * b1) * b2) * b3) – this is equivalent to applying one filter: a * (b1 * b2 * b3) © 2006 Steve Marschner • 27 Tricks with convolutions 10 20 = 30 40 50 60 10 20 30 40 50 60 Slides from Alexei Eros and Steve Seitz Salvador Dali “Gala Contemplating the Mediterranean Sea, which at 30 meters becomes the portrait of Abraham Lincoln”, 1976 A nice set of basis Teases away fast vs. slow changes in the image. This change of basis has a special name… Jean Baptiste Joseph Fourier (1768-1830) ...the manner in which the author arrives at these equations is not exempt of difficulties and...his analysis to integrate them still leaves something to Any univariate function can be be desired on the score of generality and even rigour. rewritten as a weighted sum of • had crazy idea (1807): • sines and cosines of different frequencies. • Don’t believe it? – Neither did Lagrange, Laplace, Poisson and other big wigs – Not translated into English until 1878! • But it’s (mostly) true! – called Fourier Series – there are some subtle restrictions Laplace Legendre Lagrange A sum of sines •Our building block: • Asin( x •Add enough of them to get any signal f(x) you want! •How many degrees of freedom? •What does each control? •Which one encodes the coarse vs. fine structure of the signal? Fourier Transform •We want to understand the frequency of our signal. So, let’s reparametrize the signal by instead of x: f(x) F() Fourier Transform For every from 0 to inf, F() holds the amplitude A and phase of the corresponding sine • How can F hold both? Complex number trick! Asin( x F ( ) R( ) iI ( ) 2 2 1 I ( ) A R( ) I ( ) tan R( ) We can always go back: F() Inverse Fourier Transform f(x) Time and Frequency • example : g(t) = sin(2pf t) + (1/3)sin(2p(3f) t) Time and Frequency • example : g(t) = sin(2pf t) + (1/3)sin(2p(3f) t) = + Frequency Spectra • example : g(t) = sin(2pf t) + (1/3)sin(2p(3f) t) = + Frequency Spectra • Usually, frequency is more interesting than the phase Frequency Spectra = = + Frequency Spectra = = + Frequency Spectra = = + Frequency Spectra = = + Frequency Spectra = = + Frequency Spectra 1 = A sin(2 kt ) k 1 k Frequency Spectra FT: Just a change of basis M * f(x) = F() * . . . = IFT: Just a change of basis M-1 * F() = f(x) * . . . = Finally: Scary Math Finally: Scary Math • …not really scary: eix • is hiding our old friend: phase can be encoded by sin/cos pair cos(x ) i sin( x ) Asin( x P cos( x ) Q sin( x ) A sin( x Α P2 Q2 P Q tan 1 • So it’s just our signal f(x) times sine at frequency Extension to 2D in Matlab, check out: imagesc(log(abs(fftshift(fft2(im))))); Fourier analysis in images Intensity Image Fourier Image http://sharp.bu.edu/~slehar/fourier/fourier.html#filtering Signals can be composed + = http://sharp.bu.edu/~slehar/fourier/fourier.html#filtering More: http://www.cs.unm.edu/~brayer/vision/fourier.html Man-made Scene Can change spectrum, then reconstruct Low and High Pass filtering The Convolution Theorem •The greatest thing since sliced (banana) bread! – The Fourier transform of the convolution of two functions is the product of their Fourier transforms F[ g h] F[ g ] F[h] – The inverse Fourier transform of the product of two Fourier transforms is the convolution of the two inverse Fourier transforms 1 1 1 F [ gh] F [ g ] F [h] – Convolution in spatial domain is equivalent to multiplication in frequency domain! 2D convolution theorem example |F(sx,sy)| f(x,y) * h(x,y) |H(sx,sy)| g(x,y) |G(sx,sy)| Filtering Why does the Gaussian give a nice smooth image, but the square filter give edgy artifacts? Gaussian Box filter Gaussian Box Filter Fourier Transform pairs Low-pass, Band-pass, High-pass filters low-pass: High-pass / band-pass: Edges in images What does blurring take away? original What does blurring take away? smoothed (5x5 Gaussian) High-Pass filter smoothed – original Band-pass filtering Gaussian Pyramid (low-pass images) • Laplacian Pyramid (subband images) • Created from Gaussian pyramid by subtraction Laplacian Pyramid Need this! Original image • How can we reconstruct (collapse) this pyramid into the original image? Why Laplacian? Gaussian delta function Laplacian of Gaussian Project 2: Hybrid Images Gaussian Filter! • A. Oliva, A. Torralba, P.G. Schyns, “Hybrid Images,” SIGGRAPH 2006 Laplacian Filter! Project Instructions: unit impulse Gaussian Laplacian of Gaussian http://www.cs.illinois.edu/class/fa10/cs498dwh/projects/hybrid/ComputationalPhotography_ProjectHyb Clues from Human Perception • Early processing in humans filters for various orientations and scales of frequency • Perceptual cues in the mid frequencies dominate perception • When we see an image from far away, we are effectively subsampling it Early Visual Processing: Multi-scale edge and blob filters Frequency Domain and Perception Campbell-Robson contrast sensitivity curve Da Vinci and Peripheral Vision Leonardo playing with peripheral vision Unsharp Masking 100 200 - 300 = 400 500 200 400 +a 600 800 = Freq. Perception Depends on Color R G B Lossy Image Compression (JPEG) Block-based Discrete Cosine Transform (DCT) Using DCT in JPEG • The first coefficient B(0,0) is the DC component, the average intensity • The top-left coeffs represent low frequencies, the bottom right – high frequencies Image compression using DCT • Quantize – More coarsely for high frequencies (which also tend to have smaller values) – Many quantized high frequency values will be zero • Encode – Can decode with inverse dct Filter responses Quantization table Quantized values JPEG Compression Summary Subsample color by factor of 2 – People have bad resolution for color Split into blocks (8x8, typically), subtract 128 For each block a. Compute DCT coefficients for b. Coarsely quantize • Many high frequency components will become zero c. Encode (e.g., with Huffman coding) http://en.wikipedia.org/wiki/YCbCr http://en.wikipedia.org/wiki/JPEG Block size in JPEG • Block size – small block • faster • correlation exists between neighboring pixels – large block • better compression in smooth regions – It’s 8x8 in standard JPEG JPEG compression comparison 89k 12k Image gradient • The gradient of an image: • The gradient points in the direction of most rapid change in intensity The gradient direction is given by: • how does this relate to the direction of the edge? The edge strength is given by the gradient magnitude Effects of noise • Consider a single row or column of the image – Plotting intensity as a function of position gives a signal How to compute a derivative? Where is the edge? Solution: smooth first Where is the edge? Look for peaks in Derivative theorem of convolution • This saves us one operation: Laplacian of Gaussian • Consider Laplacian of Gaussian operator Where is the edge? Zero-crossings of bottom graph 2D edge detection filters Laplacian of Gaussian Gaussian derivative of Gaussian is the Laplacian operator: Try this in MATLAB • • • • • • • • • • • • g = fspecial('gaussian',15,2); imagesc(g); colormap(gray); surfl(g) gclown = conv2(clown,g,'same'); imagesc(conv2(clown,[-1 1],'same')); imagesc(conv2(gclown,[-1 1],'same')); dx = conv2(g,[-1 1],'same'); imagesc(conv2(clown,dx,'same')); lg = fspecial('log',15,2); lclown = conv2(clown,lg,'same'); imagesc(lclown) imagesc(clown + .2*lclown) Find your own image, clown doesn’t seem to be a default image anymore For next class Read over the slides and the textbook, Chapter 3.