Digital Image Processing CPE3643
Lecture 8:
Arithmetic and Logical Operations
Spatial Filtering
3/27/2025 Capital University of Science and Technology Digital Image Processing CPE3643 1
In the last lecture
▪ Adaptive local enhancement using statistical parameters
3/27/2025 Capital University of Science and Technology Digital Image Processing CPE3643 2
1
Mathematical/logical operations on images
▪ Addition
– Averaging images for noise removal
▪ Subtraction
– Removal of background from images
– Image enhancement
– Image matching
– Moving/displaced object tracking
▪ Multiplication
– Superimposing of texture on an image
– Convolution and correlation of images
▪ And and or operations
– To remove unnecessary area of image through mask operations
3/27/2025 Capital University of Science and Technology Digital Image Processing CPE3643 3
Image averaging for noise reduction
A noisy image can be represented by
g ( x, y) = f ( x, y) + ( x, y),
where ( x, y) denotes the noise in the image
Since the noise is random and the content f ( x, y) is fixed,
The noise can be removed by taking more noisy images of
the same object and averaging them out
K
1
g ( x, y ) =
K
g ( x, y),
i =1
i
3/27/2025 Capital University of Science and Technology Digital Image Processing CPE3643 4
2
Image averaging for noise reduction
Original image Noisy image
Result of Using 16 noise Using 64 noise Using 128 noise
averaging using samples samples samples
8 noise samples
3/27/2025 Capital University of Science and Technology Digital Image Processing CPE3643 5
Image averaging for noise reduction
Noisy image
Noise
reduction by
averaging
256 samples
3/27/2025 Capital University of Science and Technology Digital Image Processing CPE3643 6
3
Examples of image subtraction
Original image Image after moving one coin
Difference image after
pixel by pixel subtraction
of second image from first
image
3/27/2025 Capital University of Science and Technology Digital Image Processing CPE3643 7
Examples of image subtraction
Difference of images from quality control: a missing chip in PCB is detected
by subtracting the master image from image of each sample
3/27/2025 Capital University of Science and Technology Digital Image Processing CPE3643 8
4
Examples of image Multiplication
Multiplication of images can be used for superimposing
texture on an image
Smooth spherical surface Texture to be
output image
image superimposed
3/27/2025 Capital University of Science and Technology Digital Image Processing CPE3643 9
Example of logical operations using masks
3/27/2025 Capital University of Science and Technology Digital Image Processing CPE3643 10
10
5
Local enhancement through spatial filtering
▪ The output intensity value at (x,y) depends not only on
the input intensity value at (x,y) but also on the specified
number of neighboring intensity values around (x,y)
▪ Spatial masks (also called window, filter, kernel,
template) are used and convolved over the entire image
for local enhancement (spatial filtering)
▪ The size of the masks determines the number of
neighboring pixels which influence the output value at
(x,y)
▪ The values (coefficients) of the mask determine the
nature and properties of enhancing technique
3/27/2025 Capital University of Science and Technology Digital Image Processing CPE3643 11
11
Local enhancement through spatial filtering
The mechanics of spatial
filtering
For an image of size M x N and
a mask of size m x n
The resulting output gray level
for any coordinates x and y is
given by
a b
g ( x, y ) = w(s, t ) f ( x + s, y + t )
s =− a t =−b
where a = (m − 1) / 2, b = (n − 1) / 2
x = 0, 1, 2,......, M − 1, y = 0, 1, 2,......, N − 1,
3/27/2025 Capital University of Science and Technology Digital Image Processing CPE3643 12
12
6
Basics of spatial filtering
▪ Given the 3×3 mask with coefficients: w1, w2,…, w9
▪ The mask cover the pixels with gray levels: z1, z2,…, z9
▪ z gives the output intensity value for the processed image
(to be stored in a new array) at the location of z5 in the
input image
3/27/2025 Capital University of Science and Technology Digital Image Processing CPE3643 13
13
Basics of spatial filtering
Mask operation near the image border
Problem arises when part of the mask is located outside the image
plane; to handle the problem:
1. Discard the problem pixels (e.g. 512x512input 510x510output if
mask size is 3x3)
2. Zero padding: expand the input image by padding zeros
(512x512input 514x514output)
– Zero padding is not good create artificial lines or edges on
the border;
3. We normally use the gray levels of border pixels to fill up the
expanded region (for 3x3 mask). For larger masks a border region
equal to half of the mask size is mirrored on the expanded region.
3/27/2025 Capital University of Science and Technology Digital Image Processing CPE3643 14
14
7
Mask operation near the image border
3/27/2025 Capital University of Science and Technology Digital Image Processing CPE3643 15
15
Spatial filtering for Smoothing
▪ For blurring/noise reduction;
▪ Blurring is usually used in preprocessing steps,
– to remove small details from an image prior to object
extraction,
– or to bridge small gaps in lines or curves
▪ Equivalent to Low-pass spatial filtering in frequency domain
because smaller (high frequency) details are removed based on
neighborhood averaging (averaging filters)
Implementation: simplest form: averaging with a square mask
(assume m×m mask) with the all coefficients
equal to 1/m2 (averaging).
Applications: Reduce noise; smooth false contours
Side effect: Edge blurring
3/27/2025 Capital University of Science and Technology Digital Image Processing CPE3643 16
16
8
Smoothing filters
3/27/2025 Capital University of Science and Technology Digital Image Processing CPE3643 17
17
Spatial filtering for Smoothing (example)
3/27/2025 Capital University of Science and Technology Digital Image Processing CPE3643 18
18
9
Spatial filtering for Smoothing (example)
Original image Smoothed by
size: 500 x 500 3 x 3 box filter
Smoothed by Smoothed by
5 x 5 box filter 9 x 9 box filter
Smoothed by Smoothed by
15 x 15 box filter 35 x 35 box filter
3/27/2025 Capital University of Science and Technology Digital Image Processing CPE3643 19
19
Spatial filtering for Smoothing (example)
3/27/2025 Capital University of Science and Technology Digital Image Processing CPE3643 20
20
10
Order-statistics filtering
▪ Nonlinear spatial filters
▪ Output is based on order of gray levels in mask (subimage)
▪ Examples: Median filtering, Max & Min filtering
Median filtering
▪ Assigns the mid value of all the gray levels in the mask to the
center of mask;
▪ Particularly effective when
– noise pattern consists of strong, spiky components (impulse
noise, salt-and-pepper
– edges are to be preserved
– Force points with distinct gray levels to be more like their
neighbors
3/27/2025 Capital University of Science and Technology Digital Image Processing CPE3643 21
21
Median Filtering
3/27/2025 Capital University of Science and Technology Digital Image Processing CPE3643 22
22
11
Median Filtering (example)
3/27/2025 Capital University of Science and Technology Digital Image Processing CPE3643 23
23
Spatial filtering for image sharpening
Background: to highlight fine detail in an image or to
enhance blurred detail
Applications: electronic printing, medical imaging,
industrial inspection, autonomous target
detection (smart weapons)......
Foundation:
▪ Blurring/smoothing is performed by spatial averaging
(equivalent to integration)
▪ Sharpening is performed by noting only the gray level
changes in the image that is the differentiation
3/27/2025 Capital University of Science and Technology Digital Image Processing CPE3643 24
24
12
Spatial filtering for image sharpening
Operation of Image Differentiation
▪ Enhance edges and discontinuities (magnitude of output
gray level >>0)
▪ De-emphasize areas with slowly varying gray-level
values (output gray level: 0)
Mathematical Basis of Filtering for Image Sharpening
▪ First-order and second-order derivatives
▪ Approximation in discrete-space domain
▪ Implementation by mask filtering
3/27/2025 Capital University of Science and Technology Digital Image Processing CPE3643 25
25
13