Image Filtering: Associate Professor Faculty of Computer Science Institute of Business Administration - Karachi
Image Filtering: Associate Professor Faculty of Computer Science Institute of Business Administration - Karachi
Imran N. Junejo
Associate Professor
Faculty of Computer Science
Institute of Business Administration - Karachi
1
Lecture outline
• point-operations
• Learn about image filtering
2
Point Operations
• Point transformation: changes a pixel’s value without changing its
location.
5
Arithmetic Operations
Overflow issue?
6
Arithmetic Operations
What if b is negative?
7
Analytic Transformations
• Graylevel transformations can be specified using analytic functions such
as the logarithm, exponential, or power functions:
8
Logarithmic Transformations can be used to brighten the intensities of an
image (like the Gamma Transformation, where gamma < 1). More often, it is
used to increase the detail (or contrast) of lower intensity values.
9
Point Operations
• Point transformation: changes a pixel’s value without changing its
location.
• Can be applied by: Thresholding
Intensity Transformations
Point Operations
• Point transformation: changes a pixel’s value without changing its
location.
• Can be applied by: Histogram Modifications
What is a Filter
• Point operations are limited (why)
• They cannot accomplish tasks like sharpening
or smoothing
13
Noise Filtering or Image Filtering
• Given a corrupted image I by noise n,
attenuate, if possible eliminate n, without
altering I significantly.
14
Image filtering
• Filtering refers to linear transforms that change the
frequency contents of signals.
• Transformations where each output pixel is a
function of the input pixel and its neighbors.
• Really important!
– Enhance images
• Denoise, resize, increase contrast, etc.
– Extract information from images
• Texture, edges, distinctive points, etc.
– Detect patterns
• Template matching
15
Image filtering
• Filtering refers to linear transforms that change the
frequency contents of signals (def: signal processing).
• Transformations where each output pixel is a
function of the input pixel and its neighbors.
• Really important!
– Enhance images
• Denoise, resize, increase contrast, etc.
– Extract information from images
• Texture, edges, distinctive points, etc.
– Detect patterns
• Template matching
16
Smoothing an image by
averaging
• Replace each pixel by the average of its neighboring pixels
• Assume a 3x3 neighborhood:
P0 P1 P2
P3 P4 P5
P6 P7 P8
17
P0 P1 P2
P3 P4 P5
P6 P7 P8
18
Averaging filter
19
G
1 3 0 1 0 -1
2 10 2 1 0.1 -1 = 5
4 1 1 1 0 -1
Image filter Filter Output
21
Image filtering 1 1 1
g[ , ] 1 1 1
1 1 1
f [.,.] h[.,.]
0 0 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
0 0
0 90
90 0
0 90
90 90
90 90
90 0
0 0
0
0
0 0
0 0
0 90
90 90
90 90
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
0
0
0 0
0 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
0 0
0 0
0 0
0 0
0 0
0
h[ m, n] g[ k , l ] f [ m k , n l ]
22
k ,l Credit: S. Seitz
Image filtering 1 1 1
g[ , ] 1 1 1
1 1 1
f [.,.] h[.,.]
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 10
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
h[ m, n] g[ k , l ] f [ m k , n l ]
23
k ,l Credit: S. Seitz
Image filtering 1 1 1
g[ , ] 1 1 1
1 1 1
f [.,.] h[.,.]
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 10 20
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
h[ m, n] g[ k , l ] f [ m k , n l ]
24
k ,l Credit: S. Seitz
Image filtering 1 1 1
g[ , ] 1 1 1
1 1 1
f [.,.] h[.,.]
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 10 20 30
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
h[ m, n] g[ k , l ] f [ m k , n l ]
25
k ,l Credit: S. Seitz
Image filtering 1 1 1
g[ , ] 1 1 1
1 1 1
f [.,.] h[.,.]
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 10 20 30 30
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
h[ m, n] g[ k , l ] f [ m k , n l ]
26
k ,l Credit: S. Seitz
Image filtering 1 1 1
g[ , ] 1 1 1
1 1 1
f [.,.] h[.,.]
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 10 20 30 30
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
h[ m, n] g[ k , l ] f [ m k , n l ]
27
k ,l Credit: S. Seitz
Image filtering 1 1 1
g[ , ] 1 1 1
1 1 1
f [.,.] h[.,.]
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 10 20 30 30
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 50
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
h[ m, n] g[ k , l ] f [ m k , n l ]
28
k ,l Credit: S. Seitz
Image filtering g[ , ] 1 1 1
1 1 1
1 1 1
f [.,.] h[.,.]
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 10 20 30 30 30 20 10
0 0 0 90 90 90 90 90 0 0 0 20 40 60 60 60 40 20
0 0 0 90 90 90 90 90 0 0 0 30 60 90 90 90 60 30
0 0 0 90 90 90 90 90 0 0 0 30 50 80 80 90 60 30
0 0 0 90 0 90 90 90 0 0 0 30 50 80 80 90 60 30
0 0 0 90 90 90 90 90 0 0 0 20 30 50 50 60 40 20
0 0 0 0 0 0 0 0 0 0 10 20 30 30 30 30 20 10
0 0 90 0 0 0 0 0 0 0 10 10 10 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
h[ m, n] g[ k , l ] f [ m k , n l ]
29
k ,l Credit: S. Seitz
g
f'
30
Box Filter
g[ , ]
What does it do?
• Replaces each pixel with 1 1 1
an average of its
neighborhood 1 1 1
1 1 1
• Achieve smoothing effect
(remove sharp features)
31
Slide credit: David Lowe (UBC)
Smoothing with box filter
32
Practice with linear filters
?
0 0 0
0 1 0
0 0 0
Original
33
Source: D. Lowe
Practice with linear filters
0 0 0
0 1 0
0 0 0
Original Filtered
(no change)
34
Source: D. Lowe
Practice with linear filters
?
0 0 0
0 0 1
0 0 0
Original
35
Source: D. Lowe
Practice with linear filters
0 0 0
0 0 1
0 0 0
36
Source: D. Lowe
Practice with linear filters
-
0 0 0 1 1 1
0
0
2
0
0
0
1
1
1
1
1
1
?
(Note that filter sums to 1)
Original
37
Source: D. Lowe
Practice with linear filters
-
0 0 0 1 1 1
0 2 0 1 1 1
0 0 0 1 1 1
38
Source: D. Lowe
Sharpening
39
Source: D. Lowe
Other filters
1 0 -1
2 0 -2
1 0 -1
Sobel
Vertical Edge
(absolute value) 40
Other filters
1 2 1
0 0 0
-1 -2 -1
Sobel
Horizontal Edge
(absolute value) 41
Convolution
[filtering in frequency domain]
f i, j I * H I k , l H i k , j l
k l
H
I Image
X flip
H7 H8 H9 H1 H2 H3
H Kernel H4 H5 H6 H4 H5 H6
H1 H2 H3 H7 H8 H9
I Y flip
I1 I2 I3 H9 H8 H7 I * H I1 H 9 I 2 H 8 I 3 H 7
I4 I5 I6 H6 H5 H4 I 4 H 6 I5 H5 I6 H 4
I7 I8 I9 H3 H2 H1 I 7 H 3 I 8 H 2 I 9 H1
42
Gaussian Filter
Special type of smoothing kernel
G x, y
1 x2 y2
exp
2 2
2
2
H i, j
1
exp
i k 12 j k 12
2 2
2
2
43
1-D Gaussian Filter
x2
g ( x) e 2 2
44
Smoothing with Gaussian filter
45
Smoothing with box filter
46
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 1D Gaussians
47
Source: K. Grauman
Separability example
2D convolution
(center location only)
Perform convolution =
along rows: *
Followed by convolution =
along the remaining column: *
48
Source: K. Grauman
Properties of Gaussian
• Most common natural model
• Smooth function, it has infinite number of
derivatives
• Fourier Transform of Gaussian is Gaussian.
• Convolution of a Gaussian with itself is a
Gaussian.
• Gaussian kernels is separable.
49
Linear filtering and convolution
Original Noisy
Filtered
Filtered
σ=3.0
σ=1.5
50
Gaussian filters
51
Types of Linear Filters
52
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
53
Source: S. Marschner
Examples
• I2 = cv2.blur(I,(5,5))
• I2 = cv2.blur(I,(15,15))
• I2 = cv2.blur(I,(50,50))
cv2.GaussianBlur(I, (3,3),1.5)
• Computational expensive
– Standard – O(r2log r)
– Huang – O(r)
– Perreault (2007) – O(1)
medianBlur(image, smoothed_image, 5);
58
Suggested Reading
59