Content
1. Fundamental of spatial filtering
• Spatial Domain
• Frequency Domain
2. Linear Filters
3. Smoothing Filters
• Average Filter
• Gaussian Filtering
𝟏 𝟏 𝟏
4. Nonlinear Filter
𝟗 𝟗 𝟗
• Median filter 𝟏 𝟏 𝟏
h=
5. Sharpening Filters 𝟗 𝟗 𝟗
𝟏 𝟏 𝟏
• Laplacian Filter 𝟗 𝟗 𝟗
6. Edge Detection
• Sobel Operators
7. Combining image enhancement methods
Filtering is a fundamental operation in image processing. It can be used for:
1- image enhancement
2- Noise reduction
3- Edge detection,
4- Sharpening.
The concept of filtering has been applied in the:
Frequency domain, where it rejects some frequency components while
accepting others.
Spatial domain, Spatial filtering modifies an image by replacing the value of
each pixel with a function of the values of the pixel and its neighbors
(filtering is pixel neighborhood operation).
Commonly used spatial filtering techniques include median, average,
Gaussian filtering, etc.
The filtering function sometimes is called filter mask, or filter kernel.
They can be broadly classified into two different categories :
1. Linear filtering.
2. Nonlinear filtering ( Order-statistic filters).
An image can be represented in the form of a 2D
matrix where each element of the matrix
represents pixel intensity.
This state of 2D matrices that depict the
intensity distribution of an image is called
Spatial Domain. It can be represented as shown
below.
• For the RGB image, the spatial domain is
represented as a 3D vector of 2D matrices.
Each 2D matrix contains the intensities for a
single color as shown below.
• Each pixel intensity is represented as I(x, y)
where x, y is the co-ordinate of the pixel in the
2D matrix. Different operations are carried
out in this value.
In frequency-domain methods are based on the Fourier Transform of an
image.
Roughly, the term frequency in an image tells about the rate of change of
pixel values.
The tie between spatial- and frequency-domain processing is the Fourier
transform.
We use the Fourier transform to go from the spatial
To the frequency domain; to return to the spatial domain we use the
inverse Fourier transform.
The figure below depicts the
conversion of the image from
the spatial domain to the
frequency domain using Fourier
Transformation.
Q: Why we need a domain other than spatial domain ?
Answer
Many times, image processing tasks are best performed in a domain other
than the spatial domain. Moreover, it is easy to detect some features in a
particular domain, i.e., a new information can be obtained in other domains.
A linear spatial filter performs a sum-of-products operation between an
image f and a filter kernel, w.
The kernel is an array whose size defines the neighborhood of operation,
and whose coefficients determine the nature of the filter.
Other terms used to refer to a spatial filter kernel are mask, template,
and window.
We use the term filter kernel or simply kernel.
The equation below illustrates the mechanics of linear spatial filtering using a
kernel. At any point (x, y) in the image, the response, g(x, y), of the filter, is the
sum of products of the kernel coefficients and the image pixels encompassed
by the kernel:
g (x , y ) = w ( − 1, − 1) f ( x-1, y− 1) + w ( − 1, 0) f ( x − 1, y ) +
… + w (0, 0) f (x , y ) + … + w(1, 1) f ( x +1, y + 1)
As coordinates x and y are varied, the center of the kernel moves from pixel
to pixel, generating the filtered image, g, in the process
Observe that the center coefficient
of the kernel, w(0, 0), aligns with
the pixel at location (x, y).
For a kernel of size mxn. we
assume that m=2a+1 and
n=2b+1, where a and b are
nonnegative integers.
This means that our focus is on
kernels of odd size in both
coordinate directions.
In general, linear spatial
filtering of an image of size MxN
with a kernel of size mxn is given
by the expression
𝒂 𝒃
𝒈(𝒙, 𝒚) = 𝒘 𝒔, 𝒕 𝒇(𝒙 + 𝒔, 𝒚 + 𝒕)
𝒔=−𝒂 𝒕=−𝒃
where x and y are varied so that the center (origin) of the kernel visits every
pixel in f once.
Correlation consists of moving
the center of a kernel over an
image, and computing the sum
of products at each location.
We used correlation to check
similarity between two image
𝒂 𝒃
𝒈(𝒙, 𝒚) = 𝒘 𝒔, 𝒕 𝒇(𝒙 + 𝒔, 𝒚 + 𝒕)
𝒔=−𝒂 𝒕=−𝒃
The mechanics of spatial convolution
are the same, except that the
correlation kernel is rotated by 𝟏𝟖𝟎◦ .
Thus, when the values of a kernel are
symmetric about its center,
correlation and convolution yield the
same result.
Smoothing (also called averaging) spatial filters are used to reduce sharp
transitions in intensity.
Because random noise typically consists of sharp transitions in intensity, an
obvious application of smoothing is noise reduction.
Smoothing prior to image resampling to reduce aliasing, is also a common
application.
The commonly used smoothing filters are Averaging and Gaussian filters.
It can be performed using the convolution operation.
Where: h (m, n) is a filtering mask of size MxN.
Each element in this filter mask usually represents the weights used in the
linear combination.
The types of different linear filters correspond to different filter masks.
The sum of all the elements in h (m, n) will affect the overall intensities
of the output image.
Therefore, it is sensible to normalize h(m, n) such that the sum is equal to
one.
In the case of negative values in h (m, n) , the sum is typically set to zero.
For smoothing (low pass filters), the values in the mask must be positive;
otherwise, this mask will contain some edges (sharpening filters).
The average filtering is also called mean filtering. Where the output pixel
value is the mean of its neighborhood. Thus, the filtering mask is as follows
(3*3 as an example).
𝟏 𝟏 𝟏
𝟗 𝟗 𝟗
𝟏 𝟏 𝟏
h= 𝟗 𝟗 𝟗
𝟏 𝟏 𝟏
𝟗 𝟗 𝟗
In practice, the size of the mask controls the degree of smoothing and loss
of the details
Example :
Used average filter to smooth sub image as shown 6 8 9 10 12
7 15 6 14 13
3 7 5 9 3
14 16 10 7 8
Answer :
(6*1/9) +(8*1/9) +(9*1/9) +(7*1/9) +(15*1/9) +(6*1/9)
1/9 1/9 1/9 +(3*1/9) +(7*1/9) +(5*1/9) =7.33
1/9 1/9 1/9 OR
1/9 1/9 1/9 (6+8+9+7+15+6+3+7+5)/9 =7.33
6 8 9 10 12 6 8 9 10 12
7 15 6 14 13 7 7.33 13
3 7 5 9 3 3 3
14 16 10 7 8 14 16 10 7 8
(8*1/9) +(9*1/9) +(10*1/9) +(15*1/9) +(6*1/9) +(14*1/9)
1/9 1/9 1/9 +(7*1/9) +(5*1/9) +(9*1/9) =9.22
1/9 1/9 1/9 OR
(8+9+10+15+6+14+7+5+9)/9 =9.22
1/9 1/9 1/9
6 8 9 10 12 6 8 9 10 12
7 15 6 14 13 7 7.33 9.22 13
3 7 5 9 3 3 3
14 16 10 7 8 14 16 10 7 8
Gaussian filtering is another important filter. The weights in the filter mask
are of a Gaussian function.
Where: is the variance and
controls the degree of smoothing.
The larger value is, the larger
degree smoothing can be
achieved.
Notes:
1. The coefficient of average or mean mask sum to one, so the image
brightness will be retained.
2. The coefficients are all positive, so it will tend to blur the image.
3. This type of mean filter smooth out local variations within an image, so it
essentially a low pass filter. So a low filter can be used to attenuate image
noise that is composed primarily of high-frequency components.
The order-statistical filters are usually non-linear filters, which are
hardly represented by convolution.
The value of a given pixel in the output image is represented by some
statistic within its support neighborhood in the original image, such as
the median filter.
There are some other filters as well such as the max/min filter.
The median filter simply replaces the value of the pixel with the
median value within its neighborhood.
The max/min filter replaces the value of the pixel with the
maximum/minimum value within its neighborhood.
Those filters are normally non-linear and cannot be easily implemented
in the frequency domain.
However, the common elements of a filter are:
( 1 ) A neighborhood
( 2) An operation on the neighborhood include the pixel itself.
Typically the neighborhood is a rectangular of different size, for
example 3x3, 5x5 … and smaller than the image itself.
Figure below shows the filtered image using a median filter of different
mask sizes.
The median filter is a non-linear filter (order filter). These filters are
based on as specific type of image statistics called order statistics.
Typically, these filters operate on small sub image, “Window”, and
replace the center pixel value (similar to the convolution process).
Order statistics is a technique that arranges the entire pixel in
sequential order, given an NxN window (W) the pixel values can be
ordered from smallest to the largest.
I1 ≤ I2 ≤ I3...…………………< IN
Where: I1, I2, I3...……, IN
are the intensity values of
the subset of pixels in the
image.
Example:
Given the following 3X3 neighborhood :
1. Sort the value in order of size (3,3,4,4,5,5,5,6,7) . 5 5 6
2. Select the middle value , un this case it is 5. 3 4 5
3. This 5 is then placed in center location. 3 4 7
A median filter can use a neighborhood of any size, but 3X3, 5X5 and
7X7 are typical.
Note that the output image must be written to a separate image (a
buffer); so that the results are not corrupted as this process is
performed. (The median filtering operation is performed on an image by
applying the sliding window concepts, similar to what is done with
convolution).
The window is overlaid on the upper left corner of the image, and the
median is determined. This value is put into the output image (buffer)
corresponding to the center location of the window. The window is then
slide one pixel over, and the process is repeated.
Example 1:
Using median filters to remove salt and pepper noise, from sub image I(r , c).
5 16 20 15 17 20
21 2 25 23 34 19
First pixel:
43 51 19 18 40 42
1. Rearrange the 3x3 neighbor pixels in
15 18 25 23 38 40
ascending way such as:
52 44 34 12 10 13
2, 5, 16, 19, 20, 21, 25, 43, 51
2. By using median filter we select the median 5 16 20 15 17 20
value 20 and put it in buffer image in the 21 20 ? ? ? 19
position (1,1) 43 ? ? ? ? 42
15 ? ? ? ? 40
Second pixel: 52 44 34 12 10 13
1. Rearrange the 3x3 neighbor pixels in
5 16 20 15 17 20
ascending way such as:
21 20 19 ? ? 19
2, 15, 16, 18, 19, 20, 23, 25, 51
43 ? ? ? ? 42
2. By using median filter we select the median
15 ? ? ? ? 40
value 19 and put it in buffer image in the
52 44 34 12 10 13
position (1,1)
Example 2:
Using min. filters to remove salt and pepper noise, from sub image I(r , c).
5 16 20 15 17 20
21 13 25 23 34 19
First pixel:
43 51 19 22 40 42
1. Rearrange the 3x3 neighbor pixels in
15 18 25 23 38 40
ascending way such as:
52 44 34 12 10 13
5, 13, 16, 19, 20, 21, 25, 25, 43, 51
2. By using min. filter we select the minimum 5 16 20 15 17 20
value 5 and put it in buffer image in the 21 5 ? ? ? 19
position (1,1) 43 ? ? ? ? 42
15 ? ? ? ? 40
52 44 34 12 10 13
Second pixel:
1. Rearrange the 3x3 neighbor pixels in 5 16 20 15 17 20
ascending way such as: 21 5 13 ? ? 19
13,15,16,19,20,22,23,25,51 43 ? ? ? ? 42
2. By using min. filter we select the minimum 15 ? ? ? ? 40
value 13 and put it in buffer image in the 52 44 34 12 10 13
position (1,1)
Example 3:
Using max. filters to remove salt and pepper noise, from sub image I(r , c).
5 16 20 15 17 20
21 13 25 23 34 19
First pixel:
43 51 19 22 40 42
1. Rearrange the 3x3 neighbor pixels in
15 18 25 23 38 40
ascending way such as:
52 44 34 12 10 13
5,13,16,19,20, 21,25,25,43,51
2. By using max. filter we select the maximum 5 16 20 15 17 20
value 51 and put it in buffer image in the 21 51 ? ? ? 19
position (1,1) 43 ? ? ? ? 42
15 ? ? ? ? 40
52 44 34 12 10 13
Second pixel:
1. Rearrange the 3x3 neighbor pixels in 5 16 20 15 17 20
ascending way such as: 21 51 51 ? ? 19
13,15,16,19,20,22,23,25,51 43 ? ? ? ? 42
2. By using max. filter we select the maximum 15 ? ? ? ? 40
value 51 and put it in buffer image in the 52 44 34 12 10 13
position (1,1)
The objective of sharpening is to draw attention to the fine details of an
image. This is also related to the situation where an image that has been
blurred and now needs to be de-blurred.
In contrast to the process of image :
Smoothing that normally uses pixel averaging techniques,
Sharpening can be conducted using spatial differentiation.
The image differentiation actually:
Enhances edges
Discontinuities
Depresses the areas of slowly changing gray-level values.
The Sharpening filter indicates the filter should have positive coefficients
near its center and negative coefficients in the outer periphery.
These filters will tend to bring out, or enhance details in the image.
Example of convolution masks for the Laplacian-type filters are:
0 -1 0 0 1 0 -1 -1 -1 1 1 1
-1 4 -1 1 -4 1 -1 8 -1 1 -8 1
0 -1 0 0 1 0 -1 -1 -1 1 1 1
Rotating by 90 Rotating by 45
The sum of the coefficients in this kernel is zero, this mean that:
• when the kernel is over an area of constant (background area) or
slowly varying gray level, the result of convolution is zero or some very
small number.
• when gray level is varying rapidly within the neighborhood, the result
of the convolution can be large number.
This number can be positive or negative, because the kernel contains both
positive and negative coefficients; we therefore need to choose an output
image representation that supports negative number.
The main advantage of Laplacian filter is to increase the degree of
sharpening. It enhance details in all directions equally.
Image sharpening and
histograms :
(a) original image
( c ) histogram
(b) sharpened image
(d) histogram .
Edge detection operators are based on the idea that edge information in an
image is found by looking at the relationship a pixel has with its
neighborhood.
• If a pixel’s gray level value is similar to those around it, there is probably
not an edge at that point.
• If a pixel has a neighbor with widely varying gray levels, it may represent
an edge point.
In other words, an edge is defined by a discontinuity in gray-level. Ideally,
an edge separates two distinct objects.
Definitions:
• Edge is a location in an image where there is a sudden variation in the
gray level or color of pixels.
• Edge is a collection of pixel values whose brightness value changes
abruptly.
• Edges represent borders between regions on an object or in a scene.
The Sobel edge detection masks look for edges in both the horizontal and
vertical directions and then combine this information into a single metric.
The masks are as follows:
-1 -2 -1 -1 0 1
0 0 0 -2 0 2
1 2 1 -1 0 1
Row Mask Column Mask
These masks are each convolved with the image. At each pixel location we
now have two numbers:
S1, corresponding to the result form the row mask.
S2, corresponding to the result from the column mask.
We use this numbers to compute two matrices, the edge magnitude and the
edge direction, which are defined as follows:
𝑬𝒅𝒈𝒆 𝑴𝒂𝒈𝒏𝒊𝒕𝒖𝒅𝒆 = 𝒔𝟐𝟏 + 𝒔𝟐𝟐
𝒔𝟐
𝑬𝒅𝒈𝒆 𝑫𝒊𝒓𝒆𝒄𝒕𝒊𝒐𝒏 = 𝒕𝒂𝒏−𝟏
𝒔𝟏
Example:
Compute the convolution of the Sobel operators s1 and s2 with the image and show
the edge magnitude image:
3 5 7 3 5 7
2 4 3 2 11 3
1 8 6 1 8 6
Answer :
3 5 7 -1 -2 -1
2 4 3 0 0 0 = (3*-1 ) + ( 5*-2) + ( 7*-1) + (2*0) + (4*0) + (3*0)
+ ( 1*1) + (8*2) + ( 6*1) = -20+23= 3
1 8 6 1 2 1
Row Mask
3 5 7 -1 0 1
= (3*-1 ) + ( 5*0) + ( 7*1) + (2*-2) + (4*0) + (3*2)
2 4 3 -2 0 2 + ( 1*-1) + (8*0) + ( 6*1) = 11
1 8 6 -1 0 1
Column Mask
𝑬𝒅𝒈𝒆 𝑴𝒂𝒈𝒏𝒊𝒕𝒖𝒅𝒆 = 𝒔𝟐𝟏 + 𝒔𝟐𝟐 = 𝟑𝟐 + 𝟏𝟏𝟐 = 𝟗 + 𝟏𝟐𝟏 = 𝟏𝟑𝟎 = 11.4
𝒔 𝟏𝟏 = 𝒕𝒂𝒏−𝟏 (𝟑. 𝟔)
𝑬𝒅𝒈𝒆 𝑫𝒊𝒓𝒆𝒄𝒕𝒊𝒐𝒏 = 𝒕𝒂𝒏 −𝟏 𝟐 = 𝒕𝒂𝒏−𝟏
𝒔𝟏 𝟑
In real applications, it is hard to know what kind of noise has been added to
an image.
• Therefore, it is difficult to find a unique filter that can appropriately
enhance this noisy image.
However, it is possible if several de-blurring methods can be combined in a
framework in order to pursue a maximum demising outcome. We explain
one of the combinatorial techniques using an X-ray example.
Figure (a) illustrates a male chest’s X-ray image. The
purpose of the process is to highlight the middle cross
section of the image using the combination of
sharpening and edge detection.
Figure (b) shows the result of applying the median
filter.
Figure (c) is the outcome of using Sobel edge detection.
Figure (d) demonstrates the combination of the
Laplacian and Sobel process.
Figure (d) shows the edge details in the graph, which
highlights the structure of the central cross section of
the image.