Image Analysis and computer Vision
CoSc-6112
Tekalign Tujo (Assit. Prof)
Email: tekalign@mwu.edu.et 1
Chapter #2 Image Enhancement
2
Topic Coverage
Image ➢ Enhancement in spatial domain
Enhancement ➢Grey level transformation
➢Smoothing and sharpening Spatial filters
➢Histogram processing
➢ Enhancement in frequency domain
➢Fourier transform
➢Smoothing and sharpening frequency
domain filtering
➢Homomorphism filtering
3
Why Enhancement?
Images may suffer from the following degradations:
Poor contrast due to poor illumination or finite sensitivity of the imaging
device
Electronic sensor noise or atmospheric disturbances leading to broadband noise
Aliasing effects due to inadequate sampling
Finite aperture effects or motion leading to spatial errors
There are various and simple algorithms for image enhancement based on
lookup tables
Contrast enhancement
Other algorithms also work with simple linear filtering methods:
Noise removal
Image enhancement can be done using 2 methods:
Spatial domain: Direct Manipulation of pixels in the image plane
Frequency domain: Modifying frequency spectrum of the image
4
Intensity Transformation
• Simple image operators can be classified as 'pointwise' or
'neighbourhood' (filtering) operators
where f(x, y) is the input image, g(x, y) is the processed image, and T is an
operator on f, defined over some neighborhood of (x, y).
• Histogram equalisation is a pointwise operation
• Most general filtering operations use neighbourhoods of pixels
5
…
• The spatial domain processes discussed here are denoted by the expression
g(x,y)=T[f(x,y)]
• Intensity transformation functions frequently are written in simplified form as
s=T(r)
• Where r denotes the intensity of f and s the intensity of g, both at any
corresponding point (x,y)
6
What is a Histogram?
In Statistics, Histogram is a graphical representation showing
a visual impression of the distribution of data.
An Image Histogram is a type of histogram that acts as a
graphical representation of the lightness/color distribution in a
digital image.
It plots the number of pixels for each value.
7
Histogram Processing
The histogram of a digital image with gray levels in the range [0, L-
1] is a discrete function h(rk) = nk, where rk is the kth gray level and
nk is the number of pixels in the image having gray level rk.
It is common practice to normalize a histogram by dividing each of
its values by the total number of pixels in the image, denoted by n.
Thus, a normalized histogram is given by p(rk) = nk / n, for k = 0,
1, …, L -1.
Thus, p(rk) gives an estimate of the probability of occurrence of
gray level rk.
Note that the sum of all components of a normalized histogram is
equal to 1.
8
Why Histogram?
Histograms are the basis for numerous spatial domain
processing techniques
Histogram manipulation can be used effectively for image
enhancement
Histograms can be used to provide useful image statistics
Information derived from histograms are quite useful in
other image processing applications, such as image
compression and segmentation.
9
Introductory Example of Histograms
As an introduction to the role of histogram processing in image
enhancement, consider the Figure shown on the next page in four basic
gray-level characteristics: dark, light, low contrast, and high contrast.
The horizontal axis of each histogram plot corresponds to gray level
values, rk.
The vertical axis corresponds to values of h(rk)=nk or p(rk)=nk/n if the
values are normalized.
Thus, as indicated previously, these histogram plots are simply plots of
h(rk)=nk versus rk or p(rk)=nk/n versus rk.
An image with low contrast has a histogram that will be narrow and will
be centered toward the middle of the gray scale
The components of the histogram in the high-contrast image cover a
broad range of the gray scale
10
Introductory Example of Histograms Cont.
11
Histogram in MATLAB
h = imhist (f, b)
Where f, is the input image, h is the histogram, b is number of bins
(tick marks) used in forming the histogram (b = 255 is the default)
A bin, is simply, a subdivision of the intensity scale.
If we are working with uint8 images and we let b = 2, then the
intensity scale is subdivided into two ranges: 0 – 127 and 128 – 255.
The resulting histograms will have two values:
h(1) equals to the number of pixels in the image with values in the interval [0,127], and
h(2) equal to the number of pixels with values in the interval [128 255].
We obtain the normalized histogram simply by using the expression.
p = imhist (f, b)/numel(f)
numel (f): is a MATLAB function that gives the number of
elements in array f (i.e. the number of pixels in an image)
12
Histogram transformation
Point operation T(rk) =sk
grey values:
rk sk
T
Properties of T:
keeps the original range of grey values
monoton increasing
13
Histogram equalization (HE)
• Transforms the intensity values so that the
histogram of the output image approximately
matches the flat (uniform) histogram
• As for the discrete case, the following formula
applies:
*(L-1)
• k = 0,1,2,...,L-1
• L: number of grey levels in image (e.g., 255)
• nj: number of times j-th grey level appears in
image
• n: total number of pixels in the image
14
Histogram equalization III
15
Histogram equalization IV
16
Histogram equalization V
cumulative histogram
17
Histogram equalization VI
18
Histogram equalization VIII
histogram can be taken also on a part of the image
19
Histogram Equalisation
20
Color Histogram
21
Bit Plane Slicing
• Bit-plane slicing : Pixels are digital numbers composed of
bits.
• For example, the intensity of each pixel in an 256-level
gray-scale image is composed of 8 bits (i.e., one byte).
• Instead of highlighting intensity-level ranges, we could
highlight the contribution made to total image appearance
by specific bits. 22
23
24
Grey level transformation
There are three basic gray
level transformation.
Linear
Logarithmic
Power – law
Linear transformation includes simple
identity and negative transformation.
In Identity transformation, the results is the
same input image and output image.
In negative transformation, each value of
the input image is subtracted from the L-
1(255 for 8 bit depth) and mapped onto the
output image as shown in the figure. 25
Image Negatives (p130)
Image negatives
s = L −1− r
intensity level s ( 0 ~ 255)
s is the pixel value of the output
image and r is the pixel value of
the input image.
Output image –
r= input intensity level
s= output intensity level
L-1 = maximum intensity level
= 256 -1 = 255
r=0 → s= (256-1)-0 =255
r=128 → s=(256-1)-128 =127
r=255 → s=(256-1)-255 = 0
Input image –
intensity level r ( 0 ~ 255)
26
…
Image negatives
s = L −1− r
27
Example: Image Negatives
Ex:
w w=686
h
Ex:
h=790
Small
lesion
28
Log Transformations (p131)
The general form of the C = 1.0
log transformation is
done as:
s = c * log(1+r)
Where c is constant, and it
is assumed that r0
s is the pixel value of the
output image and r is the C = 0.8
pixel value of the input
image.
29
Example: Log Transformations
30
q Original Hist Eq Original Filtered with C=0.2 Filtered with C=0.2
with C=0.4 Filtered with C=0.4 Filtered with C=0.6 Filtered with C=0.6
with C=0.8 Filtered with C=0.8 Filtered with C=1.0 Filtered with C=1.0
31
Power-Law (Gamma) Transformations
• Power-law transformations
have the basic form:
s = cr
❑ Where c and are
positive constant.
• Sometimes the above
Equation is written as
s = c ( r + )
32
= 0.5
= 1.0
= 5.0
33
Power-Law (Gamma) Transformations
void DibGammaCorrection(CDib& dib, float gamma)
{ register int i, j;
float invgamma = 1.f / gamma;
int w = dib.GetWidth();int h = dib.GetHeight();
BYTE** ptr = dib.GetPtr();
for( j = 0 ; j < h ; j++ )
for( i = 0 ; i < w ; i++ )
{ ptr[j][i] = (BYTE)limit(pow((ptr[j][i]/255.f), invgamma)*255 + 0.5f); }
}
s = cr
34
Example: Gamma Corrections - p134
s=r2.5
s=r0.5
s=r2.5
35
Example: Gamma Transformations
Cathode ray tube
(CRT) devices have an
s=r2.5 intensity-to-voltage
response that is a
power function, with
exponents varying
from approximately
s = r1/2.5 1.8 to 2.5
s=r0.4
s=r2.5
36
Example: Gamma Transformations
s=r0.6
(a) original
37
s=r0.4 s=r0.3
Example: Gamma Transformations
s=r3.0
s=r4.0
s=r5.0 38
Function imadjust()
Function imadjust is the basic IPT tool for intensity transformations of
gray scale images. It has the syntax:
g=imadjust(f, [low_in high_in], [low_out high_out], gamma)
Negative image, obtained using the command :
g1=imadjust(f,[0,1],[1,0]);
This process, which is the digital equivalent of obtaining a photographic
negative, is particularly useful for enhancing white or gray detail
embedded in a large, predominantly dark region.
The negative of an image can be obtained also with IPT function
imcomplement:
g=imcomplement(f)
Expanding the gray scale region between 0.5 and 0.75 to the full [0,1]
range: This type of processing is useful for highlighting an intensity band
of interest.
39
g2=imadjust(f,[0.5 0.75], [0 1]);
Image Filtering
Simple image operators can be classified as
'pointwise' or 'neighbourhood' (filtering)
operators
where f(x, y) is the input image, g(x, y) is the
processed image, and T is an operator on f, defined
over some neighborhood of (x, y).
Histogram equalisation is a pointwise operation
More general filtering operations use
neighbourhoods of pixels .
40
Spatial domain filtering
Some neighborhood operations work with:
The Values of The Image Pixels In The Neighborhood, And
The corresponding values of a subimage that has the same dimensions as
the neighborhood.
The subimage is called a filter (or mask, kernel, template, window).
The values in a filter subimage are referred to as coefficients, not pixels.
Operation:
modify the pixels in an image based on some function of the pixels in
their neighborhood.
Simplest: is linear filtering (replace each pixel by a linear combination
of its neighbors).
Linear spatial filtering is often referred to as “convolving an image with a
filter”.
41
Image Filtering
Input image Output image
(x,y) (x,y)
pointwise
transformation
Input image Output image
(x,y) (x,y)
neighbourhood
transformation 42
Image Filtering
The output g(x,y) can be a linear or non-linear function of the set of
input pixel grey levels {f(x-M,y-M)…f(x+M,y+M}.
Input image f(x,y) Output image g(x,y)
(x-1,y-1)
(x,y) (x,y)
(x+1,y+1)
43
Image Filtering
Examples of filters:
g( x , y ) = h1 f ( x − 1, y − 1 ) + h2 f ( x , y − 1 )
+ .....h9 f ( x + 1, y + 1 )
f ( x − 1, y − 1 ), f ( x , y − 1 )
g( x , y ) = median
..... f ( x + 1, y + 1 )
44
Linear filtering and convolution
Example
3x3 arithmetic mean of an input image (ignoring floating point byte
rounding)
Input image f(x,y) Output image g(x,y)
(x-1,y-1)
(x,y) (x,y)
(x+1,y+1)
45
Linear filtering and convolution
Convolution involves ‘overlap – multiply – add’ with
‘convolution mask’
1 1 1
9 9 9 1 1 1
1 1 1
H= = 1 / 91 1 1
9 9 9
1
1 1 1 1 1
9 9 9
46
Spatial domain filtering
3 3 3
3 3
? 3 What is the value of the center pixel?
3 3 3
3 4 3 What assumptions are you making to
infer the center value?
2 3
? 3
3 4 2
47
Linear filtering
g [m,n] f [m,n]
For a linear spatially invariant system
f [m, n ] = I g = h[m − k, n − l ]g [k, l ]
m=0 1 2 … k ,l
111 115 113 111 112 111 112 111 ? ? ? ? ? ? ? ?
135 138 137 139 145 146 149 147 ? -5 9 -9 21 -12 10 ?
163 168 188 196 206 202 206 207 ? -29 18 24 4 -7 5 ?
-1 2 -1
180 184 206 219 202 200 195 193 ? -50 40 142 -88 -34 10 ?
-1 2 -1
=
189 193 214 216 104 79 83 77 ? -41 41 264 -175 -71 0 ?
191 201 217 220 103 59 60 68 ? -24 37 349 -224 -120 -10 ?
-1 2 -1
195 205 216 222 113 68 69 83 ? -23 33 360 -217 -134 -23 ?
199 203 223 228 108 68 71 77
? ? ? ? ? ? ? ?
h[m,n]
g[m,n] f[m,n]
48
Spatial domain filtering
Be careful about indices, image borders and padding during
implementation.
zero fixed/clamp periodic/wrap reflected/mirror
Border padding examples.
49
Smoothing spatial filters
Often, an image is composed of
some underlying ideal structure, which we want to
detect and describe,
together with some random noise or artifact, which we
would like to remove.
Smoothing filters are used for blurring and for noise
reduction.
Linear smoothing filters are also called averaging filters.
50
Smoothing spatial filters
10 11 10 0 0 1 X X X X X X
9 10 11 1 0 1 X 10 X
I 10 9 10 0 2 1 O X X
11 10 9 10 9 11 X X
9 10 11 9 99 11 F X X
10 9 9 11 10 10 X X X X X X
1 1 1
1/9 1 1 1
1 1 1
1/9.(10x1 + 11x1 + 10x1 + 9x1 + 10x1 + 11x1 + 10x1 + 9x1 + 10x1) =
1/9.( 90) = 10
51
Smoothing spatial filters
10 11 10 0 0 1 X X X X X X
9 10 11 1 0 1 X X
I 10 9 10 0 2 1 O X X
11 10 9 10 9 11 X X
9 10 11 9 99 11 F X 20 X
10 9 9 11 10 10 X X X X X X
1 1 1
1/9 1 1 1
1 1 1
1/9.(10x1 + 9x1 + 11x1 + 9x1 + 99x1 + 11x1 + 11x1 + 10x1 + 10x1) =
1/9.( 180) = 20
52
Order-statistic filters
10 11 10 0 0 1 X X X X X X
9 10 11 1 0 1 X 10 X
I 10 9 10 0 2 1 O X X
11 10 9 10 9 11 X X
9 10 11 9 99 11 X X
10 9 9 11 10 10 X X X X X X
median
sort
10,11,10,9,10,11,10,9,10 9,9,10,10,10,10,10,11,11
53
Order-statistic filters
10 11 10 0 0 1 X X X X X X
9 10 11 1 0 1 X X
I 10 9 10 0 2 1 O X X
11 10 9 10 9 11 X X
9 10 11 9 99 11 X 10 X
10 9 9 11 10 10 X X X X X X
median
sort
10,9,11,9,99,11,11,10,10 9,9,10,10,10,11,11,11,99
54
Common 3x3 Filters
1 1 1 − 1 − 1 − 1
Low/High pass filter 1 − 1 9 − 1
1
1
1
9
1 1 1
− 1 − 1 − 1
1 2 1
Blur operator 1 2 1 2
13
1 2 1
1 2 1 −1 0 1
H/V Edge detector 0 − 2 0 2
0 0
− 1 − 2 − 1 − 1 0 1
55
Edge Detection Example
Vertical Edge Horizontal Combined
1 2 1
0 0 0
− 1 − 2 − 1 Combined
Vertical Edge Horizontal
−1
Vertical Edge Horizontal
0 1
− 2 0 2
− 1 0 1
56
Convolution Based Filtering
Original Filtered with 3x3 [1]
window 57
Filter image with motion feature
Original Motion Filtered
58
Smoothing spatial filters
Common types of noise:
Salt-and-pepper noise:
contains random
occurrences of black and
white pixels.
Impulse noise: contains
random occurrences of
white pixels.
Gaussian noise: variations in
intensity drawn from a
Gaussian normal
distribution.
59
60
Smoothing spatial filters
A Gaussian filter is a weighted
average that weighs pixels at
its center much more strongly
than its boundaries.
2D Gaussian filter
61
Smoothing spatial filters
If σ is small: smoothing
will have little effect.
If σ is larger: neighboring
pixels will have larger
weights resulting in
consensus of the
neighbors.
If σ is very large: details
will disappear along with
the noise.
62
Linear filtering and convolution
We can define the convolution operator mathematically
A 2D convolution of an image f(x,y) with a filter h(x,y)
is defined as
1 1
g( x , y ) = h( x' , y' ) f ( x − x' , y − y' )
x' = − 1 y ' = − 1
1 1 1
= f ( x − x' , y − y' )
9 x' = − 1 y' = − 1
63
Linear filtering and convolution
Example – convolution with a Gaussian filter kernel
σ determines the width of the filter and hence the amount of
smoothing
( x2 + y2 )
g( x , y ) = exp( − )
2 2
= g( x ) g ( y )
x2
g( x ) = exp( − 2 )
2
64
Linear filtering and convolution
Original Noisy
Filtered
Filtered
σ=3.0
σ=1.5
65
Enhancement in Frequency
Domain
66
Frequency Domain
Jean-Baptiste Fourier (1768)
Any function that periodically repeats itself can be expressed
as the sum of sines and/or cosines of different frequencies,
each multiplied by a different coefficient (Fourier Series).
Even functions that are not periodic (but whose area under the
curve is finite) can be expressed as the integral of sines and/or
cosines multiplied by a weighting function (Fourier
Transform).
Functions can be recovered by the inverse operation with no
loss of information
The term Fourier transform refers to both the frequency domain
representation and the mathematical operation that associates
the frequency domain representation to a function of time. 67
Spatial Vs Frequency Domains
Spatial domain
refers to planar region of intensity
values at time t
Frequency domain
think of each color plane as a sinusoidal
function of changing intensity
values
Refers to organizing pixels according to
their changing intensity (frequency)
Frequency Domain Filtering is used
when one can not find a straight
forward kernel in a spatial domain
filtering These are pixels of the image above according
to their changing intensity (frequency) 68
…
Image enhancement in the frequency domain is
straightforward.
Steps:
1. Compute the Fourier transform of the image to be
enhanced,
2. Multiply the result by a filter, and
3. Take the inverse transform to produce the enhanced
image.
69
…
The frequency domain
refers to the plane of the two
dimensional discrete Fourier
transform of an image.
The purpose of the Fourier
transform is to represent a
signal as a linear combination
of sinusoidal signals of
various frequencies.
70
…
How can we analyze what a given filter does to high,
medium, and low frequencies?
The answer is to simply pass a sinusoid of known frequency
through the filter and observe by how much it is attenuated.
A sine wave or sinusoid is a mathematical curve that
describes a smooth repetitive oscillation.
It occurs often in pure and applied mathematics, as well as
physics, engineering, signal processing and many other
fields.
71
Fourier Transform and the Frequency Domain
Continuous Case
The one-dimensional Fourier transform and its inverse
Fourier transform (continuous case)
F (u ) = f ( x)e − j 2ux dx where j = − 1
−
Inverse Fourier transform: e j = cos + j sin
f ( x) = F (u )e j 2ux du
−
The two-dimensional Fourier transform and its inverse
Fourier transform (continuous case)
F (u, v) = f ( x, y )e − j 2 (ux+ vy ) dxdy
− −
Inverse Fourier transform:
f ( x, y ) = F (u, v)e j 2 (ux+ vy ) dudv
− −
72
Fourier Transform and the Frequency Domain
Discrete Case
The one-dimensional Fourier transform and its inverse
Fourier transform (discrete case)
M −1
1
F (u ) =
M
f ( x )e
x =0
− j 2ux / M
for u = 0,1,2,..., M − 1
Inverse Fourier transform:
M −1
f ( x) = F (u )e j 2ux / M
for x = 0,1,2,..., M − 1
u =0
73
Fourier Transform and the Frequency Domain
Since e j = cos + j sin and the fact cos(− ) = cos
then discrete Fourier transform can be redefined
M −1
1
F (u ) =
M
f ( x)[cos 2ux / M − j sin 2ux / M ]
x =0
for u = 0,1,2,..., M − 1
Frequency (time) domain: the domain (values of u) over which the
values of F(u) range; because u determines the frequency of the
components of the transform.
Frequency (time) component: each of the M terms of F(u).
74
Fourier transform of two images
75
Basics of Filtering in the Frequency Domain
76
Linear filtering and convolution
log( 1 + F ( u , v ) )
77
…
F(u,v) is the frequency content of the image at spatial frequency
position (u,v)
Smooth regions of the image contribute low frequency
components to F(u,v)
Abrupt transitions in grey level (lines and edges) contribute high
frequency components to F(u,v)
We can compute the DFT (Discrete Fourier Transform) directly
using the formula
2
An N point DFT would require N floating point multiplications per
output point
2
Since there are N output points , the computational complexity of
the DFT is N4
4 9
N =4x10 for N=256
78
…
Input image f(x,y) Output image g(x,y)
(x,y) (x,y)
Filter mask h(x,y)
79
…
Note that the filter mask is shifted and inverted prior to the
‘overlap, multiply and add’ stage of the convolution
Define the DFT’s of f(x,y),h(x,y), and g(x,y) as F(u,v),H(u,v) and
G(u,v)
The convolution theorem states simply that :
G( u , v ) = H ( u , v ) F ( u , v )
80
…
As an example, suppose h(x,y) corresponds to a linear filter
with frequency response defined as follows:
H (u , v) = 0 for u 2 + v 2 R
= 1 otherwise
Removes low frequency components of the image
81
…
DFT
IDFT
82
Filter mask h(x,y)
Linear zero padding Input image f(x,y)
filtering and
x x
x x
convolution
x x
x x x x x
x x x x x
DFT DFT
H(u,v) F(u,v)
f(x,y) * h(x,y)
H(u,v)F(u,v)
IDFT
83
…
Input image f(x,y) Output image g(x,y)
(x,y)
(x',y')
x' = x modulo N
Filter mask h(x,y)
y' = y modulo N
84
…
Example 1, N=512, M=7
Spatial domain implementation requires 1.3 x 107 floating point
multiplications
Frequency domain implementation requires 1.4 x 107 floating
point multiplications
Example 2, N=512, M=32
Spatial domain implementation requires 2.7 x 108 floating point
multiplications
Frequency domain implementation requires 1.4 x 107 floating
point multiplications
85
…
For smaller mask sizes, spatial and frequency domain
implementations have about the same computational
complexity
However, we can speed up frequency domain interpretations
by tessellating the image into sub-blocks and filtering these
independently
Not quite that simple – we need to overlap the filtered sub-blocks
to remove blocking artefacts
Overlap and add algorithm
MatLab uses a Fast Fourier Transform computation called FFT
and FFT2 for 2 D images
86
…
We can look at some examples of linear filters commonly used in
image processing and their frequency responses
In particular we will look at a smoothing filter and a filter to perform
edge detection
h( x ) H( u )
x u
Spatial domain Spatial frequency domain
87
Conclusion
We have looked at basic (low level) image
processing operations
Enhancement
Filtering
These are usually important pre-processing steps
carried out in computer vision systems
88
Frequency Domain Filtering Implimentation in Matlab
1) import the image
2) check the size to see the dimension so that to set the size of the Gaussian filter
3) create the Gaussian filter based on the dimension
gu_f=fspecial('gaussian', 256,10);
# 256 by 512: dimension 10: standard deviation (sigma)
4)Check the maximum value in the Gaussian filter
max(gu_f(:)) if the value is too small, scale it so that the maximum value is 1
5) Scale the Gaussian filter
gu_f1=mat2gray(gu_f); now the max value will be 1 >>max(gu_f1(:))
6) Translate the image into a Fourier domain
img=imread(‘pout.tif ’);
imgf=fftshift(fft2(img));
7) multiply the image in a Fourier domain with the Gaussian filter (pixel by pixel) This is the
transformed filtered image
img_guf1=imgf.*gu_f1
8) show the fft (frequency Fourier transformed)
imshow(img_guf1)
9) make the inverse of the frequency Fourier transform
img_gufi=ifft2(img_guf1)
10) Show the filtered image 89
imshow(img_gufi)
Thank you
90