TRƯỜNG ĐẠI HỌC CÔNG NGHIỆP TP HỒ CHÍ MINH
Dr. Tran Minh Chinh
References
“Digital Image Processing”, Fourth Edition,
Rafael C. Gonzalez & Richard E. Woods.
Richard Szeliski. Computer Vision:
Algorithms and Applications. Springer, 2011.
Forsyth D. Computer Vision: A Modern
Approach. Prentice Hall. 2011.
2
Chapter 3:
IMAGE ENHANCEMENT
IN THE SPATIAL DOMAIN
4
Chapter 3
3.1 BACKGROUND
5
3.1. BACKGROUND
Spatial domain techniques operate directly on the pixels
of an image
Two categories of spatial (domain) processing
Intensity transformation (Point operations):
oOperate on single pixels
oContrast manipulation, image thresholding
Spatial filtering (Spatial operations)
oWork in a neighborhood of every pixel in an image
oImage smoothing, image sharpening
6
3.1. BACKGROUND
THE BASICS OF INTENSITY TRANSFORMATIONS AND SPATIAL FILTERING
The spatial domain processes are based on the
expression
g ( x , y ) T f ( x, y )
f(x, y): input image
g(x, y): output (processed) image
T: operator
• Operator T is defined over a neighborhood of point (x, y)
• The operator can be applied to the pixels of a single image
or to the pixels of a set of images.
7
3.1. BACKGROUND
The point (x0, y0) shown is
an arbitrary location in the
image, and the small region
shown is a neighborhood of
(x0, y0). Typically, the
neighborhood is
rectangular, centered on (x0,
y0), and much smaller in
size than the image.
For any location (x, y), output image g(x, y) is equal to the
result of applying T to the neighborhood of (x, y) in f.
8
3.1. BACKGROUND
The smallest possible neighborhood is of size 1x1. In this
case, g depends only on the value of f at a single point (x, y)
and becomes an intensity (also called a gray-level, or
mapping) transformation function of the form
s T (r )
Where s and r denote, respectively, the intensity of g and f at
any point (x, y).
9
3.1. BACKGROUND
EXAMPLE
The result of applying the
transformation to every pixel in f to
generate the corresponding pixels in
g would be to produce an image of
higher contrast than the original, by
darkening the intensity levels below k
and brightening the levels above k. Contrast stretching function
10
3.1. BACKGROUND
EXAMPLE
T(r) produces a two level (binary)
image. A mapping of this form is
called a thresholding function.
Some simple yet powerful
processing approaches can be
formulated with intensity Thresholding function
transformation functions.
11
Chapter 3
3.2 IMAGE ENHANCEMENT
USING POINT OPERATIONS
12
3.2. SOME BASIC INTENSITY
TRANSFORMATION FUNCTIONS
Some basic intensity transformation functions. Each curve
was scaled independently so that all curves would fit in the
same graph 13
3.2. SOME BASIC INTENSITY
TRANSFORMATION FUNCTIONS
3.2.1. IMAGE NEGATIVES
The negative of an image with intensity levels in the
range [ 0, L-1] is obtained by using the negative
transformation function
s L 1 r
Reversing the intensity levels of a digital image in this
manner produces the equivalent of a photographic negative.
This type of processing is used, for example, in enhancing
white or gray detail embedded in dark regions of an image,
especially when the black areas are dominant in size.
14
3.2. SOME BASIC INTENSITY
TRANSFORMATION FUNCTIONS
3.2.1. IMAGE NEGATIVES
(a) A digital mammogram.
(b) Negative image 15
3.2. SOME BASIC INTENSITY
TRANSFORMATION FUNCTIONS
3.2.2. LOG TRANSFORMATIONS
The general form of the log transformation
s c log(1 r )
where c is a constant and it is assumed that r0
16
3.2. SOME BASIC INTENSITY
TRANSFORMATION FUNCTIONS
3.2.3. POWER-LAW (GAMMA) TRANSFORMATIONS
Power-law (gamma) transformations have a form
s cr
where c, γ are positive constants.
Sometimes written as s c r to account for offsets
(that is, a measurable output when the input is zero).
However, offsets typically are an issue of display
calibration, and as a result they are normally ignored.
17
3.2. SOME BASIC INTENSITY
TRANSFORMATION FUNCTIONS
3.2.3. POWER-LAW (GAMMA) TRANSFORMATIONS
Plots of the
gamma equation
for various values
of γ (c = 1 in all
cases). Each
curve was scaled
independently so
that all curves
would fit in the
same graph.
18
3.2. SOME BASIC INTENSITY
TRANSFORMATION FUNCTIONS
3.2.3. POWER-LAW (GAMMA) TRANSFORMATIONS
(a)Intensity ramp
image.
(b) Image
as viewed on a
simulated
monitor with a
gamma of 2.5.
(c) Gamma-
corrected image.
(d) Corrected
image as viewed
on the same
monitor.
19
3.2. SOME BASIC INTENSITY
TRANSFORMATION FUNCTIONS
3.2.3. POWER-LAW (GAMMA) TRANSFORMATIONS
20
3.2. SOME BASIC INTENSITY
TRANSFORMATION FUNCTIONS
3.2.3 POWER-LAW (GAMMA) TRANSFORMATIONS
21
3.2. SOME BASIC INTENSITY
TRANSFORMATION FUNCTIONS
3.2.4. PIECEWISE LINEAR TRANSFORMATION FUNCTIONS
1. Contrast Stretching
Low-contrast images can result from poor illumination,
lack of dynamic range in the imaging sensor, or even
the wrong setting of a lens aperture during image
acquisition.
Contrast stretching expands the range of intensity
levels in an image so that it spans the ideal full intensity
range of the recording medium or display device.
22
3.2. SOME BASIC INTENSITY
TRANSFORMATION FUNCTIONS
1. Contrast Stretching
23
cho a =giá trị nhỏ thứ nhì,
b=giá trị lớn nhất thứ 2,
=0.5, β=5, =1
24
3.2. SOME BASIC INTENSITY
TRANSFORMATION FUNCTIONS
3.2.4. PIECEWISE LINEAR TRANSFORMATION FUNCTIONS
2. Intensity-Level Slicing
Intensity-level slicing to highlight a specific range of
intensities in an image.
Its applications include enhancing features in satellite
imagery, such as masses of water, and enhancing flaws
in X-ray images.
It can be implemented in several ways, but most are
variations of two basic themes. One approach is to
display in one value (say, white) all the values in the
range of interest and in another (say, black) all other
intensities.
25
3.2. SOME BASIC INTENSITY
TRANSFORMATION FUNCTIONS
2. Intensity-Level Slicing
26
3.2. SOME BASIC INTENSITY
TRANSFORMATION FUNCTIONS
2. Intensity-Level Slicing
27
3.2. SOME BASIC INTENSITY
TRANSFORMATION FUNCTIONS
3.2.4. PIECEWISE LINEAR TRANSFORMATION FUNCTIONS
3. Bit-plane slicing
Bit-plane slicing:
It can highlight the contribution made to total image
appearance by specific bits.
Each pixel in an image represented by 8 bits.
Image is composed of eight 1-bit planes, ranging
from bit-plane 0 for the least significant bit to bit
plane 7 for the most significant bit.
28
Chapter 3
3.3 HISTOGRAM
PROCESSING
29
3.3. HISTOGRAM PROCESSING
3.3.1. HISTOGRAM
Let rk, for k = 0, 1, 2,…, L-1 denote the intensities of an
L-level digital image, f(x, y). The unnormalized
histogram of f is defined as
h(rk ) nk (k 1, 2,..., L 1)
Where nk is the number of pixel in f with intensity rk. and the
subdivisions of the intensity scale are called histogram bins.
The normalized histogram (histograms or image
histograms) of f is defined as
h(rk ) nk
p (rk )
MN MN
Where M, N are the number of image rows and columns,
respectively.
30
3.3. HISTOGRAM PROCESSING
Histogram
The sum of p(rk) for all values of k is always 1.
The components of p(rk) are estimates of the
probabilities of intensity levels occurring in an image.
Histogram manipulation is a fundamental tool in image
processing. Histograms are simple to compute and are
also suitable for fast hardware implementations, thus
making histogram-based techniques a popular tool for
real-time image processing.
Histogram shape is related to image appearance.
31
3.3. HISTOGRAM PROCESSING
Histogram
32
3.3. HISTOGRAM PROCESSING
3.3.2. HISTOGRAM EQUALIZATION
Histogram equalization:
To improve the contrast of an image
To transform an image in such a way that the transformed image
has a nearly uniform distribution of pixel values
Transformation:
Assume r has been normalized to the interval [0,1], with r = 0
representing black and r = 1 representing white
s T (r ) 0 r 1
The transformation function satisfies the following conditions:
T(r) is single-valued and monotonically increasing in the interval
0 T (r ) 1 for 0 r 1
33
3.3. HISTOGRAM PROCESSING
3.3.2. HISTOGRAM EQUALIZATION
34
3.3. HISTOGRAM PROCESSING
3.3.2. HISTOGRAM EQUALIZATION
Histogram equalization is based on a transformation of the
probability density function of a random variable.
Let pr(r) and ps(s) denote the probability density function of
random variable r and s, respectively.
If pr(r) and T(r) are known, then the probability density function
ps(s) of the transformed variable s can be obtained
dr
ps ( s ) pr ( r )
ds
r
Define a transformation function s T (r ) pr ( w)dw
0
where w is a dummy variable of integration
and the right side of this equation is the cumulative distribution
function of random variable r.
35
3.3. HISTOGRAM PROCESSING
3.3.2. HISTOGRAM EQUALIZATION
r
Given transformation function T(r), T (r ) pr ( w)dw
0
dr dT (r ) d r p (r )
dr 0
p ( w) dw
ds dr
r r
dr 1
p s ( s ) pr ( r ) pr ( r ) 1 0 s 1
ds pr ( r )
ps(s) now is a uniform probability density function.
T(r) depends on pr(r), but the resulting ps(s) always is
uniform.
36
3.3. HISTOGRAM PROCESSING
3.3.2. HISTOGRAM EQUALIZATION
For discrete values:
The probability of occurrence of gray level rk in an image is
nk
pr (r ) k 0,1, 2,..., L 1
MN
MN : the total number of pixels in the image
nk : the number of pixels that have gray level rk
L : the total number of possible gray levels in the image
The transformation function is
k k nj
sk T (rk ) pr ( rj ) k 0,1,2,..., L 1
j 0 j 0 n
Thus, an output image is obtained by mapping each pixel with
level rk in the input image into a corresponding pixel with level sk.
37
3.3. HISTOGRAM PROCESSING
3.3.2. HISTOGRAM EQUALIZATION
38
3.3. HISTOGRAM PROCESSING
3.3.2. HISTOGRAM EQUALIZATION
39
3.3. HISTOGRAM PROCESSING
3.3.2. HISTOGRAM EQUALIZATION
Transformation functions for histogram equalization.
40
3.3. HISTOGRAM PROCESSING
3.3.3. HISTOGRAM MATCHING (SPECIFICATION)
Histogram equalization produces a transformation function
that seeks to generate an output image with a uniform
histogram.
The method used to generate images that have a specified
histogram is called histogram matching or histogram
specification.
Histogram matching or histogram specification is the
transformation of an image so that its histogram matches a
specified histogram.
Histogram equalization method is a special case in which
the specified histogram is uniformly distributed.
41
3.3. HISTOGRAM PROCESSING
3.3.4. LOCAL HISTOGRAM PROCESSING
The histogram processing methods discussed above are
global, in the sense that pixels are modified by a
transformation function based on the gray-level content of
an entire image.
However, there are cases in which it is necessary to
enhance details over small areas in an image.
42
Chapter 3
3.4 MAGE ENHANCEMENT
USING SPATIAL OPERATIONS
43
3.4. IMAGE ENHANCEMENT USING SPATIAL
OPERATIONS
3.4.1. FUNDAMENTALS OF SPATIAL FILTERING
Spatial filtering modifies an image by replacing the value
of each pixel by a function of the values of the pixel and
its neighbors.
If the operation performed on the image pixels is linear,
then the filter is called a linear spatial filter (such as mean
(average), Gaussian filters). Otherwise, the filter is a
nonlinear spatial filter.(such as median, max, min,
morphological filters).
Spatial filtering is widely used in various applications such
as noise reduction, edge detection, and image
enhancement.
44
3.4. IMAGE ENHANCEMENT USING SPATIAL
OPERATIONS
1. LINEAR SPATIAL FILTERING
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 mask, template, and window
In general, linear filtering of an image f of size M x N with a filter
mask of size m x n is given by
a b
g ( x, y ) w(s, t ) f ( x s, y t )
s at b
where a=(m-1)/2 and b=(n-1)/2
45
3.4. IMAGE ENHANCEMENT USING SPATIAL
OPERATIONS
2. SPATIAL CORRELATION
The correlation of a kernel w of size m x n with an image
f(x, y) of size M x N , denoted as
Correlation consists of moving the center of a kernel over
an image, and computing the sum of products at each
location.
46
3.4. IMAGE ENHANCEMENT USING SPATIAL
OPERATIONS
3. SPATIAL CONVOLUTION
The mechanics of spatial convolution are the same as
spatial correlation, except that the correlation kernel is
rotated by 180°.
When the values of a kernel are symmetric about its
center, correlation and convolution yield the same result.
47
3.4. IMAGE ENHANCEMENT USING SPATIAL
OPERATIONS
Mask 3x3
48
3.4. IMAGE ENHANCEMENT USING SPATIAL
OPERATIONS
In cases part of w lies outside f, so the summation is undefined
in that area. There are a few approaches to dealing with
missing edge pixels:
Omit missing pixels
• Only works with some filters
• Can add extra code and slow down processing
Pad the image
• Typically with either all white or all black pixels
Replicate border pixels
Truncate the image
Allow pixels wrap around the image
• Can cause some strange image artefacts
49
3.4. IMAGE ENHANCEMENT USING SPATIAL
OPERATIONS
At the edges of an image, which missing
pixels to form a neighborhood.
50
3.4. IMAGE ENHANCEMENT USING SPATIAL
OPERATIONS
51
3.4. IMAGE ENHANCEMENT USING SPATIAL
OPERATIONS
3. EXAMPLES OF SPATIAL CORRELATION AND CONVOLUTION
52
3.4. IMAGE ENHANCEMENT USING SPATIAL
OPERATIONS
3. EXAMPLES OF SPATIAL CORRELATION AND CONVOLUTION
53
3.4. IMAGE ENHANCEMENT USING SPATIAL
OPERATIONS
3.4.2. SMOOTHING (LOWPASS) SPATIAL FILTERS (IMAGE AVERAGING)
Smoothing spatial filters is a technique used in image
processing and computer vision to smooth images or
reduce noise.
Smoothing is used to reduce irrelevant detail in an image
or smoothing the false contours that result from using an
insufficient number of intensity levels in an image.
The Smoothing spatial filters technique can be used to
smooth the input image before applying other image
processing steps such as edge detection, segmentation,
and object recognition. It can also be used to reduce
noise in digital images or videos.
54
3.4. IMAGE ENHANCEMENT USING SPATIAL
OPERATIONS
3.4.2. SMOOTHING (LOWPASS) SPATIAL FILTERS (IMAGE AVERAGING)
Linear smoothing filters consists of convolving an image
with a filter kernel. Convolving a smoothing kernel with an
image blurs the image, with the degree of blurring being
determined by the size of the kernel and the values of its
coefficients.
Depending on the complexity and purpose of use, there
are many different types of filters that can be used in this
technique, such as simple average filter (box filter),
Gaussian filter, median filter, and Bilateral filter."
55
3.4. IMAGE ENHANCEMENT USING SPATIAL
OPERATIONS
1. AVERAGE FILTER KENELS (BOX FILTER KENELS)
The simplest, separable lowpass filter kernel is the box kernel,
whose coefficients have the same value.
An m x n box filter is an m x n array of 1’s, with a normalizing
constant in front, whose value is 1 divided by the sum of the
values of the coefficients.
This normalization is applied to all lowpass kernels, has two
purposes:
First, the average value of an area of constant intensity
would equal that intensity in the filtered image.
Second, normalizing the kernel in this way prevents
introducing a bias during filtering; that is, the sum of the
pixels in the original and filtered images will be the same.
56
3.4. IMAGE ENHANCEMENT USING SPATIAL
OPERATIONS
1. AVERAGE FILTER KENELS (BOX FILTER KENELS)
57
3.4. IMAGE ENHANCEMENT USING SPATIAL
OPERATIONS
1. AVERAGE FILTER KENELS (BOX FILTER KENELS)
The above is repeated for every pixel in the original image
to generate the smoothed image. 58
3.4. IMAGE ENHANCEMENT USING SPATIAL
OPERATIONS
1. AVERAGE FILTER KENELS (BOX FILTER KENELS)
59
3.4. IMAGE ENHANCEMENT USING SPATIAL
OPERATIONS
1. AVERAGE FILTER KENELS (BOX FILTER KENELS)
More effective smoothing filters can be generated by
allowing different pixels in the neighborhood different
weights in the averaging function.
Pixels closer to the central pixel
are more important
Often referred to as a weighted
averaging
60
3.4. IMAGE ENHANCEMENT USING SPATIAL
OPERATIONS
2. GAUSSIAN FILTER KENELS.
Gaussian filter is a linear filter, which based on a
Gaussian function: s 2 t 2
w( s, t ) G ( s, t ) Ke 2 2
The Gaussian function is 3 x 3 Gaussian
a bell-shaped curve kernel.
61
3.4. IMAGE ENHANCEMENT USING SPATIAL
OPERATIONS
2. GAUSSIAN FILTER KENELS.
62
3.4. IMAGE ENHANCEMENT USING SPATIAL
OPERATIONS
Comparison of Gaussian and box filter smoothing characteristics.
63
3.4. IMAGE ENHANCEMENT USING SPATIAL
OPERATIONS
3. ORDER-STATISTIC (NONLINEAR) FILTERS
Order-statistic filters are nonlinear spatial filters whose
response is based on ordering (ranking) the pixels contained in
the region encompassed by the filter.
The best-known filter in this category is the median filter, which,
as its name implies, replaces the value of the center pixel by
the median of the intensity values in the neighborhood of that
pixel.
Median filters provide excellent noise reduction capabilities for
certain types of random noise, with considerably less blurring
than linear smoothing filters of similar size.
Median filters are particularly effective in the presence of
impulse noise.
64
3.4. IMAGE ENHANCEMENT USING SPATIAL
OPERATIONS
MEDIAN FILTERS
Median mask 3x3
65
3.4. IMAGE ENHANCEMENT USING SPATIAL
OPERATIONS
MEDIAN FILTERS
x x
x 77
1, 2, 3, 4, 77, 78, 79, 80, 81
SORT
1, 2, 3, 4, 77, 78, 79, 80, 81
66
MEDIAN
3.4. IMAGE ENHANCEMENT USING SPATIAL
OPERATIONS
MEDIAN FILTERS
x x
x 77 74
2, 3, 5, 77, 78, 79, 8, 80, 81, 74
SORT
2, 3, 5, 8, 74, 77, 78, 79, 80, 81
67
MEDIAN
3.4. IMAGE ENHANCEMENT USING SPATIAL
OPERATIONS
MAX, MIN FILTERS
x x
x
1, 2, 3, 4, 77, 78, 80, 81
SORT
1, 2, 3, 4, 77, 78, 80, 81
MIN MEDIAN MAX 68
3.4. IMAGE ENHANCEMENT USING SPATIAL
OPERATIONS
3.4.3. SHARPENING (HIGHPASS) SPATIAL FILTERS
Sharpening highlights transitions in intensity.
Applications of image sharpening include: electronic printing,
medical imaging, industrial inspection and autonomous
guidance in military systems.
The strength of the response of a derivative operator is
proportional to the magnitude of the intensity discontinuity at
the point at which the operator is applied. Thus, image
differentiation enhances edges and other discontinuities
(such as noise) and de-emphasizes areas with slowly
varying intensities.
69
3.4. IMAGE ENHANCEMENT USING SPATIAL
OPERATIONS
1. FOUNDATION OF SHARPENING SPATIAL FILTERS
First-order derivative of one-dimensional function:
f
f ( x 1) f ( x)
x
a. Must be zero in areas of constant intensity.
b. Must be nonzero at the onset of an intensity step or ramp.
c. Must be nonzero along intensity ramps
Second-order derivative of one-dimensional function:
2 f
f ( x 1) f ( x 1) 2 f ( x)
x 2
a. Must be zero in areas of constant intensity.
b. Must be nonzero at the onset and end of an intensity
step or ramp.
c. Must be zero along intensity ramps. 70
3.4. IMAGE ENHANCEMENT USING SPATIAL
OPERATIONS
71
3.4. IMAGE ENHANCEMENT USING SPATIAL
OPERATIONS
The 2nd derivative is more useful for image enhancement
than the 1st derivative
– Stronger response to fine detail
– Simpler implementation
72
3.4. IMAGE ENHANCEMENT USING SPATIAL
OPERATIONS
2. THE LAPLACIAN FILTERS
The Laplacian filter is
– Isotropic
– One of the simplest sharpening filters
It uses second-order derivative of two-dimensional continuous
functions Laplacian
2
2
The Laplacian is defined as follows: 2 f 2 f2
f
x y
2 f
In the x-direction f ( x 1, y ) f ( x 1, y ) 2 f ( x, y )
x 2
2 f
In the y-direction f ( x, y 1) f ( x, y 1) 2 f ( x, y )
y 2
2 f [ f ( x 1, y ) f ( x 1, y ) f ( x, y 1) f ( x, y 1)] 4 f ( x, y )
73
3.4. IMAGE ENHANCEMENT USING SPATIAL
OPERATIONS
The Laplacian filters can be used for sharpening
Laplacian kernel Kernel used to Two other Laplacian kernels.
implement an
extension
74
3.4. IMAGE ENHANCEMENT USING SPATIAL
OPERATIONS
Applying the Laplacian to an image we get a new image
that highlights edges and other discontinuities
75
3.4. IMAGE ENHANCEMENT USING SPATIAL
OPERATIONS
The result of a Laplacian filtering is not an enhanced image.
Subtract the Laplacian result from the original image to
generate our final sharpened enhanced image
f ( x, y ) 2 f if the center coefficient of the Laplacian mask is negative.
g ( x, y )
f ( x , y ) 2
f if the center coefficient of the Laplacian mask is positive.
76
3.4. IMAGE ENHANCEMENT USING SPATIAL
OPERATIONS
The entire enhancement can be combined into a single
filtering operation
g ( x, y ) f ( x , y ) 2 f
f ( x, y ) f ( x 1, y ) f ( x 1, y ) f ( x, y 1) f ( x, y 1) 4 f ( x, y )
5 f ( x, y ) f ( x 1, y ) f ( x 1, y ) f ( x, y 1) f ( x, y 1)
77
3.4. IMAGE ENHANCEMENT USING SPATIAL
OPERATIONS
This gives us a new filter which does the whole job for us in
one step
78
3.4. IMAGE ENHANCEMENT USING SPATIAL
OPERATIONS
There are lots of slightly different versions of the Laplacian
that can be used:
79