Spatial Image Enhancement
Sankalp Kallakuri
elsanky@gmail.com
Spatial Image Enhancement
. Images are usually represented as matrices
P(0,0) P(0,1) P(0,2) . .. …….. P(0,M-1)
P(1,0) P(1,1) P(1,2) ………………. P(1,M-1)
P (nxm) .. ..
=
.. ..
P(N-2,0) P(N-2,1) P(N-2,2)……………P(N-2,M-1)
P(N-1,0) P(N-1,1) P(N-1,2)……………P(N-1,M-1)
Spatial enhancement is all about
neighborhoods and histograms.
Transpose
• A(i,j) = B(j,i) i,j
Vertical Flip
Cropping an Image
• cutting out a portion of the image.
Given X_start Y_start Width and Height
Masks or Operators
• g(x,y) = T[(x,y)]
• Point processing
s = T(r)
Light Light
Dark Dark
Dark light
Dark light
A simple transform
Light
Dark
Dark light
Digital negative
Grey level Transformations
Square law transformation
Power law and Log Transformations
• s = c log (1+r)
• s=cr
Power Law Results
Worked on an RGB image
Log Transform for Fourier Domain
DC level image in space domain
2D Fourier Domain
2D Fourier transform
&
Unprocessed image
representation
2D Fourier Image after Log transform
Gamma Correction
Image Screen Image
Image Gamma Corrected Screen Image
Image
Image Statistics
N 1 M 1 Variance N 1 M 1
Mean
G(i, j )
i 0
(G
j 0
(i , j ) sm) 2
i 0 j 0
sm s var
NM NM
Histogram
0 255
Histogram
Image histogram
Piecewise Linear Transformations
.Contrast stretching
.Thresholding
.Grey level slicing
.Bit plane slicing
Contrast Stretching
Low contrast High Contrast
Piecewise linear
function
How did I get the stretch function?
Low contrast histogram High contrast histogram
Example contrast stretching
Original High Contrast
Thresholding and Grey Level Slicing
.If you know the intensity values you would like to have
enhanced in an image
.If the background needs to be retained the grey level
slicing method is used
.If the background isn’t important the pixels can be thresholded
to two levels.
Grey Level Slicing Thresholding
Examples of Thresholding
R 80-130 R 80-160
R 80-190 R 80-220
Examples of Grey level slicing
Grey slice
original 88
R 80-130
Grey slice Grey slice
128 168
R 80-130 R 80-130
Histogram Types
dark light
Low contrast High Contrast
Histogram Equalization
s=T(r) for a normalized image in the range [0,1]
Satisfies
a) T(r) is single valued and monotonically increasing in .
0 r 1
b) 0 T (r ) 1 for 0 r 1
CDF is integral of PDF
PDF is defined on [0,1] so is CDF and it satisfies above
mentioned conditions
ds
p s ( s ) pr ( r ) (1)
dr
Relation between PDF of the two variables
Histogram Equalization
r
CDF as a transform s T (r ) pr ( w)dw (2)
0
ds dT (r )
We can prove that pr ( r ) (3)
dr dr
Plugging this in eqn 1 ps ( s ) 1 0 s 1 (4)
This last equation states that the distribution is uniform.
T(r) depends on P(r) but P(s) will always be uniform
Discrete Domain Histogram Equalization
PMF or the probabilities for the use of a particular grey level
can be found in the following manner.
nk
pr (rk ) k 0,1,2....L 1
n
Setting up T(r) as the CDF
k
nk k 0,1,2....L 1
sk T (rk )
0 n
Histogram Equalization Results
Original Histogram Equalized
Histogram Matching (Specification)
.In contrast stretching we had control over the mapping
.In Equalization the mapping depended on the PDF of the image
.In matching we try to get the best of both worlds
Step 1: Equalization r
s T (r ) pr ( w)dw
0
Step 2: Define a variable z such that:
z
G ( z ) p z ( w)dw s
0
Step 3: Obtain mapping from r to z
z G 1 (s) G 1[T (r )]
Statistics from Histograms
Let p(ri) be the PDF defined by the normalized histogram.
The nth moment about its mean can be defined as:
L 1
un (r ) (ri m) n p(ri )
i 0
M being the sample mean
L 1
m ri p(ri )
i 0
The second moment or variance
L 1
u2 (r ) (ri m) 2 p(ri )
i 0
Arithmetic and Logical Operations
• AND OR NOT
• ADD SUB
Masks
for
logical
operators
Difference between original and histogram
equalized image.
Image Averaging
Image can be expressed as original image + noise
g ( x, y) f ( x, y) ( x, y)
If the noise is uncorrelated and has zero mean. An image
formed by averaging K noisy images could be created.
K
1
g ( x, y )
K
g ( x, y)
i 1
i
The expected average image should be close to the
original image as the noise will cancel out.
Eg ( x, y) f ( x, y)
The variance of the averaged image will be given by
1
g ( x, y ) n ( x, y)
K
Spatial Filtering
• Involves moving a kernel over the image to enhance the
image
• Kernel size is << Image size
• Odd sized masks are more common
Spatial Filtering
m(-1,-1) m(0,-1) m(1,-1)
f(x-1,y-1) f(x,y-1) f(x+1,y-1)
m(-1,0) m(0,0) m(1,0)
f(x-1,y) f(x,y) f(x+1,y) m(-1,1) m(0,1) m(1,1)
f(x-1,y+1) f(x,y+1) f(x+1,y+1)
Mask coefficients
Image pixels under mask
R = m(-1,-1) f(x-1,y-1) + m(0,-1) f(x,y-1) + m(1,-1) f(x+1,y-1) + ………..
a b
g ( x, y) m(s, t ) f ( x s, y t )
s at b
Spatial Filtering
• The operation is similar to Convolution. hence the masks are also
called convolution masks.
• Non linear operations such as finding median may also be done on
a neighborhood.
• Near the edges parts of the masks may lie beyond the image
boundary.
• To avoid this either a smaller filtered image is accepted.
• Or zeros are padded along the image boundary.
Smoothing Spatial Filters
• Averaging or Low pass filters.
• Used to remove unwanted detail.
• Smooth out unwanted sharp noise transitions.
• May smooth out edges which should be sharp.
Examples
Box filter Weighted Average
1 1 1 1 2 1
1/9 x 1 1 1 1/16 x 2 4 2
1 1 1 1 2 1
Smoothing Spatial Filters
• The box filter does a simple average.
• The weighted average filter weighs the contribution by the
distance from the central image pixel.
Original Mathematical expression
a b
m( s , t ) f ( x s , y t )
g ( x, y ) s at b
a b
m( s , t )
s at b
3 6 9 12
Home Work 2
• Write a matlab script to vertically flip an image.
• Write a matlab script to transpose an image.
• Write a matlab script to crop a circular region out from an image and
place it in a new square image of appropriate size with a black
background. The [x,y] coordinates of the center and the radius of the
circle shall be given.
• Write a matlab script to get the digital negative of an image.
• 1) Read in the given image.
2) Crop out a circular region with center at x,y = 3L/4 ,
3L/4 and radius=25. where L is the length of the image.
3) Display the circular cropped region on a square image with
black background. Lets call this image A.
Home Work 2
4)Display the original image with the circular region darkened to black.
Lets call this image B.
5)Vertically flip image B and display it.Lets call this image C.
6) Digitally invert a circular region at center x,y = 3L/4,3L/4 and radius
25 for image C and display this image. Lets call this image D.
7) Transpose image D and display. This last image should be labeled
E.
DUE : 22nd February 2007 at 11:59 pm
Read IP tool box functions at
www.Mathworks.com