Ch3 Filters v3 Part1
Ch3 Filters v3 Part1
Computer Vision
Course
School of Information and
Communication Technology
1
2/20/24
Computer Vision
Chapter 3. Image Processing
(Part 1)
Computer Vision Group
School of Information and Communications Technology
Contents
2
2/20/24
Digital images ?
Digital images ?
x
• For an image I
• Index (0,0): Top left corner
• I(x,y): intensity of pixel at
the position (x,y)
y
3
2/20/24
Digital images ?
• Gray image:
- I(x,y) Î [0..255]
- 1 pixel: 8 bits (1 byte)
• Color image
- IR(x,y), IG(x,y) IB(x,y) Î [0..255]
- 1 pixel: 24 bits (3 bytes )
4
2/20/24
231
148
• “color”
• RGB: [R, G, B]
• Lab: [L, a, b] [249,
• HSV: [H, S, V] 215,
203]
10
10
5
2/20/24
11
11
Pixel transformation
12
12
6
2/20/24
Contents
Point processing
14
14
7
2/20/24
Homogenous transformation
s=T(r)
light
The output is
T(r) identical as the
input : s = r
m
r
Dark
m
Dark Light
15
15
s=T(r) s=T(r)
light
light
T(r) T(r)
n n
r r
Dark
Dark
m m
Dark Light Dark Light
16
16
8
2/20/24
Contrast stretching
s=T(r)
Light
r
Dark
m
Dark Light
17
17
Thresholding function
s=T(r)
1
T(r)
Thresholding function T(r)
outputs the image as binary
image:
• s = 0 if r <m
• s = 1 if r >= m
0 r
m
18
18
9
2/20/24
• Image Negatives
• Log Transformations
• Power-Law Transformations
• Piecewise-Linear Transformation Functions
• Histogram Processing
19
19
Image Negatives
• The negative of an image with gray levels in the range [0, L-
1] is obtained by s = L - 1 – r
L-1
3L/4
Ouput gray level, s
L/2 Negative
L/4
20
20
10
2/20/24
Log Transformations
L/4 Inverse
log
21
21
Log Transformations
• This transformation maps
• a narrow range of low gray-level values in the input image
into a wider range of output levels.
• The opposite is true of higher values of input levels.
22
22
11
2/20/24
23
23
Log Transformations
• Expand the values of dark pixels in an image while
compressing the higher-level values
24
24
12
2/20/24
25
25
• γ1 = 1; γ1 = 3; γ2 = 4; γ3 = 5;
26
26
13
2/20/24
Power-Law Transformations
• Given input image I, intensities are falled into [0, 255]. Use s =
r0.3 to find output image
24 25 45 18 90 45 54 3 3 3 2 4 3 3
16 2 25 214 97 54 54 2 1 3 5 4 3 3
18 154 14 201 98 65 54 2 5 2 5 4 3 3
19 254 13 201 50 31 24 2 5 2 5 3 3 3
200 210 254 231 47 201 8 5 5 5 5 3 5 2
21 218 217 120 102 156 58 2 5 5 4 4 5 3
0 236 208 10 12 95 4 0 5 5 2 2 4 2
27
27
Gama correction
28
28
14
2/20/24
Gama correction
29
29
Gama correction
Image: Wikipedia
30
30
15
2/20/24
Piecewise-Linear Transformation
• Contrast stretching:
L-1
• the idea behind contrast
stretching is to increase the
dynamic range of the gray levels (r2, s2)
in the image being processed 3L/4
• Output: T(r)
• Output values of input ones from (0, 0) L/2
to (r1, s1) decrease
• Output values of input ones from
(r1, s1) đến (r2, s2) increase.
L/4
(r1, s1)
31
31
Piecewise-Linear Transformation
L-
L-1
• If r1 = s1, r2 = s2, 1
3L/4/
• the function becomes 4
Homogenous transformation
L/
L/2
2 T(r)
(r2, s2)
L/ s2) (r1, s1)
L/4
• If s1 = 0, s2 = L-1, 4 s1 )
Cấp xám
• the function become thresholding 0 L/4L/ đầu vào
L/
L/2 3L L-
3L/4 L-1
4 2 /4 1
function
L-1
(r2, s2)
• Output is a binary image
3L/4
T(r)
L/2
L/4
(r1, s1)
0 L/4 L/2 3L/4 L-1
32
32
16
2/20/24
Piecewise-Linear Transformation
(r2, s2)
• If (r1, s1) = (rmin, 0) ; 255
(r1, s1)
0
• If (r1, s1) = (rsmin, 0), rsmin > rmin 0 rmin rmax
T(r)
0 (r1, s1)
0 rmin rsmin rsmax rmax
33
33
Piecewise-Linear Transformation
34
34
17
2/20/24
Image histogram
• What is histogram?
• Histogram of an image with gray level range [0, L-1] is a
discrete function: ℎ 𝑟! = 𝑛!
• 𝑟! is kth gray level
• 𝑛! : number of pixels that have level 𝑟!
• Histogram is a graphical representation of the repartition
of colours among the pixels of a digital image
35
35
Image histogram
• Normalized histogram?
• The histogram should be normalized by dividing all elements to total
$"
number of pixels in the image (n) : ℎ 𝑟! = $
36
36
18
2/20/24
Image histogram
• Properties?
• Only statistic information
• No indication about the location of pixel (no spatial information)
• Different images can have the same histogram
37
37
Image Brightness
38
38
19
2/20/24
Contrast
39
39
Contrast
• Contrast vs histogram
40
40
20
2/20/24
Dark image
Light image
41
41
Low- contrast
image
High- contrast
image
42
42
21
2/20/24
Histogram
43
43
Histogram equalization
• Input:
• Image I which have L gray levels rk Î [0, L-1], k=0, 1, ..., L-1. Total pixels
is n.
• Assume that the input image is low contrast
• Output:
• Image J with gray level sk Î [0, L-1], k=0, 1, ..., L-1
• Histogram of modified image: uniform distribution
• Each gray level in the image occurs with the same frequency
44
44
22
2/20/24
Histogram equalization
45
45
Histogram equalization
• Matching function:
• If T is the cumulative distributive function of r multiplied by (L − 1),
then s is uniformly distributed on [0, L − 1]
46
46
23
2/20/24
Histogram equalization
• Discrete domain (apply to image)
49
49
Histogram equalization
• Step-by-step
• s1. count the number of pixels which have gray level k
in image, let’s say nk (k=0, 1, ..., L-1)
𝑛!
• s2. compute p(k) like follows 𝑝(𝑘) =
𝑛
(normalized histogram): k = 0, 1, 2, ..., L-1.
50
50
24
2/20/24
Histogram equalization
• s3. Compute T(k) (cumulative histogram)
! !
1
𝑇(𝑘) = * 𝑝(𝑗) = * 𝑛* k = 0, 1, 2, ..., L-1.
𝑛
*$+ *$+
51
51
Histogram equalization
2 3 3 2 6
4 2 4 3 5 Histogram
3 2 3 5 4
2 4 2 4 3
2
k 0 1 2 3 4 5 6 7 8 9
1
nk 0 0 6 5 4 1 0 0 0 0 Gray level
0 1 2 3 4 5 6 7 8 9
52
52
25
2/20/24
Histogram equalization
k 0 1 2 3 4 5 6 7 8 9
nk 0 0 6 5 4 1 0 0 0 0
!
* 𝑛* 0 0 6 11 15 16 16 16 16 16
*$+
53
53
Histogram equalization
k sk 2 3 3 2 # pixcels
0 0 6
4 2 4 3
1 0 5
3 2 3 5
2 3 4
2 4 2 4
3 6
3
4 8
2
5 9 3 6 6 3
6 9 1 Gray
8 3 8 6 level
7 9
8 9 6 3 6 9 0 1 2 3 4 5 6 7 8 9
9 9 3 8 3 8 Histogram after equalization
54
54
26
2/20/24
Histogram equalization
55
55
Histogram equalization
56
56
27
2/20/24
Histogram equalization
• Discussion
• Compare between histogram equalization and Piecewise-
Linear Transformation Functions
• Is histogram equalization always good?
57
57
Histogram equalization
58
58
28
2/20/24
Linear stretching
¡ Linear transform
59
59
Linear stretching
No differences?
60
60
29
2/20/24
Histogram equalization
61
61
Histogram equalization
62
62
30
2/20/24
63
63
Histogram matching/specification
64
64
31
2/20/24
Histogram matching
Desired histogram
è Additional lecture
65
65
73
73
32
2/20/24
Histogram statistic
74
74
Histogram statistic
75
75
33
2/20/24
Histogram statistic
• Problem:
• how to enhance the contrast of one part in the image but
not affect other parts
• Idea:
• only increase or decrease desired parts while remaining
the others
76
76
Histogram statistic
77
77
34
2/20/24
Histogram statistic
78
78
79
79
35
2/20/24
80
80
Contents
36
2/20/24
Image filtering
• Image filtering:
• Image filters in spatial domain (called spatial filtering)
– Filter is a mathematical operation of a grid of numbers
– Smoothing, sharpening, measuring texture, …
• Image filters in the frequency domain (later)
– Filtering is a way to modify the frequencies of images
– Denoising, sampling, image compression,…
• Really important!
• Enhance images: Denoise, smooth, increase contrast, etc.
• Extract information from images:
• Texture, edges, distinctive points, etc.
• Detect patterns
• Template matching
82
82
• Spatial filtering
• affects directly to pixels in image
• Local transformation in the spatial domain can be represented as:
g(x,y) = T(f(x,y))
• T may affect the point (x,y) and its neighborhood (K) and output a
new value
(K: Filter/Mask/Kernel/Window/Template Processing)
• Same function applied at each position
• Output and input image are typically the same size
• Convolution: Linear filtering, function is a weighted
sum/difference of pixel values
I' = I * K
83
83
37
2/20/24
84
84
Spatial Convolution
Mask (kernel)
85
85
38
2/20/24
Spatial convolution
K: convolution kernel,
mask, filter, …
86
86
Spatial convolution
Source: http://machinelearninguru.com
87
87
39
2/20/24
Spatial convolution
I' = I * K
88
88
Spatial convolution
I' = I * K
89
89
40
2/20/24
Spatial convolution
I' = I * K
90
90
Spatial convolution
• Border problem?
• Ignore, set to 0
• Zero padding io the input matrix
• reflect across edge:
• f(-x,y) = f(x,y)
• f(-x,-y) = f(x,y)
• …
91
91
41
2/20/24
Spatial convolution
Source: http://machinelearninguru.com
92
92
93
93
42
2/20/24
• Correlation
94
94
95
95
43
2/20/24
Some kernels
• 2D spatial convolution
• is mostly used in image processing for feature extraction
• And is also the core block of Convolutional Neural Networks (CNNs)
• Each kernel has its own effect and is useful for a specific task such
as
• blurring (noise removing),
• sharpening,
• edge detection,
• …..
96
96
Smooth filtering
0 0 0
* 0 1 0
0 0 0
0 0 0
* 0 0 1
0 0 0
Filtered image
Original image Source: David Lowe (shifted left by 1 pixel)
97
97
44
2/20/24
Smooth filtering
• Application
• Noise remove
• Image blurring
98
98
1
𝑔(𝑥, 𝑦) = [𝑓(𝑥 − 1, 𝑦 − 1) + 𝑓(𝑥 − 1, 𝑦) + 𝑓(𝑥 − 1, 𝑦 + 1) +
9
𝑓(𝑥, 𝑦 − 1) + 𝑓(𝑥, 𝑦) + 𝑓(𝑥, 𝑦 + 1) +
𝑓(𝑥 + 1, 𝑦 − 1) + 𝑓(𝑥 + 1, 𝑦) + 𝑓(𝑥 + 1, 𝑦 + 1)]
99
45
2/20/24
• Mask:
• All elements of the mask are equal
• Results:
• Replacing each pixel with an average of its neigborhood
• Achieve smoothing effect
100
1 2 1
1
× 2 4 2
16
1 2 1
!
𝑔(𝑥, 𝑦) = [𝑓(𝑥 − 1, 𝑦 − 1) + 2𝑓(𝑥 − 1, 𝑦) + 𝑓 𝑥 − 1, 𝑦 + 1
!"
+ 2𝑓 𝑥, 𝑦 − 1 + 4𝑓 𝑥, 𝑦 + 2𝑓 𝑥, 𝑦 + 1
+ 𝑓 𝑥 + 1, 𝑦 − 1 + 2𝑓 𝑥 + 1, 𝑦 + 𝑓(𝑥 + 1, 𝑦 + 1)]
101
101
46
2/20/24
Gaussian filtering
102
102
Gaussian filtering
103
103
47
2/20/24
Smooth filtering
• General formulation
104
104
Smooth filtering
105
105
48
2/20/24
Sharpening filter
106
106
107
107
49
2/20/24
108
108
109
109
50
2/20/24
Derivative
• Second Derivative:
• Strong response for fine details and isolated points;
• Double response at step changes
110
110
First derivatives
• Filters used to compute the first derivatives of image
• Robert
• Prewitt
Robert kernels
• less sensitive to noise
• Smoothing with mean filter,
then compute1st derivative
• Sobel:
• less sensitive to noise Prewitt kernels
y Sobel kernels x
111
111
51
2/20/24
First derivatives
• Sobel
Vertical Edge
(absolute value)
112
112
First derivatives
• Sobel
Horizontal Edge
(absolute value)
113
113
52
2/20/24
𝜕I 𝑓 𝜕I 𝑓
∇I 𝑓 = +
𝜕𝑥 I 𝜕𝑦 I
𝜕 I𝑓
= 𝑓(𝑥 + 1, 𝑦) + 𝑓(𝑥 − 1, 𝑦) − 2𝑓(𝑥, 𝑦)
𝜕𝑥 I
𝜕 I𝑓
= 𝑓(𝑥, 𝑦 + 1) + 𝑓(𝑥, 𝑦 − 1) − 2𝑓(𝑥, 𝑦)
𝜕𝑦 I
114
114
115
115
53
2/20/24
∇) 𝑓 = 8𝑓(𝑥, 𝑦) − [𝑓(𝑥 + 1, 𝑦 + 1) +
-1 -1 -1
𝑓(𝑥 + 1, 𝑦) + 𝑓(𝑥 + 1, 𝑦 − 1) +
𝑓(𝑥 − 1, 𝑦 + 1) + 𝑓(𝑥 − 1, 𝑦) + -1 8 -1
𝑓(𝑥 − 1, 𝑦 − 1) + 𝑓(𝑥, 𝑦 + 1) + 𝑓(𝑥, 𝑦 − 1)]
• 45o isotropic filter -1 -1 -1
116
116
117
117
54
2/20/24
𝑓(𝑥, 𝑦) − ∇2 𝑓(𝑥, 𝑦)
𝑔(𝑥, 𝑦) = 4
𝑓(𝑥, 𝑦) + ∇2 𝑓(𝑥, 𝑦)
0 -1 0 0 0 0 0 -1 0
-1 5 -1 = 0 1 0 + -1 4 -1
0 -1 0 0 0 0 0 -1 0
-1 -1 -1 0 0 0 -1 -1 -1
-1 9 -1 = 0 1 0 + -1 8 -1
-1 -1 -1 0 0 0 -1 -1 -1
118
118
Sharpening filter
a) Original image
b) Laplacian filtering
c) Laplacian with
scaling
d) Adding original with
Laplacian image
119
119
55
2/20/24
Sharpening filter
120
120
Contents
56
2/20/24
Median filter
122
Median filter
Salt and
Median
pepper
noise filtered
Source: M. Hebert
123
57
2/20/24
Median filter
124
• The Maximum Filter: replaces the central pixel with the lightest one
125
125
58
2/20/24
Examples
126
126
Examples
Initial image Adding Salt and pepper noise Mean filter 3x3
127
59
2/20/24
Exercises
128
128
THANK YOU !
60