Biomedical Image Analysis Contents: Spatial Filtering The mechanics of Spatial Filtering Smoothing and sharpening filters BMIA 15 V. Roth & P. Cattin 1
The Mechanics of Spatial Filtering Spatial filter: (i) neighborhood N, (ii) predefined operation w Example: N (3 3): g(x, y) = w( 1, 1)f(x 1, y 1) + + w(1, 1)f(x + 1, y + 1) BMIA 15 V. Roth & P. Cattin 2
Correlation and Convolution Correlation: w(x, y) f(x, y) = a b w(s, t)f(x + s, y + t) s= a t= b Correlating w with unit impulse w rotated by 180 deg Convolving w with unit impulse w. Convolve = pre-rotate filter and correlate: w(x, y) f(x, y) = a b w(s, t)f(x s, y t) s= a t= b BMIA 15 V. Roth & P. Cattin 3
Correlation and Convolution BMIA 15 V. Roth & P. Cattin 4
Correlation and Convolution (2) BMIA 15 V. Roth & P. Cattin 5
Vector Representation Let w = (w 1,..., w mn ) T be the coefficients of a m n filter. Its response R is R = w 1 z 1 + w 2 z 2 +..., +w mn z mn = w T z, where the zs are the corresponding image intensities. BMIA 15 V. Roth & P. Cattin 6
Smoothing Spatial Filters Box filter: averaging pixels over neighborhood. Generalization: weighted average. BMIA 15 V. Roth & P. Cattin 7
Smoothing Spatial Filters (2) BMIA 15 V. Roth & P. Cattin 8
Smoothing Spatial Filters (3) Typical application: blur image to get a gross representation of objects of interest BMIA 15 V. Roth & P. Cattin 9
Order-Statistic Filters Response based on ranking the pixels nonlinear. Median filter: response is median of of pixels in N. Salt-and-pepper noise good noise reduction with less blurring than linear filters. BMIA 15 V. Roth & P. Cattin 10
Median filter: example BMIA 15 V. Roth & P. Cattin 11
Sharpening Spatial Filters First order derivative of digital function: f x = f(x + 1) f(x). Second order: 2 f x 2 = f(x + 1) + f(x 1) 2f(x) Many ramp-like transitions in real images first order gives thick edges second order desirable for enhancing details. BMIA 15 V. Roth & P. Cattin 12
Sharpening Spatial Filters: Laplacian Simplest 2nd-order isotropic filter: Laplacian Digital functions: f = 2 f = 2 f x 2 + 2 f y 2. f = f(x + 1, y) + f(x 1, y) + f(x, y + 1) + f(x, y 1) 4f(x, y). Additional diagonal directions: ( f) = f + f(x 1, y 1) + f(x 1, y + 1) + f(x + 1, y 1) + f(x + 1, y + 1) 4f(x, y). Sharpening: weighted sum of original + filtered: g(x, y) = f(x, y) + c [ f(x, y)]. BMIA 15 V. Roth & P. Cattin 13
Laplacian filter masks BMIA 15 V. Roth & P. Cattin 14
Unsharp Masking and Highboost Filtering 1. Blur image f(x, y). 2. Subtract blurred from original mask g m = f(x, y) f(x, y). 3. Add (weighted portion of) mask to original g(x, y) = f(x, y) + k g m (x, y). BMIA 15 V. Roth & P. Cattin 15
Highboost: example BMIA 15 V. Roth & P. Cattin 16
Sharpening Spatial Filters: Gradient Approximate gradient magnitude by sum of absolute values: M(x, y) f/ x + f/ y = g x + g y. Sobel masks: g x = (z 7 + 2z 8 + z 9 ) (z 1 + 2z 2 + z 3 ), g y = (z 3 + 2z 6 + z 9 ) (z 1 + 2z 4 + z 7 ) y z z z z 1 2 3 4 5 z z6 x z 7 z 8 z 9 1 2 1 1 0 1 0 0 0 2 0 2 1 2 1 1 0 1 BMIA 15 V. Roth & P. Cattin 17
Sharpening Spatial Filters: Gradient (2) BMIA 15 V. Roth & P. Cattin 18
Combining Spatial Filters BMIA 15 V. Roth & P. Cattin 19
Combining Spatial Filters (2) BMIA 15 V. Roth & P. Cattin 20
Biomedical Image Analysis Filtering in the Fourier Domain Contents: Transform Domain Properties of the 2D DFT Practical filtering in the frequency domain BMIA 15 V. Roth & P. Cattin 21
Fourier s Idea BMIA 15 V. Roth & P. Cattin 22
Filtering in the Transform Domain So far: transformations operated directly on pixels Sometimes it is easier to transform images, filter in the transform domain, and apply inverse transform back to the spatial domain. Important class of 2D-linear transforms: transformed image = input image forward transformation kernel T (u, v) = f(x, y)r(x, y, u, v) x y BMIA 15 V. Roth & P. Cattin 23
Filtering in the Transform Domain (2) Inverse transform back into spatial domain uses inverse transformation kernel f(x, y) = T (u, v)s(x, y, u, v) u v Transform and its inverse build a transform pair. Kernel is separable if r(x, y, u, v) = r 1 (x, u)r 2 (y, v) and symmetric if r 1 = r 2. BMIA 15 V. Roth & P. Cattin 24
2D-Fourier transform Kernels: r(x, y, u, v) = exp( i2π(ux/m + vy/n)) s(x, y, u, v) = 1/(MN) exp(i2π(ux/m + vy/n)) Discrete Fourier transform pair: T (u, v) = M 1 x=0 f(x, y) = 1 MN N 1 y=0 M 1 u=0 f(x, y) exp N 1 v=0 T (u, v) exp ( i2π( ux M + vy N ) ) ( i2π( ux M + vy N ) ) BMIA 15 V. Roth & P. Cattin 25
Sampling 1. Continuous function f(t). 2. Impulse train. 3. Sampled function f(t) = f(t)s T (t) = n= f(t)δ(t n T ). 4. Sample values by integration: f k = f(t)δ(t k T ) dt = f(k T ). BMIA 15 V. Roth & P. Cattin 26
FT of sampled function: FT of sampled functions F (µ) = F{ f(t)} = F{f(t)s T (t)} = F (µ) S(µ) = F (µ) 1 T = = 1 T F (µ) S(µ τ) dτ + n= F (µ n T ). + n= δ(µ n T ) Infinite periodic sequence of copies of F (µ), separation 1/ T. BMIA 15 V. Roth & P. Cattin 27
FT of sampled functions (2) 1. FT of band-limited function: FT is zero outside [ µ max, µ max ]. 2. Oversampling: copies separated by C µ > 0. 3. Critical sampling: C µ = 0 4. Undersampling: copies overlap. BMIA 15 V. Roth & P. Cattin 28
FT of sampled functions (3) F (µ) is continuous periodic function with period 1/ T. Entire transform characterized by one complete period! We can recover f(t) from single period by inverse FT! Sampling theorem: 1/ T > 2µ max. BMIA 15 V. Roth & P. Cattin 29
FT of sampled functions (4) BMIA 15 V. Roth & P. Cattin 30
Aliasing BMIA 15 V. Roth & P. Cattin 31
Aliasing (2) BMIA 15 V. Roth & P. Cattin 32
Properties of the 2D DFT Translation: f(x, y)e i2π(u 0x/M+v 0 y/n) F (u u 0, v v 0 ), f(x x 0, y y 0 ) F (u, v)e i2π(x 0u/M+y 0 v/n). Rotation: x = r cos θ, y = r sin θ, u = ω cos φ, v = ω sin φ: f(r, θ + θ 0 ) F (ω, φ + θ 0 ). BMIA 15 V. Roth & P. Cattin 33
2D DFT Example BMIA 15 V. Roth & P. Cattin 34
Centering the DFT Centering in 2D: f(x, y)( 1) x+y F (u M/2, v N/2). BMIA 15 V. Roth & P. Cattin 35
Padding: 1D example BMIA 15 V. Roth & P. Cattin 36
Padding: 2D example BMIA 15 V. Roth & P. Cattin 37
Padding of a frequency domain filter Our goal: define filters directly in the frequency domain. What about padding?? One strategy: Given: image of size M N. Construct filter of size M N. Compute IFT of filter spatial filter mask. Pad spatial filter. Use FT to return to frequency domain. BMIA 15 V. Roth & P. Cattin 38
Padding of a frequency domain filter (2) FT of ideal low pass (box): sinc, frequency components extending to! Ringing artifacts. BMIA 15 V. Roth & P. Cattin 39
Practical filtering in the frequency domain Given: image of size M N. Select P = 2M, Q = 2M. Pad image to size P Q (appending zeros). Multiply by ( 1) x+y to center the FT. Compute DFT F (u, v). Construct (real,symmetric) filter H(u, v) of size P Q. Multiply G(u, v) = F (u, v) H(u, v) g p (x, y) = real { F 1 [G(u, v)] } ( 1) x+y. Crop to top left quadrant. BMIA 15 V. Roth & P. Cattin 40
Filtering in the frequency domain (2) BMIA 15 V. Roth & P. Cattin 41
Spatial- vs Frequency Domain Filtering BMIA 15 V. Roth & P. Cattin 42
Spatial- vs Frequency Domain Filtering BMIA 15 V. Roth & P. Cattin 43
Smoothing in the frequency domain BMIA 15 V. Roth & P. Cattin 44
Smoothing: ideal lowpass BMIA 15 V. Roth & P. Cattin 45
Smoothing: Butterworth lowpass 1 H(u, v) = 1 + [D(u, v)/d 0 ] 2n, D(u, v)2 = u 2 + v 2, (assuming centered filter) BMIA 15 V. Roth & P. Cattin 46
Smoothing: Butterworth lowpass (2) BMIA 15 V. Roth & P. Cattin 47
Smoothing: Gaussian lowpass H(u, v) = exp( 1 2 D(u, v)2 /D 2 0) BMIA 15 V. Roth & P. Cattin 48
Smoothing in the frequency domain (4) BMIA 15 V. Roth & P. Cattin 49
Sharpening in the frequency domain BMIA 15 V. Roth & P. Cattin 50
Sharpening in the frequency domain (2) BMIA 15 V. Roth & P. Cattin 51
Sharpening in the frequency domain (3) BMIA 15 V. Roth & P. Cattin 52
Sharpening in the frequency domain (4) BMIA 15 V. Roth & P. Cattin 53
Sharpening in the frequency domain (5) BMIA 15 V. Roth & P. Cattin 54
Sharpening in the frequency domain (6) BMIA 15 V. Roth & P. Cattin 55