Fourier analysis of images Intensity Image Fourier Image Slides: James Hays, Hoiem, Efros, and others 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
Fourier Transform Fourier transform stores the magnitude and phase at each frequency Magnitude encodes how much signal there is at a particular frequency Phase encodes spatial information (indirectly) For mathematical convenience, this is often notated in terms of real and complex numbers Amplitude: A = ± R ω + I 2 2 ( ) ( ω) Phase: φ = tan I( ω) R( ω)
Filtering in the Frequency Domain Ideal LPF: D(u,v) is the distance from point (u,v) to the center of the filter. D is the cutoff frequency. Gaussian LPF:. g(x).8.6.4.2. σ -6-4 -2 2 4 x 2 2 ( x + y ) g( x, y ) = exp( 2 ) 2σ = g( x ) g( y ) 2 x g( x ) = exp( 2 ) 2σ G(u,v) = H(u,v) F(u,v) element-wise multiplication in frequency domain, then perform inverse DFT on G(u,v)...
Ideal LPF: Gaussian LPF:
High-pass Filters in the Frequency Domain Ideal HPF: Gaussian HPF: G(u,v) = H(u,v) F(u,v) element-wise multiplication in frequency domain, then perform inverse DFT on G(u,v)...
Ideal HPF: Gaussian HPF:
Filtering with the FFT in Matlab im = double(imread('baboon.bmp'))/255; im = rgb2gray(im); % im should be a gray-scale floating point image figure; imshow(im); [imh, imw] = size(im); hs = 5; % filter half-size fil = fspecial('gaussian', hs*2+, ); figure; bar3(fil); fftsize = 24; % should be order of 2 (for speed) and include padding im_fft = fft2(im, fftsize, fftsize); % ) fft im with padding figure(); imagesc(log(abs(fftshift(im_fft)))), axis image, colormap jet fil_fft = fft2(fil, fftsize, fftsize); image % 2) fft fil, pad to same size as figure(); imagesc(log(abs(fftshift(fil_fft)))), axis image, colormap jet im_fil_fft = im_fft.* fil_fft; % 3) multiply fft images im_fil = ifft2(im_fil_fft); % 4) inverse fft2 im_fil = im_fil(+hs:size(im,)+hs, +hs:size(im, 2)+hs); % 5) remove padding figure; imshow(im_fil);
Filtering Why does the Gaussian give a nice smooth image, but the square filter give edgy artifacts? Gaussian Box filter
Gaussian
Box Filter
Sampling Why does a lower resolution image still make sense to us? What do we lose? Image: http://www.flickr.com/photos/igorms/3696757/
Subsampling by a factor of 2 Throw away every other row and column to create a /2 size image
Aliasing problem D example (sinewave): Source: S. Marschner
Aliasing problem D example (sinewave): Source: S. Marschner
Aliasing problem Sub-sampling may be dangerous. Characteristic errors may appear: Wagon wheels rolling the wrong way in movies Checkerboards disintegrate in ray tracing Striped shirts look funny on color television Source: D. Forsyth
Aliasing in video Slide by Steve Seitz
Aliasing in graphics Source: A. Efros
Sampling and aliasing
Nyquist-Shannon Sampling Theorem When sampling a signal at discrete intervals, the sampling frequency must be 2 f max f max = max frequency of the input signal This allows us to reconstruct the original perfectly from the sampled version v v v good bad
Anti-aliasing Solutions: Sample more often Get rid of all frequencies that are greater than half the new sampling frequency Will lose information But it s better than aliasing Apply a smoothing filter
Algorithm for downsampling by factor of 2. Start with image(h, w) 2. Apply low-pass filter im_blur = imfilter(image, fspecial( gaussian, 7, )) 3. Sample every other pixel im_small = im_blur(:2:end, :2:end);
Anti-aliasing Forsyth and Ponce 22
Subsampling without pre-filtering /2 /4 (2x zoom) /8 (4x zoom) Slide by Steve Seitz
Subsampling with Gaussian pre-filtering Gaussian /2 G /4 G /8 Slide by Steve Seitz
Why do we get different, distance-dependent interpretations of hybrid images??
Salvador Dali invented Hybrid Images? Salvador Dali Gala Contemplating the Mediterranean Sea, which at 3 meters becomes the portrait of Abraham Lincoln, 976
Clues from Human Perception Early processing in humans filters for various orientations and scales of frequency Perceptual cues in the mid-high 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
Campbell-Robson contrast sensitivity curve
Hybrid Image in FFT Hybrid Image Low-passed Image High-passed Image
Perception Why do we get different, distance-dependent interpretations of hybrid images??
Things to Remember Sometimes it makes sense to think of images and filtering in the frequency domain Fourier analysis Can be faster to filter using FFT for large images (N logn vs. N 2 for autocorrelation) Images are mostly smooth Basis for compression Remember to low-pass before sampling
Practice question. Match the spatial domain image to the Fourier magnitude image 2 3 4 5 B A C D E
Slide credit Fei Fei Li
three views of filtering Image filtered in spatial domain Filter is a mathematical operation of a grid of numbers Smoothing, sharpening, measuring texture Image filtered in the frequency domain Filtering is a way to modify the frequencies of images Denoising, sampling, image compression Templates and Image Pyramids Filtering is a way to match a template to the image Detection, coarse-to-fine registration
Image filtering Image filtering: compute function of local neighborhood at each position Really important! Enhance images Denoise, resize, increase contrast, etc. Extract information from images Texture, edges, distinctive points, etc. Detect patterns Template matching
Example: box filter g[, ] Slide credit: David Lowe (UBC)
Image filtering g[, ] f [.,.] h[.,.] 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 h[ m, n] = k, l g[ k, l] f [ m + k, n + l] Credit: S. Seitz
Image filtering g[, ] f [.,.] h[.,.] 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 h[ m, n] = g[ k, l] k, l f [ m + k, n + l] Credit: S. Seitz
Image filtering g[, ] f [.,.] h[.,.] 2 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 h[ m, n] = g[ k, l] k, l f [ m + k, n + l] Credit: S. Seitz
Image filtering g[, ] f [.,.] h[.,.] 2 3 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 h[ m, n] = g[ k, l] k, l f [ m + k, n + l] Credit: S. Seitz
Image filtering g[, ] f [.,.] h[.,.] 2 3 3 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 h[ m, n] = g[ k, l] k, l f [ m + k, n + l] Credit: S. Seitz
Image filtering g[, ] f [.,.] h[.,.] 2 3 3 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9? 9 h[ m, n] = g[ k, l] k, l f [ m + k, n + l] Credit: S. Seitz
Image filtering g[, ] f [.,.] h[.,.] 2 3 3 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9? 9 9 9 9 9 5 9 h[ m, n] = g[ k, l] k, l f [ m + k, n + l] Credit: S. Seitz
Image filtering g[, ] f [.,.] h[.,.] 2 3 3 3 2 9 9 9 9 9 2 4 6 6 6 4 2 9 9 9 9 9 3 6 9 9 9 6 3 9 9 9 9 9 3 5 8 8 9 6 3 9 9 9 9 3 5 8 8 9 6 3 9 9 9 9 9 2 3 5 5 6 4 2 2 3 3 3 3 2 9 h[ m, n] = g[ k, l] k, l f [ m + k, n + l] Credit: S. Seitz
Box Filter What does it do? g[, ] Replaces each pixel with an average of its neighborhood Achieve smoothing effect (remove sharp features) Slide credit: David Lowe (UBC)
Smoothing with box filter
Practice with linear filters? Original Source: D. Lowe
Practice with linear filters Original Filtered (no change) Source: D. Lowe
Practice with linear filters? Original Source: D. Lowe
Practice with linear filters Original Shifted left By pixel Source: D. Lowe
Practice with linear filters 2 -? Original (Note that filter sums to ) Source: D. Lowe
Practice with linear filters 2 - Original Sharpening filter - Accentuates differences with local average Source: D. Lowe
Sharpening Source: D. Lowe
Other filters 2 Sobel - -2 - Vertical Edge (absolute value)
Other filters - 2-2 Sobel - Horizontal Edge (absolute value)
The Convolution Theorem The Fourier transform of the convolution of two functions is the product of their Fourier transforms F[ g h] = F[ g]f[ h] Convolution in spatial domain is equivalent to multiplication in frequency domain! g * h = F [F[ g]f[ h]]
Filtering vs. Convolution g=filter f=image 2d filtering h=filter2(g,f); or h=imfilter(f,g); h[ m, n] = g[ k, l] k, l f [ m + k, n + l] 2d convolution h=conv2(g,f); h[ m, n] = k, l g[ k, l] f [ m k, n l]
Key properties of linear filters Linearity: filter(f + f 2 ) = filter(f ) + filter(f 2 ) Shift invariance: same behavior regardless of pixel location filter(shift(f)) = shift(filter(f)) Any linear, shift-invariant operator can be represented as a convolution Source: S. Lazebnik
More properties Commutative: a * b = b * a Conceptually no difference between filter and signal But particular filtering implementations might break this equality Associative: a * (b * c) = (a * b) * c Often apply several filters one after another: (((a * b ) * b 2 ) * b 3 ) This is equivalent to applying one filter: a * (b * b 2 * b 3 ) Distributes over addition: a * (b + c) = (a * b) + (a * c) Scalars factor out: ka * b = a * kb = k (a * b) Identity: unit impulse e = [,,,, ], a * e = a Source: S. Lazebnik
Important filter: Gaussian Weight contributions of neighboring pixels by nearness.3.3.22.3.3.3.59.97.59.3.22.97.59.97.22.3.59.97.59.3.3.3.22.3.3 5 x 5, σ = Slide credit: Christopher Rasmussen
Smoothing with Gaussian filter
Smoothing with box filter
Gaussian filters Remove high-frequency components from the image (low-pass filter) Images become more smooth Convolution with self is another Gaussian So can smooth with small-width kernel, repeat, and get same result as larger-width kernel would have Convolving two times with Gaussian kernel of width σ is same as convolving once with kernel of width σ 2 Separable kernel Factors into product of two D Gaussians Source: K. Grauman
Separability of the Gaussian filter Source: D. Lowe
Separability example 2D convolution (center location only) The filter factors into a product of D filters: Perform convolution along rows: * = Followed by convolution along the remaining column: * = Source: K. Grauman
Separability Why is separability useful in practice?
Practical matters How big should the filter be? Values at edges should be near zero Rule of thumb for Gaussian: set filter half-width to about 3 σ
Practical matters What about near the edge? the filter window falls off the edge of the image need to extrapolate methods: clip filter (black) wrap around copy edge reflect across edge Source: S. Marschner
Practical matters Q? methods (MATLAB): clip filter (black): imfilter(f, g, ) wrap around: imfilter(f, g, circular ) copy edge: imfilter(f, g, replicate ) reflect across edge: imfilter(f, g, symmetric ) Source: S. Marschner
g Practical matters What is the size of the output? MATLAB: filter2(g, f, shape) shape = full : output size is sum of sizes of f and g shape = same : output size is same as f shape = valid : output size is difference of sizes of f and g full same valid g g g g g f f f g g g g g g Source: S. Lazebnik
Median filters A Median Filter operates over a window by selecting the median intensity in the window. What advantage does a median filter have over a mean filter? Is a median filter a kind of convolution? 26 Steve Marschner 78 Slide by Steve Seitz
Comparison: salt and pepper noise 26 Steve Marschner 79 Slide by Steve Seitz
Hybrid Images Gaussian Filter! A. Oliva, A. Torralba, P.G. Schyns, Hybrid Images, SIGGRAPH 26 Laplacian Filter! unit impulse Gaussian Laplacian of Gaussian
Remember Linear filtering is sum of products at each position Can smooth, sharpen, translate (among many other uses) Be aware of details for filter size, extrapolation, cropping
Review: questions. Write down a 3x3 filter that returns a positive value if the average value of the 4-adjacent neighbors is less than the center and a negative value otherwise 2. Write down a filter that will compute the gradient in the x-direction: gradx(y,x) = im(y,x+)-im(y,x) for each x, y Slide: Hoiem
Review: questions 3. Fill in the blanks: a) _ = D * B b) A = _ * _ c) F = D * _ d) _ = D * D Filtering Operator A E B F G C H I D Slide: Hoiem