Lecture 2 1 Image Filtering 2018
Lecture 2 1 Image Filtering 2018
Idar Dyrdal
Image function y f (x, y)
f : R2 ! R
x
0 0 0 0 0 0 0 0 0 0
0 0 0 255 255 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
Discrete (sampled and quantized) version of the (continuous) image function f (x, y)
3
Image Processing
• Point operators
f [i, j] ! g[i, j]
4
Point Operators
• Histogram equalization
• … g[i, j] = 2f [i, j]
(Each pixel multiplied by 2)
g[i, j] = h(f [i, j])
(Pixel-by-pixel transformation)
5
Pixel transforms - example
p
f [i, j] g[i, j] = f [i, j]
6
Histogram equalization
7
Image filtering
8
Image filtering in spatial domain
Modify the pixels in an image based on some function of a local neighborhood of each pixel:
f [i, j] g[i, j]
10 5 3
h
4 5 1 10
1 1 7
9
Linear filtering
Convolution or cross-correlation where each pixel in the filtered image is a linear combination of the
pixels in a local neighborhood in the original image:
10 5 3 0 0 0
4 6 1 0 1 0 16
1 1 8 0 2 1
Local image data Kernel Modified image data
The coefficients of the linear combination is contained in the “kernel” (filter mask).
10
Cross-correlation
Let f be the image, h be the kernel (of size 2k+1 x 2k+1), and g
be the output image:
k
X k
X
g[i, j] = h[u, v]f [i + u, j + v]
u= k v= k
g =h⌦f
11
Linear filtering
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 10 20 20 10 0 0 0 0
0
0
0
0
0
90
90
90
90
0
90
0
0
0
0
0
0
0
0
h 0 0 20
1 1 1
f 0
0
0
0
90
90
90
90
90
90
90
90
0
90
0
0
0
0
0
0
⌦ 1 1 1 = g
1 1 1
0 90 90 90 90 90 90 90 0 0
0 0 90 90 90 90 90 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
X
g[i, j] = h[u, v]f [i + u, j + v]
u,v
12
Linear filtering
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 10 20 20 10 0 0 0 0
0 0 0 90 90 0 0 0 0 0 0 0 20 40 50 30 10 0 0 0
0 0 0 90 90 90 0 0 0 0 0 10 40 70 80 50 20 0 0 0
1 1 1
⌦
0 0 90 90 90 90 0 0 0 0 0 20 50 80 90 70 40 10
0 0 90 90 90 90 90 0 0 0
1
1
1
1
1
1
=
0 90 90 90 90 90 90 90 0 0
0 0 90 90 90 90 90 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
13
Linear filtering
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 10 20 20 10 0 0 0 0
0 0 0 90 90 0 0 0 0 0 0 0 20 40 50 30 10 0 0 0
0 0 0 90 90 90 0 0 0 0 0 10 40 70 80 50 20 0 0 0
1 1 1
0 0 90 90 90 90 0 0 0 0 0 20 50 80 90 70 40 10 0 0
0 0 90 90 90 90 90 0 0 0
⌦ 1
1
1
1
1
1
= 0 40 70 90 90 80 60 30 10 0
0 90 90 90 90 90 90 90 0 0 10 40 70 90 90 90 70 40 10 0
0 0 90 90 90 90 90 0 0 0 0 30 50 60 60 60 50 30 10 0
0 0 0 0 0 0 0 0 0 0 0 10 20 30 30 30 20 10 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
14
Moving average filter (box filter)
3 x 3 kernel
1 1 1
1 1 1
1 1 1
16
Convolution
• Same as cross-correlation, except that the kernel is “flipped” (horizontally and vertically):
k
X k
X
g[i, j] = h[u, v]f [i u, j v]
u= k v= k
g =h⇤f
• Convolution is commutative and associative (no difference between filter and image):
a⇤b=b⇤a a ⇤ (b ⇤ c) = (a ⇤ b) ⇤ c
• Apply several filters, one after the other:
(((a ⇤ b1 ) ⇤ b2 ) ⇤ b3 ) = a ⇤ (b1 ⇤ b2 ⇤ b3 )
17
Gaussian filter (smoothing)
5 ⇥ 5, =1
1 x2 +y 2
( 2 2 )
G = 2
e
100 ⇥ 100, = 10 2⇡
18
Gaussian filtering
Discrete 3 x 3 approximation:
1 2 1 1
2 4 2 = 2 ⇥ 1 2 1
1 2 1 1
20
2D convolution
1 2 1 1 2 2
2 4 2 ⇤ 1 4 4 = 47
1 2 1 3 3 5
Filter kernel 3 x 3 image window Result (center pixel only)
21
1D convolution along rows and columns
1 2 2 7
1 2 1 ⇤ 1 4 4 = 13
3 3 5 14
1 7
2 ⇤ 13 = 47
1 14
22
Edge detection
23
Edges and image derivatives
Image gradient:
• An edge is a place of rapid change of the
image intensity function @f @f
rf = ,
• Corresponds to extrema of the first @x @y
derivative of the image intensity function
Gradient magnitude:
• Discrete approximation to the image s✓
derivatives: ◆2 ✓ ◆2
@f @f
||rf || = +
@x @y
@f
[i, j] ⇡ f [i + 1, j] f [i, j]
@x Prewitt operator:
-1 0 1 -1 -1 -1
@f Gx = -1 0 1 Gy = 0 0 0
[i, j] ⇡ f [i, j + 1] f [i, j] -1 0 1 1 1 1
@y
24
Image gradient
@f
f @x
@f
||rf ||
@y
25
Derivative of Gaussians @
h (u, v)
@u
@
h (u, v)
@v
1 u2 +v 2
( 2 2 )
h (u, v) = 2
e
2⇡
26
Sobel operator
-1 0 1 -1 -2 -1
-2 0 2 0 0 0
-1 0 1 1 2 1
x-direction y-direction
27
Gradient magnitude
Sobel operator - example
x-direction y-direction
28
Non-linear filtering - Median filter
A median filter operates over a neighborhood in the input image by selecting the median intensity:
10 5 3 Compute median
4 6 1 from 4
1 1 8 neighborhood
Local image data Modified image data
29
Median filtering - example
Image with Salt & Pepper noise Image after median filtering
30
Morphological operations
• Non-linear filtering
• Typically used to clean up binary images
• Erosion: replace pixel value with minimum
in local neighborhood
• Dilation: replace pixel value with maximum
in local neighborhood
• Structuring element used to define the local
neighborhood:
31
Morphological operations - Erosion
32
Morphological operations - Dilation
33
Opening = Erosion + Dilation
34
Closing = Dilation + Erosion
35
Filtering in frequency domain
Fourier (1807):
Any univariate function can be rewritten as
a weighted sum of sines and cosines of
different frequencies (true with some subtle
restrictions).
36
Sum of sines
+ + =
A sin(!x + ) Amplitude
Amplitude: Phase:
p 1 I(!)
A = ± R(!)2 + I(!)2 = tan
R(!)
f0 2f0 3f0 Frequency
37
Two-dimensional Fourier transform
Continous transform:
Z 1 Z 1
j(!x x+!y y)
H(!x , !y ) = h(x, y)e dxdy
1 1
Discrete transform:
M
X1 NX1
1 2⇡j km m+k nn
H(km , kn ) = h(m, n)e MN
MN m=0 n=0
38
Fourier analysis in images
Intensity images
Fourier images
39
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]
1
g⇤h=F [F [g]F [h]]
40
Example – Gaussian (low pass) filtering
41
Example – Gaussian filtering
FFT
F [g]
g
42
Example – Gaussian filtering
FFT
F [h]
h
43
Example – Gaussian filtering
44
Example – Gaussian filtering
Inverse
FFT
F [g]F [h] 1
g⇤h=F [F [g]F [h]]
45
Summary
Image Processing
• Point operators
46