5. Digital Image Processing - Intensity Transformation & Spatial Filtering
5. Digital Image Processing - Intensity Transformation & Spatial Filtering
12/19/2024 Le Thanh Ha 2
Digital Image
• A continuous image a(x,y) is sampled and quantized to form a
digital image presented by a matrix with N rows and M column.
One element in the matrix is called “Pixel”.
• The value of a pixel , called gray level, is assigned with an
integer I[m,n] {m=0,1,2,...,M-1} and {n=0,1,2,...,N-1}
12/19/2024 Le Thanh Ha 3
12/19/2024 Le Thanh Ha 4
Values frequently used
row N 256,512,525,625,1024,1035
Column M 256,512,768,1024,1320
12/19/2024 5 Le Thanh Ha
Image operator types
• Image operator takes images as inputs and also output images.
• Operators for Images are classified based on the affecting
region of the operators. There are three types:
– Point
– Local
– Global
12/19/2024 Le Thanh Ha 6
Image operator types
Operator Describe Complexity
12/19/2024 7 Le Thanh Ha
Các thao tác trên ảnh (…)
12/19/2024 Le Thanh Ha 8
-Image negatives
-Power-law (gamma) transformations
- Contrast stretching
- Intensity-level slicing
-Histogram processing
INTENSITY TRANSFORMATION
(POINT OPERATORS)
12/19/2024 Le Thanh Ha 9
Image negatives
• I(x,y) = 255-I(x,y)
12/19/2024 Le Thanh Ha 10
Power-law (gamma) transformations
12/19/2024 Le Thanh Ha 11
Power-law (gamma) transformations
12/19/2024 Le Thanh Ha 12
Contrast stretching
• Low-contrast
images can
result from:
– Poor
illumination
– Small sensitive
range in imaging
sensor
–…
12/19/2024 Le Thanh Ha 13
Intensity-level slicing
12/19/2024 Le Thanh Ha 14
Histogram processing
• Simply, histogram shows the count number of each gray level.
• In maths, histogram is the probability mass function of
intensity random variable.
• Histogram applications:
– Easily to be implemented in hardware
– How contrast an image is.
– Useful for applications involved with image segmentation and
compression.
–…
12/19/2024 Le Thanh Ha 15
Histogram processing
• H(i) is the number of pixels
1 2 1 2 1 1 with gray level i.
1 3 3 2 3 1 – H(1) = 14
– H(2) = 8
1 5 6 5 5 1
– H(3) = 6
2 5 7 7 4 2
– H(4) = 1
1 3 3 2 3 1
– H(5) = 4
1 2 1 1 1 2 – H(6) = 1
– H(7) = 2
12/19/2024 Le Thanh Ha 16
Histogram processing
• Dark image: large numbers at
low intensity range.
• Light image: large numbers at
high intensity range.
• Low-contrast image:
histogram is condensed.
• High-contrast image:
histogram is look like a
uniform distribution.
12/19/2024 Le Thanh Ha 17
Histogram Equalization
• Is used to increase the contrast of a given
image by linearizing its histogram.
12/19/2024 Le Thanh Ha 18
Histogram Equalization
• Pseudocode
– Input I[M,N]:image, L:maximum gray level.
– Output Ihe[M,N]: histogram equalized image.
– Procedure:
1.
2.
Calculate histogram pI for image I.
T[0] = pI[0]
0 0 0 0
3. For k=1 to L
T[k] = T[k-1] + pI[k]
1
1 2 2
I
End for
4. For r=0 to L
S[r] = round(T[r]*L) 0 0 0 2
End for
2 2 3 3
5. For y=0 to M-1
For x=0 to N-1
r = I[y,x]
Ihe[y,x] = S[r]
End for
End for
12/19/2024 Le Thanh Ha 19
Histogram Equalization
• Equalize histogram of following image, (L=7):
0 0 0 0
1
1 2 2
I
0 0 0 2
2 2 3 3
12/19/2024 Le Thanh Ha 20
Histogram Equalization
12/19/2024 Le Thanh Ha 21
-Convolution
-Convolution for Image Processing
-Average filtering
-Median filtering
SPATIAL FILTERING
12/19/2024 Le Thanh Ha 22
Convolution
f g (t ) f ( ) g (t )d
12/19/2024 Le Thanh Ha 23
Convolution
• Convolution of 2D continuous signals:
f g ( x, y ) f ( x , y ) g ( x x , y y )d x d y
12/19/2024 Le Thanh Ha 24
Properties of convolution
• Commulative:
c = a*b = b*a
• Associativity:
d = a*(b*c) = (a*b)*c
• Distributivity
d = a*(b+c) = a*b + a*c
12/19/2024 Le Thanh Ha 25
Convolution for Image Processing
I f
12/19/2024 Le Thanh Ha 27
Convolution for Image Processing
• Filtering
f [ x, y ] I [ x, y ] h[ x, y ]
– I is an Image, h is a mask
– A pixel called anchor for the mask (topleft, or center)
12/19/2024 Le Thanh Ha 28
Convolution for Image Processing
1/8 0 1/8
12/19/2024 Le Thanh Ha 29
Convolution for Image Processing
12/19/2024 Le Thanh Ha 30
Border problem
• It happens when calculating convolution at border points
where some points of mask is placed outside of the image.
• It can be solved by padding the missing pixel by zero value
or copy vale from the nearest available pixels.
12/19/2024 Le Thanh Ha 31
Mask shape
• The value of output pixel at a specific location depends on the
values of the pixel’s neighbors
4-neighbor 8-neighbor
Mask size 3x3 Mask size 5x5
12/19/2024 Le Thanh Ha 32
Mask shape
12/19/2024 Le Thanh Ha 33
Local Image Processing methods
• Smoothing and noise removal • Edge detection and highboost filtering
– Average filtering – Gradient methods
– Median filtering – Laplace method
– Probability filtering – Highboost filtering
12/19/2024 Le Thanh Ha 34
Average Filter
• Random noise typically consists of sharp transitions in gray
levels
12/19/2024 Le Thanh Ha 35
Average Filter
• Average filter is to replace the value of every pixel by the
average of the gray levels in the neighborhood defined by the
filter mask.
12/19/2024 Le Thanh Ha 36
Average Filter
1/ 9 1/ 9 1/ 9 1 1 1
h3 1/ 9 1/ 9 1/ 9 1
h4 1 2 1
1/ 9 1/ 9 1/ 9 10
1 1 1
12/19/2024 Le Thanh Ha 37
Average Filter
Mask: h1 Mask: h2
Original Image
Mask: h3 Mask: h4
12/19/2024 Le Thanh Ha 38
Average Filter
• The effects of smoothing
as a function of filter size
– The larger is the size, the
more significant blurring
observed.
12/19/2024 Le Thanh Ha 39
Median Filter
• For each p pixel in the image:
– Collect all neighbor pixels which are in the mask.
– Sort these pixel in the order of pixel’s value.
– Chose the pixel at the center of the sorted pixel array.
– Assign the value of chosen pixel to p.
12/19/2024 Le Thanh Ha 40
Median Filter
• Median Filter is different from Average Filter.
• A = {5 4 1 6 2 7 2 6 2 10 2 3}
• The average value is 4.667
• The median value:
– Sort array A = [1 2 2 2 2 3 4 5 6 6 7 10]
– Choose the the center value A = [1 2 2 2 2 3 4 5 6 6 7 10]
– The median value is 3
12/19/2024 Le Thanh Ha 41
Median Filter
Noised images Noise removed
Orignal Image
12/19/2024 Le Thanh Ha 42
Edge detection - Gradient
• Edges in 1 dimesion:
Ramp edge
Step edge
Line
Roof edge
12/19/2024 Le Thanh Ha 43
Gradient
12/19/2024 Le Thanh Ha 45
Edge detection - Gradient
12/19/2024 Le Thanh Ha 46
Edge detection - Gradient
• Pixel Difference masks
0 0 0 0 1 0
0 1 1 0 1 0
0 0 0 0 0 0
12/19/2024 Le Thanh Ha 47
Edge Detection – Gradient
Column
edges
Original Image Final edges
Row
edges
12/19/2024 Le Thanh Ha 48
Edge detection - Gradient
Column Row
0 0 1 1 0
0 1 0 0 1 0
0 0 0 0 0 0
12/19/2024 Le Thanh Ha 49
Edge detection - Gradient
Column
edges
Original Image Final edges
Row
edges
12/19/2024 Le Thanh Ha 50
12/19/2024 50
Edge detection - Gradient
• Prewitt masks
Column Row
1 0 1 1 1 1
1 1
1 0 1 0 0 0
3 3
1 0 1 1 1 1
12/19/2024 Le Thanh Ha 51
Edge detection - Gradient
Row
edges
Original Image Final edges
Column
edges
12/19/2024 Le Thanh Ha 52
Edge detection - Gradient
• Sobel masks
Column Row
1 0 1 1 2 1
1 1
2 0 2 0 0 0
4 4
1 0 1 1 2 1
12/19/2024 Le Thanh Ha 53
Edge detection - Gradient
Column
Original Image Final edges
Row
12/19/2024 Le Thanh Ha 54
12/19/2024 54
Edge detection - compass
• Image is convoluted with 8 masks
12/19/2024 Le Thanh Ha 55
Edge detection - compass
• T is a template mask
• Let T0=T; Ti is obtained by rotating T with an
angle of i*/4.
• A(x,y)=max{|Ti*I(x,y)|: i=0,1,2,...,7}
1 1 1 5 3 3
Pr ewitt 1 2 1 ; Kirsh 5 0 3 ;
1 1 1 5 3 3
1 0 1 1 0 1
Robinson Robinson
1 0 1 ; 2 0 2
bac3 bac5
1 0 1 1 0 1
12/19/2024 Le Thanh Ha 56
Edge detection - compass
Example: Kirsh mask
5 3 3 5 5 3 5 5 5 3 5 5
H 0 5 0 3 ; H1 5 0 3 ; H 2 3 0 3 ; H 3 3 0 5 ;
5 3 3 3 3 3 3 3 3 3 3 3
3 3 5 3 3 3 3 3 3 3 3 3
H 4 3 0 5 ; H 5 3 0 5 ; H 6 3 0 3 ; H 7 5 0 3 ;
3 3 5 3 5 5 5 5 5 5 5 3
12/19/2024 Le Thanh Ha 57
Edge detection – Laplace
2 f ( x, y ) 2 f ( x, y )
G ( x, y ) 2
2
x y
12/19/2024 Le Thanh Ha 58
Edge detection - Laplace
• In discrete domain, Laplace edge is approximated by the
second order of dirivative:
G ( x, y ) [ f ( x, y ) f ( x, y 1] [ f ( x, y 1) f ( x, y )]
[ f ( x, y ) f ( x 1, y ] [ f ( x 1, y ) f ( x, y )]
f ( x, y ) H ( x, y )
12/19/2024 Le Thanh Ha 59
Edge detection - Laplace
• Mask H
0 0 0 0 1 0
H 1 2 1 0 2 0
0 0 0 0 1 0
0 1 0
1 4 1
0 1 0
12/19/2024 Le Thanh Ha 60
Edge detection - Laplace
Original image I |I*H|
12/19/2024 Le Thanh Ha 61
Highboost filtering
12/19/2024 Le Thanh Ha 62
Highboost filtering
• Overall method:
I highboost c I orginal I highpass
0 0 0
c 0 1 0 H I original
0 0 0
• Using Laplace mask
0 0 0 0 1 0 0 1 0
I highboost c 0 1 0 1 4 1 I original 1 c 4 1 Ioriginal
0 0 0 0 1 0 0 1 0
12/19/2024 Le Thanh Ha 63
Highboost filtering
12/19/2024 Le Thanh Ha 64