You are on page 1of 117

IMAGE ENHANCEMENT IN

SPATIAL DOMAIN
M Lokanath

Over the next few lectures we will


look at image enhancement
techniques working in the spatial
domain:

What is image enhancement?


Different kinds of image enhancement
Histogram processing
Point processing

What Is Image Enhancement?


Image enhancement is the process of making images
more useful
The reasons for doing this include:
Highlighting

interesting detail in images


Removing noise from images
Making images more visually appealing

Image Enhancement Examples

Image Enhancement Examples (cont)

Image Enhancement Examples (cont)

Spatial & Frequency Domains


There are two broad categories of image
enhancement techniques
Spatial
Direct

domain techniques
manipulation of image pixels

Frequency

domain techniques

Manipulation

of Fourier transform or wavelet transform of

an image

For the moment we will concentrate on techniques that


operate in the spatial domain

In Spatial domain

Spatial domain processes will be denoted by the expression


g(x,y)=T[f(x,y)]

Where f(x,y) is the input image, g(x,y) is the processed image and T is an
operator on f.

Single pixel operations

The simplest form of T is when the neighborhood is of size 1x1


s=T(r)

Neighbor hood operations

Some basic level transformations

Image negatives

Negative image

Log transformations

Maps narrow range of low gray level values in the


input image into a wider range of output levels. The
opposite is true of higher values of input levels

Power law transformations

Gamma correction in devices

Gamma correction in devices

Low contrast images

Low contrast images are formed due to


Poor illumination
Lack of dynamic range in the image sensor
Wrong setting of lens aperture during image acquisition

Contrast stretching

To increase the dynamic range of the gray levels

Assignment 1

Write a MATLAB function that creates a synthetic image


based on the equation
f(x,y)=Asin (uox+ voy)

MATLAB does not have a function to determine which


elements of an array are integers (i.e., .2,1,0, -1,-2...).
Write a function for this purpose,
(Use of while or for loops is not allowed. Note: Integer and
double-precision arrays with real or complex values are
numeric, while strings, cell arrays, and structure arrays are
not. Hints: Become familiar with function floor. If you
include the capability to handle complex numbers, become
familiar with functions real and imag.)

Assignment 2
Write an M-function with the following specifications:

function H = imcircle(R, M, N)
%IMCIRCLE Generates a circle inside a rectangle.
% H = IMCIRCLE(R, M, N) generates a circle of radius R centered
% on a rectangle of height M and width N. H is a binary image with
% 1s on the circle and 0s elsewhere. R must be an integer >= 1.
Your program must check the validity of R and also it should check to
make sure that the specified circle fits in the given rectangle
dimensions. Use of for or while loops is not permitted.
Hint: Review function meshgrid and become familiar with function floor.

Assignment 2
Show the negative of these images

Some useful Information on MATLAB

Matlab functions

Understand the following functions in matlab


meshgird()
imadjust()
imcomplement()
nargin ,nargout, nargchk
imhist()

Assignment
first solution
Written a function for generating
256x256 synthetic image

twodsin1.m
function f = twodsin1(A, uo, vo, M, N)

f=twodsin1(1, 1/(1*pi), 1/(1*pi), 256, 256);


imshow(f,[ ])

f=zeros(M, N);
for c =1:N
voy=vo *(c-1);
for r= 1:M
uox=uo *(r-1);
f(r,c)= A*sin(uox+voy);
end

end

Assignment
second solutions
A=[21 23 3i 4j 5+2i -12 0.45 -2.4 -4-3i ]
if ~isnumeric(A)
error('A must be a numeric array.');
end

% A must be one of the integer types, so we don't have


to convert

if isa(A, 'double')
I = isfinite(A) & (imag(A) == 0) & (A ==
floor(A));

I = imag(A) == 0;
end
end

elseif isa(A, 'single')


A = double(A);
I = isfinite(A) & (imag(A) == 0) & (A ==
floor(A));

else

% to double.
if isreal(A)
I = true(size(A));
else

% I will show the exact position of integers for the given


Array 'A'

Gray level slicing

Highlighting a specific range of gray levels in an


image often is desired
Similar

to thresholding
Other levels can be
suppressed or maintained
Useful for highlighting
features in an image,
Shape, to detect blockages.

Bit plane slicing


Often by isolating particular bits of the pixel values
in an image we can highlight interesting aspects of
that image

Higher-order

bits usually contain most of the significant


visual information
Lower-order bits contain
subtle details

Gray scale image

Bit Plane Slicing (cont)


[10000000]

[01000000]

[00100000]

[00001000]

[00000100]

[00000001]

Image Histograms

Frequencies

The histogram of an image shows us the distribution of


grey levels in the image
Massively useful in image processing, especially in
segmentation, enhancement and compression

Grey Levels

Histogram Examples (cont)


A selection of images and
their histograms
Notice the relationships
between the images and
their histograms
Note that the high contrast
image has the most
evenly spaced histogram

Dark image

Bright image

Low contrast image

High contrast image

Implementation of histogram
Histogram

equalization (linearization)

Equalisation Transformation Function

Histogram Equalisation
Spreading out the frequencies in an image (or
equalising the image) is a simple way to improve dark
or washed out images
sk T (rk )
The formula for histogram
equalisation is given where
k
rk:

input intensity
sk: processed intensity
k:
the intensity range
(e.g 0.0 1.0)
nj: the frequency of intensity j
MN: the sum of all frequencies

( L 1) pr (rj )
j 0
k

( L 1)
j 0

nj
MN

ASSIGNMENT 3

Question 1
For this image find
Histogram
Histogram

equalization of image
Histogram of processed image
Plot the transfer function.

Specified histogram

Question 2
Find for the given input image
Histogram
Histogram

equalization
Processed image histogram
Histogram specification
Specified image histogram

Specified histogram function.


function p= twomodegauss(m1, sig1, m2, sig2, A1, A2, k)
c1=A1*(1/((2*pi)^0.5)*sig1);
k1=2*(sig1^2);
c2=A2*(1/((2*pi)^0.5)*sig2);
k2=2*(sig2^2);
z=linspace(0,1,256);
p = k + c1*exp(-((z-m1).^2)./k1) + c2*exp(-((z-m2).^2)./k2);
p=p./sum(p(:));
Take m1=0.15, sig1=0.05, m2=0.75, sig2=0.05
A1=1, A2=0.07, k=0.002

Assignment 2
first solution
function H = imcircle(R, M, N)
IMCIRCLE Generates a circle inside a rectangle.
H = IMCIRCLE(R, M, N) generates a circle of radius R centered
on a rectangle of height M and width N. H is a binary image with 1s on
the circle and 0s elsewhere. R must be an integer >= 1.

Compute a matrix, A, of distances from any point in the


rectangle to its center.
x = 0:M - 1;
y = 0:N - 1;

The values of the coordinates of the rectangle are x = 0, 1 , 2,

[Y, X] = meshgrid(y, x);

. . ., M - 1 and y = 0, 1, 2, . . ., N - 1, so the center is at

A = sqrt((X - X0).^2 + (Y - Y0).^2);

coordinates (floor(M/2), floor(N/2)). Check to make sure that the

circle will fit in the rectangle.

X0 = floor(M/2);
Y0 = floor(N/2);
if R > M - X0 - 1 | R > N - Y0 - 1
error('Circle does not fit in rectangle.')

Find all the distances in the range


(R - L/2) >= A(I,J) <= (R + L/2), with L = sqrt(2). These are
the points comprising the circumference of the circle
within a tolerance of sqrt(2). This tolerance is chosen
based on the fact that the coordinates are separated
by 1 in the horizontal and vertical directions and by
sqrt(2) in the diagonal direction.

end

H = ((R - sqrt(2)/2) <= A) & (A <= (R + sqrt(2)/2));

if (floor(R) ~= R) | (R < 1)

Imshow(H);

error('The radius R must be an integer >= 1.')


end

Histogram matching(specification)

We keep target histogram and we do process on


the input image to reach the target Histogram

Histogram matching may be summarized as follows:

Obtain histogram for given input image


Precompute a mapped level sk from each level

sk T (rk )
k

nj

j 0

Obtain transfer function G from given

rk

Pz (z) using

Vk G ( z k ) Pz ( zi ) sk
i 0

Precompute z k for each value of sk using iterative scheme


For each pixel in original image, if the value of the pixel is rk , map
this to its corresponding level sk , then map sk into the final z k using
the precomputed values

LOGIC OPERATIONS

IMAGE SUBTACTION

IMAGE SUBTRACTION IN MEDICAL


IMAGE PROCESSING

IMAGE AVERAGING

SPATIAL FILTERING

Spatial filtering
Basics

of spatial filtering
Smoothing spatial filters
Smoothing

linear filters
Order statistics filters

Median filter(non linear)

Sharpening

spatial filters

BASICS OF SPATIAL FILTERING

Strange Things Happen At The Edges!


At the edges of an image we are missing pixels
to form a neighbourhood
Origin

e
e

Image f (x, y)

Strange Things Happen At The Edges! (cont)


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

Smoothing Spatial Filters


One of the simplest spatial filtering operations we
can perform is a smoothing operation

Simply

average all of the pixels in a neighbourhood


around a central value
Especially useful
in removing noise
1/
1/
1/
from images
9
9
9
Simple
Also useful for
1/
1/
1/
averaging
highlighting gross
9
9
9
detail
filter
1/

1/

1/

Weighted Smoothing Filters


More effective smoothing filters can be generated
by allowing different pixels in the neighbourhood
different weights in the averaging function

Pixels

closer to the
central pixel are more
important
Often referred to as a
weighted averaging

1/

16

2/

16

1/

16

2/

16

4/

16

2/

16

1/

16

2/

16

1/

16

Weighted
averaging filter

Another Smoothing Example


By smoothing the original image we get rid of lots of
the finer detail which leaves only the gross features
for thresholding

Original Image

Smoothed Image

Thresholded Image

ORDER STATISTICS FILTER

Non linear filter


Widely

used is median filter


The response is based on ordering (ranking) the pixels
contained in the image area encompassed by the filter
and then replacing the value of the center pixel with
the value determined by the ranking result.

Median filter
Example

Averaging Filter Vs. Median Filter Example

Original Image
With Noise

Image After
Averaging Filter

Image After
Median Filter

Filtering is often used to remove noise from images


Sometimes a median filter works better than an
averaging filter

Sharpening Spatial filters


Previously we have looked at smoothing filters which
remove fine detail
Sharpening spatial filters seek to highlight fine detail

Remove

blurring from images


Highlight edges

Sharpening filters are based on spatial


differentiation

Sharpening
1st

filters

derivative filters
2nd derivative filters

Spatial Differentiation
Differentiation measures the rate of change of a
function
Lets consider a simple 1 dimensional example

Spatial Differentiation

st
1

Derivative

The formula for the 1st derivative of a function is as


follows:

f
f ( x 1) f ( x)
x

Its just the difference between subsequent values and


measures the rate of change of the function

st
1

Derivative (cont)
Image Strip
8
7
6
5
4
3
2
1
0

5 5 4 3 2 1 0 0 0 6 0 0 0 0 1 3 1 0 0 0 0 7 7 7 7
1st Derivative

0 -1 -1 -1 -1 0 0 6 -6 0 0 0 1 2 -2 -1 0 0 0 7 0 0 0
8
6
4
2
0
-2
-4
-6
-8

nd
2

Derivative

The formula for the 2nd derivative of a function is as


follows: 2

f
f ( x 1) f ( x 1) 2 f ( x)
2
x

Simply takes into account the values both before and


after the current value

nd
2

Derivative (cont)
Image Strip

8
7
6
5
4
3
2
1
0

5 5 4 3 2 1 0 0 0 6 0 0 0 0 1 3 1 0 0 0 0 7 7 7 7

-1 0 0 0 0 1 0 6
10
5
0
-5
-10
-15

-12 6
0 1
2nd0
Derivative

1 -4 1 1 0 0 7 -7 0 0

Using Second Derivatives For Image Enhancement


The 2nd derivative is more useful for image
enhancement than the 1st derivative
Stronger

response to fine detail


Simpler implementation

The first sharpening filter we will look at is the


Laplacian
Isotropic
One

of the simplest sharpening filters


We will look at a digital implementation

The Laplacian
The Laplacian is defined as follows:

f f
f 2 2
x
y
st
2

where the partial 1 order derivative in the x direction


is defined as follows:

f
f ( x 1, y) f ( x 1, y) 2 f ( x, y)
2
x
2

and in the y direction as follows:

f
f ( x, y 1) f ( x, y 1) 2 f ( x, y)
2
y
2

The Laplacian (cont)


So, the Laplacian can be given as follows:

f [ f ( x 1, y) f ( x 1, y)
f ( x, y 1) f ( x, y 1)]
4 f ( x, y )
2

We can easily build a filter based on this


0

-4

The Laplacian (cont)


Applying the Laplacian to an image we get a new
image that highlights edges and other discontinuities

Original
Image

Laplacian
Filtered Image

Laplacian
Filtered Image
Scaled for Display

But That Is Not Very Enhanced!


The result of a Laplacian filtering is not
an enhanced image
We have to do more work in order to
get our final image
Subtract the Laplacian result from the
original image to generate our final
sharpened enhanced image

g ( x, y) f ( x, y ) c[ f ( x, y)]
2

C=-1 for centre coefficient negative


C=+1 for centre coefficient positive

Laplacian
Filtered Image
Scaled for Display

Laplacian Image Enhancement

Original
Image

=
Laplacian
Filtered Image

Sharpened
Image

In the final sharpened image edges and fine detail


are much more obvious

Laplacian Image Enhancement

Simplified Image Enhancement


The entire enhancement can be combined into a single
filtering operation

g ( x, y) f ( x, y) 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)
2

Simplified Image Enhancement (cont)


This gives us a new filter which does the whole job for
us in one step
0

-1

-1

-1

-1

Simplified Image Enhancement (cont)

Variants On The Simple Laplacian

-1

-1

-1

-1

-1

-1

-1

-1

Unsharp masking and high boost filtering

Gradient

Combining spatial enhancement


methods

Application of Fuzzy sets

Fuzzy sets for intensity transformations

IF a pixel is dark, THEN make it darker


IF a pixel is gray, THEN make it gray
IF a pixel is bright , THEN make it brighter

Using fuzzy sets for spatial filtering

Assignment 4.
problem 1. Local Histogram Equalization
The global histogram equalization technique is easily adaptable to local histogram equalization. The procedure is to
define a square or rectangular window (neighborhood) and move the center of the window from pixel to pixel. At each
location, the histogram of the points inside the window is computed and a histogram equalization transformation function
is obtained. This function is finally used to map the intensity level of the pixel centered in the neighborhood to create a
corresponding (processed) pixel in the output image. The center of the neighborhood region is then moved to an
adjacent pixel location and the procedure is repeated. Since only one new row or column of the neighborhood changes
during a pixel-to-pixel translation of the region, updating the histogram obtained in the previous location with the new
data introduced at each motion step is possible. This approach has obvious advantages over repeatedly computing the
histogram over all pixels in the neighborhood region each time the region is moved one pixel location.
Write an M-function for performing local histogram equalization. Your function should have the following specifications.
function g = localhisteq(f, m, n)
LOCALHISTEQ Local histogram equalization.
G = LOCALHISTEQ(F, M, N) performs local histogram equalization on input image F using a window of (odd) size M-by-N to
produce the processed image, G. To handle border effects, image F is extended by using the symmetric option in
function padarray. The amount of extension is determined by the dimensions of the local window. If M and N are
omitted, they default to 3. If N is omitted, it defaults to M. Both must be odd.
This function accepts input images of class uint8, uint16, or double. However, all computations are done using 8-bit
intensity values to speed-up computations. If F is of class double its values should be in the range [0 1]. The class of the
output image is the same as the class of the input.
Hint: Your code will be simplified if you use functions cumsum and tofloat. Keep in mind that only the intensity level of the
center of the neighborhood needs to be mapped at each location of the window.

Image for local histogram

Assignment 4 problem 2:
Experimenting with Larger Laplacian Masks
It is shown in class that the Laplacian mask w8 = [ 1, 1, 1; 1 8 1; 1, 1, 1]
yields a result sharper than the result with a similar mask with a 4 in the
center.
(a) Write an M-function that generates a Laplacian mask of arbitrary odd
size. Example, the mask of size 5 5 would consist of all 1s with a 24 in
the centre location.
(b) For the given image blurry_moon.tif. Compare the results obtained with
masks of size n x n for n = 3, 5, 9, 15, and 25
(c) Explain the differences in the resulting images.

Next class

Image enhancement in the frequency domain