2. Digital Image Processing
2. Digital Image Processing
y x
The value of I at spatial coordinates (x, y) gives the intensity (or gray
level) of the image at that point, which depends on
The amount of source light incident on the scene being viewed
The amount of light reflected by the objects in the scene
2
Digitizing an Image
When both the spatial coordinates (x, y) and the intensity values are all
finite and discrete quantities, the image is called a digital image
To convert an image into digital form, the function I(x, y) has to be
sampled in both the coordinate and intensity values
Digitizing the coordinate values is called sampling
Digitizing the intensity values is called quantization
3
Digital Image Representation
A digital image can be represented as a N ´ M matrix I(i, j) of intensity
values
é I 0, 0 I 0,1 I 0, M -1 ù
ê I I1,1 I1, M -1 úú
I(i, j ) = ê
1, 0
ê ú
ê ú
ë I N -1,0 I N -1,1 I N -1, M -1 û
4
Quality of a Digital Image
The quality of a digital image is determined to a large degree by
the number of samples used in sampling (i.e., spatial resolution) and
the number of discrete gray levels used in quantization (i.e., gray-level
resolution)
When there is an insufficient number of samples, there will be the
problem of sampling checkerboards
When there is an insufficient number of gray levels, there will be the
problem of false contouring
5
Quality of a Digital Image
The problem of sampling checkerboards:
6
Quality of a Digital Image
The problem of false contouring:
7
Neighbors of a Pixel
Consider a pixel p at coordinates (x, y)
9
Adjacency
2 image subsets S1 and S2 are adjacent if some pixel in S1 is adjacent to
some pixel in S2
S1 S2
2 2 3 3 3 4 5 0 1 0 4-adjacent?
0 2 4 1 5 5 1 2 3 1 8-adjacent?
1 3 5 1 5 0 1 4 2 0
m-adjacent?
4 3 1 1 1 3 4 5 6 0
3 2 1 0 0 1 1 1 1 0
V = {0, 1}
10
Path
A path from pixel p with coordinates (x, y) to pixel q with coordinates
(s, t) is a sequence of distinct pixels with coordinates (x0, y0), (x1, y1), …,
(xn, yn) where (x0, y0) = (x, y), (xn, yn) = (s, t), and pixels (xi, yi) and
(xi-1, yi-1) are adjacent for 1 £ i £ n
The length of the path is n
The path is said to be closed if (x0, y0) = (xn, yn)
11
Path
Example: Find the shortest path from pixel p to pixel q
3 1 2 1 q shortest 4-path?
shortest 8-path?
2 2 0 2
shortest m-path?
1 2 1 1
p 1 0 1 2
V = {0, 1}
12
Connectivity
Let S represents a subset of pixels in an image
2 pixels p and q are said to be connected in S if there exists a path
between them consisting entirely of pixels in S
For any pixel p in S, the set of pixels that are connected to it in S is called
a connected component of S
If S has only one connected component, it is called a connected set
13
Connectivity
Example: Find the number of connected components in S
S
2 2 3 3 3 4 5 0 1 0 4-adjacency?
0 2 4 1 5 5 1 2 3 1 8-adjacency?
1 3 5 1 5 0 1 4 2 0
m-adjacency?
4 3 1 1 1 3 4 5 6 0
3 2 1 0 0 1 1 1 1 0
V = {0, 1}
14
Distance Measures
Consider pixels p, q, and z, with coordinates (x, y), (s, t), and (v, w),
respectively z
p
D is a distance function (or metric) if
D(p, q) ³ 0 (D(p, q) = 0 iff p = q),
D(p, q) = D(q, p), and
D(p, z) £ D(p, q) + D(q, z)
15
Distance Measures
Euclidean distance:
De(p, q) = sqrt[(x – s)2 + (y – t)2]
City-block distance (D4 distance):
D4(p, q) = |x – s| + |y – t|
Chessboard distance (D8 distance ):
D8(p, q) = max(|x – s| , |y – t|)
Dm distance
length of the shortest m-path between 2 pixels
2 2 2 2 2 2
2 1 2 2 1 1 1 2
2 1 0 1 2 2 1 0 1 2
2 1 2 2 1 1 1 2
2 2 2 2 2 2
city-block distance chessboard distance
16
Logic Operations
AND OR NOT
0 0 0 0 0 0 0 1
0 1 0 0 1 1 1 0
1 0 0 1 0 1
1 1 1 1 1 1
Only need to consider AND, OR and NOT logic operators, as any other
operator can be implemented by using only these three basic functions
Operate on a pixel-by-pixel basis
For grayscale images, pixel values are processed as strings of binary
numbers
17
Negative Transformation using NOT
NOT operator performs the same function as the negative
transformation
Example: consider an 8-bit pixel with a gray level of 127(x), its
corresponding binary string is thus 01111111(ii), and the result of the
NOT operator will be 10000000(ii) = 128(x)
0 1
1 0
NOT
18
Masking using AND/OR
AND and OR operators are used for masking
Masking is the process of selecting sub-images in an image, sometimes
is referred to as region of interest (ROI) processing
Examples:
AND
0 0 0
0 1 0 AND =
1 0 0
1 1 1
OR
0 0 0
0 1 1 OR =
1 0 1
1 1 1
19
Arithmetic Operations
Addition, Subtraction, Multiplication & Division
Performed on a pixel-by-pixel basis between 2 or more images
Useful for image enhancement and segmentation
20
Image Averaging using Addition and Division
Commonly used for noise reduction by observing the same scene over a
long period of time
Effective in removing zero-mean uncorrelated noise from an image
K -1
1
g ( x, y ) =
K
å g ( x, y )
i =0
i
K -1
1
=
K
å [ f ( x, y ) + n ( x, y ) ]
i =0
i
1 K -1 1 K -1
= å f ( x, y ) + å n ( x, y )
i
K i =0 K i =0
» f ( x, y )
21
Image Subtraction
To enhance the difference between 2 images
Commonly used for change detection
Rescaling may be needed to ensure all pixel values are within the
interval [0, L- 1]
L -1
g ' ( x, y) = {g ( x, y) - min[ g ( x, y)]}´
max[g ( x, y)] - min[ g ( x, y)]
Example: background subtraction
- =
rescaling
To extract the original object, create a mask using the difference image
and use AND/OR operator
22
Gray-Level Masks using Multiplication
Division can be considered as multiplying one image by the reciprocal of
the other
Multiplying an image by a constant can increase / decrease the average
gray level of the image
Multiplication can also be used to implement gray-level masks (as
compare to binary masks using AND and OR operators)
´ =
23
Convolution
The convolution of two functions f(x) and g(x), denoted by f(x)*g(x), is
defined as ¥
f ( x) * g ( x) = ò f (u ) g ( x - u )du
-¥
Example:
1
* = 1
1/2 1/2
x x x
1 1 1 2
24
Convolution
¥
Example: f ( x) * g ( x) = ò f (u ) g ( x - u )du
-¥
g(u) f(u)
1
1/2
u u
1 1
g(-u) g(x-u)
1/2 1/2
u u
-1 1 -1 x 1
f(u) f(x)*g(x)
1 1
g(x-u)
1/2
u x
1 1 2
x 25
x
Convolution
Convolution in 2D:
¥ ¥
f ( x, y ) * g ( x, y ) = ò ò f (u, v) g ( x - u, y - v)dudv
- ¥- ¥
Discrete cases:
M -1
1
f ( x) * g ( x) = å f (u) g ( x - u) where x = 0, 1, …, M-1
M u =0
M -1 N -1
1
f ( x , y ) * g ( x, y ) =
MN
åå f (u, v) g ( x - u, y - v)
u =0 v =0
where x = 0, 1, …, M-1
y = 0, 1, …, N-1
Convolution is symmetric, i.e.,
f ( x, y) * g ( x, y) = g ( x, y ) * f ( x, y )
Convolution is associative, i.e.,
[ f ( x, y) * g ( x, y)]* h( x, y) = f ( x, y) * [ g ( x, y ) * h( x, y)]
26
Linear Spatial Filtering
Linear spatial filtering in image processing
w(-1,-1) w(0,-1) w(1,-1)
is based on convolution, and is often referred
to as convolving a mask with an image w(-1,0) w(0,0) w(1,0)
a b
I 2 ( x, y ) = å å w(u, v) I ( x - u, y - v)
u = - av = - b
1
14 13 13 12
27
Linear Spatial Filtering
Problem occurs near the border of the
image when part of the mask is located
outside the image 12 16 18 12
28
Smoothing Filters
Smoothing filters are used for blurring and for noise reduction
The response of a linear smoothing filter is simply the weighted average
of the pixel intensities contained in the neighborhood of the mask
Examples: Linear smoothing filters
1 1 1 1 2 1
1/9 ´ 1 1 1 1/16 ´ 2 4 2
1 1 1 1 2 1
a 3´3 box filter a 3´3 weighted average filter
29
Sharpening Filters
Sharpening filters are used to highlight fine details in an image or to
enhance details that have been blurred
Sharpening is accomplished by spatial differentiation
Derivatives of a digital function can be
obtained by finite differences
𝜕𝐼
𝐼 𝑥 + 1, 𝑦 − 𝐼(𝑥, 𝑦) 𝐼 𝑥, 𝑦 − 𝐼(𝑥 − 1, 𝑦)
𝜕𝑥
𝜕!𝐼
𝐼 𝑥 + 1, 𝑦 + 𝐼(𝑥 − 1, 𝑦) − 2𝐼(𝑥, 𝑦)
𝜕𝑥 !
𝜕𝐼
𝐼 𝑥, 𝑦 + 1 − 𝐼(𝑥, 𝑦) 𝐼 𝑥, 𝑦 − 𝐼(𝑥, 𝑦 − 1)
𝜕𝑦
𝜕!𝐼
𝐼 𝑥, 𝑦 + 1 + 𝐼(𝑥, 𝑦 − 1) − 2𝐼(𝑥, 𝑦)
𝜕𝑦 !
30
Sharpening Filters
Example: Laplacian filter
𝜕!𝐼 𝜕!𝐼
∇! 𝐼 = !+ !
𝜕𝑥 𝜕𝑦
= [𝐼 𝑥 + 1, 𝑦 + 𝐼 𝑥 − 1, 𝑦 − 2𝐼 𝑥, 𝑦 ] +
[𝐼 𝑥, 𝑦 + 1 + 𝐼 𝑥, 𝑦 − 1 − 2𝐼 𝑥, 𝑦 ]
= 𝐼 𝑥 + 1, 𝑦 + 𝐼 𝑥 − 1, 𝑦 +
𝐼 𝑥, 𝑦 + 1 + 𝐼 𝑥, 𝑦 − 1 − 4𝐼(𝑥, 𝑦)
31
Composite Laplacian Masks
To preserve the background features in the image, the Laplacian image is
subtracted from / added to the original image to produce the enhanced
image:
If the center coefficient of the
ì I ( x, y) - Ñ 2 I ( x, y) Laplacian mask is negative
g ( x, y) = í
îI ( x, y) + Ñ I ( x, y)
2 If the center coefficient of the
Laplacian mask is positive
Example:
0 0 0 1 1 1 -1 -1 -1
0 1 0 1 -8 1 -1 9 -1
0 0 0 1 1 1 -1 -1 -1
- =
32
Nonlinear Smoothing Spatial Filters
Median filters replace the value of a pixel by the median of the pixel
intensities in the neighborhood of that pixel
Particularly effective in the presence of impulse noise, also called salt-
and-pepper noise because of its appearance as white (salt) and black
(pepper) dots superimposed on an image
Produce less blurring than linear smoothing filters of similar size
Example:
34
Color Models
Human eye perceive all colors as variable
combinations of the 3 primary colors:
Red (R), Green (G), and Blue (B)
The primary colors can be added to produce the
secondary colors
Green + Blue → Cyan (C)
Blue + Red → Magenta (M)
Red + Green → Yellow (Y)
Commonly used color models:
RGB (for color monitors)
CMY (for printers)
YIQ (for color TV broadcast)
HSI, HSV, …, etc.
35
RGB Model
Each color appears in its primary spectral components of red, green and
blue, i.e., (R,G,B)
All values of R, G, and B are assumed to
be in the range [0, 1]
Examples:
(1, 0, 0) = pure red
(0, 1, 0) = pure green
(0, 0, 1) = pure blue
Typically, each component is represented by 8 bits
Hence each pixel can be presented by 8´3 = 24 bits (pixel depth), giving
rise to 224 = 16 million different colors
A 24-bit RGB color image is called a full-color image
The grayscale is given by equal portions of R, G, and B
36
YIQ Model
Used in commercial TV broadcasting
RGB to YIQ conversion:
éY ù é0.299 0.587 0.114 ù é R ù
ê I ú = ê0.596 - 0.275 - 0.321ú êG ú
ê ú ê úê ú Converting a color image into
êëQ úû êë0.212 - 0.523 0.311 úû êë B úû
a monochrome image
The Y component gives the luminance, whereas the I and Q
components provide the color information
The Y component provides all the video information required by a
monochrome television set
A color image can be easily converted into a monochrome image by
taking only the Y component of the above conversion
I(i, j) = 0.299´R(i, j) + 0.587´G(i, j) + 0.114´B(i, j)
37