You are on page 1of 17

DIGITAL IMAGE

PROCESSING Image Enhancement in the


Spatial Domain
HISTOGRAM
6 6 7 7 6
The histogram of a digital image with 5 2 2 3 4
gray levels in the range [0, L-1] is a
discrete function 3 3 4 4 5
5 7 3 6 2
h(rk)=nk,
7 6 5 5 4
where rk is the kth gray level
and nk is the number of pixels in the
image having gray level rk. 0 1 2 3 4 5 6 7 tota
l
To simplify this even further: 0 0 3 4 4 5 5 4 25
It is defined as a plot of frequency of
occurrence of different grey levels.
FREQUENC
Y
OF
OCCURRE
NCE

INTENSITY OF GREY LEVELS


SPATIAL FILTERING

some neighborhood operations work The values in a filter subimage are


with the values of the image pixels in referred to as coefficients, rather than
the neighborhood and the corresponding pixels.
values of a subimage that has the same
dimensions as the neighborhood.
The subimage is called a filter, mask,
kernel, template, or window, with the
first three terms being the most
prevalent terminology.
THE MECHANICS OF SPATIAL
FILTERING
The process consists simply of moving For the 3*3 mask shown in Fig. 2, the
the filter mask from point to point in an result (or response), R, of linear filtering
image. with the filter mask at a point (x, y) in
the image is which we see is the sum of
At each point (x, y), the response of the products of the mask coefficients with
filter at that point is calculated using a the corresponding pixels directly under
predefined relationship. the mask.
For linear spatial filtering, the response Note in particular that the coefficient
is given by a sum of products of the w(0, 0) coincides with image value f(x,
filter coefficients and the corresponding y), indicating that the mask is centered
image pixels in the area spanned by the at (x, y) when the computation of the
filter mask. sum of products takes place.
R = w(-1, -1)f(x - 1, y - 1) +
w(-1, 0)f(x - 1, y) + w(0, 0)f(x, y) +
w(1, 0)f(x + 1, y) + w(1, 1)f(x + 1, y +
1).
THE ISSUES WITH BORDERS
An important consideration in If the center of the mask moves any closer to
implementing neighborhood operations the border, one or more rows or columns of
for spatial filtering is the issue of what the mask will be located outside the image
plane.
happens when the center of the filter
approaches the border of the image. There are several ways to handle this
situation:
Consider for simplicity a square mask of
size n*n. 1. The simplest is to limit the excursions of
the center of the mask to be at a distance no
At least one edge of such a mask will less than (n-1)\2 pixels from the border.
coincide with the border of the image The resulting filtered image will be smaller
when the center of the mask is at a than the original, but all the pixels in the
distance of (n-1)\2 pixels away from filtered imaged will have been processed with
the border of the image. the full mask.
CONT.
If the result is required to be the same 3. Other approaches include “padding”
size as the original, then the image by adding rows and columns
of 0’s (or other constant gray level), or
2. the approach typically employed is to padding by replicating rows or columns.
filter all pixels only with the section of
the mask that is fully contained in the The padding is then stripped off at the
image. end of the process.
With this approach, there will be bands This keeps the size of the filtered image
of pixels near the border that will the same as the original, but the values
have been processed with a partial of the padding will have an effect near
filter mask. the edges that becomes more prevalent
as the size of the mask increases
SMOOTHING SPATIAL
FILTERS
Smoothing filters are used for blurring Smoothing Linear Filters
and for noise reduction.
The output (response) of a smoothing,
Blurring is used in preprocessing steps, linear spatial filter is simply the
such as removal of small details from average of the pixels contained in the
an image prior to (large) object neighborhood of the filter mask.
extraction, and bridging of small gaps
in lines or curves. These filters sometimes are called
averaging filters.
Noise reduction can be accomplished by
blurring with a linear filter and also by
nonlinear filtering. they also are referred to a lowpass
filters.
MECHANICS OF A SMOOTHING
(AVERAGING) FILTER
The idea behind smoothing filters is However, edges (which almost always
straightforward. are desirable features of an image) also
By replacing the value of every pixel in an are characterized by sharp transitions in
image by the average of the gray levels in the gray levels, so averaging filters
neighborhood defined by the filter mask, this
process results in an image with reduced have the undesirable side effect
“sharp” transitions in gray levels. that they blur edges.

Because random noise typically consists of


sharp transitions in gray levels, the most
obvious application of smoothing is noise
reduction.
BOX AVERAGING FILTER
Use of this filter yields the standard average of
the pixels under the mask.
This can best be seen by substituting the
coefficients of the mask into

The idea here is that it is computationally more


efficient to have coefficients valued 1.
At the end of the filtering process the entire image
which is the average of the gray levels of the is divided by 9.
pixels in the 3*3 neighborhood defined by the An m*n mask would have a normalizing constant
mask. equal to 1\m*n.
Note that, instead of being 1\9, the coefficients A spatial averaging filter in which all coefficients
of the filter are all 1’s. are equal is sometimes called a box filter.
WEIGHTED AVERAGE FILTER
This mask yields a so-called weighted The other pixels are inversely weighted as a
average, terminology used to indicate function of their distance from the center of
that pixels are multiplied by different the mask.
coefficients, thus giving more The basic strategy behind weighing the center
importance (weight) to some pixels at point the highest and then reducing the value
the expense of others. of the coefficients as a function of increasing
distance from the origin is simply an attempt
in this mask the pixel at the center of the to reduce blurring in the smoothing process.
mask is multiplied by a higher value
than any other, thus giving this pixel
more importance in the calculation of
the average.
CONT.
the general implementation for filtering Where,
an M*N image with a weighted
averaging filter of size m*n (m and n a=(m-1)/2 and b=(n-1)/2.
odd) is given by the expression x=0, 1, 2,…., M-1 and
y=0, 1, 2,…., N-1
EXAMPLE
(a) Original image, of size 500*500 pixels. (b)–(f) Results
of smoothing with square averaging filter masks of sizes
n=3, 5, 9, 15, and 35, respectively.
The black squares at the top are of sizes 3, 5, 9, 15, 25, 35,
45, and 55 pixels, respectively; their borders are 25 pixels
apart.
The letters at the bottom range in size from 10 to 24 points,
in increments of 2 points; the large letter at the top is 60
points.
The vertical bars are 5 pixels wide and 100 pixels high;
their separation is 20 pixels.
The diameter of the circles is 25 pixels, and their borders
are 15 pixels apart; their gray levels range from 0% to
100% black in increments of 20%.
The background of the image is 10% black. The noisy
rectangles are of size 50*120 pixels
ORDER-STATISTICS FILTERS

Order-statistics filters are nonlinear spatial Median filters are quite popular because,
filters whose response is based on ordering for certain types of random noise, they
(ranking) the pixels contained in the image provide excellent noise-reduction
area encompassed by the filter, and then capabilities, with considerably less
replacing the value of the center pixel with blurring than linear smoothing filters
the value determined by the ranking result. of similar size.
The best-known example in this category
is the median filter, which, as its name
implies, replaces the value of a pixel by Median filters are particularly effective in
the median of the gray levels in the the presence of impulse noise, also called
neighborhood of that pixel (the original salt-and-pepper noise because of its
value of the pixel is included in the appearance as white and black dots
computation of the median). superimposed on an image.
MEDIAN FILTER
The median, j, of a set of values is such that When several values in a neighborhood
half the values in the set are less than or are the same, all equal values are grouped.
equal to j, and half are greater than or equal
to j. For example, suppose that a 3*3
neighborhood has values (10, 20, 20, 20,
In order to perform median filtering at a 15, 20, 20, 25, 100).
point in an image, we first sort the values of
the pixel in question and its neighbors, These values are sorted as (10, 15, 20, 20,
determine their median, and assign this 20, 20, 20, 25, 100), which results in a
value to that pixel. median of 20.
For example, in a 3*3 neighborhood the Thus, the principal function of median
median is the 5th largest value, in a 5*5 filters is to force points with distinct gray
neighborhood the 13th largest value, and so levels to be more like their neighbors
on.

You might also like