Image Enhancement-Spatial Domain - Updated
Image Enhancement-Spatial Domain - Updated
(Spatial Domain) 1
Last updated: 13-11-15
$ Szeliski R., Computer Vision - Algorithms and Applications,
Springer, 2011. (Ch. 3)
Gonzalez R. C., Woods R. E., Eddins S. L., Digital Image Processing
Using Matlab, Pearson Education, 2nd edition, 2009. (Ch. 3)
# Gonzalez R. C., Woods R. E., Eddins S. L., Digital Image
Processing, Pearson Education, 3rd edition, 2007. (Ch. 3)
>> f = imread('rose.tif');
>> imtool(f)
Background 4
1. Image Negative
2. Power-Law
3. Logarithmic
4. Contrast Stretching
Image Negative 6
Digital negatives are often produced if the region of
interest contains mostly darker pixels (low intensity
values)
s = (L – 1) – r, where r [0, L-1]
>> g = imcomplement(f);
Power-Law (Gamma) Transformations
7
Useful for brightening the
darkest regions (0 < < 1)
or darkening the brightest
regions in an image ( > 1).
Here L = 2.
Power-Law (Gamma) Transformations
8
g = imadjust(f, [low_in high_in], [low_out high_out], gamma);
Example:
a. >> f = imread('xray.tif');
b. >> g = imadjust(f, [0 0.5], [0 1]); a
c. >> g = imadjust(f, [0 0.5], [0 1], 2);
d. >> g = imadjust(f, [0 0.5], [0 1], 0.5);
e. >> g = imadjust(f, [ ], [ ], 0.5);
f. >> g = imadjust(f, [0 1], [1 0]);
b c
Can we have
negative image by
using imadjust()?
d e f
Power-Law (Gamma) Transformations
9
Power-Law (Gamma) Transformations
10
>> rgb = imread('baby-mom.tif');
>> hsv = rgb2hsv(rgb);
>> hsv1 = hsv(:, :, 1);
>> hsv2 = hsv(:, :, 2);
>> hsv3 = hsv(:, :, 3);
>> hsv3a = imadjust(hsv3);
>> hsvNew = cat(3, hsv1, hsv2, hsv3a);
>> rgbNew = hsv2rgb(hsvNew);
>> imshow(rgb), figure, imshow(rgbNew)
Logarithmic Transformations
11
Logarithmic Transformations
12
Used to compress the dynamic range of values within an image.
s = c * log(1 + double(r))
where c is constant
>> f = [0.1 2; 3000 900000]; >> g = log(1+double(f))
1.0e+005 * g=
0.0000 0.0000 0.0953 1.0986
0.0300 9.0000 8.0067 13.7102
>> h = mat2gray(f) >> h = mat2gray(g)
h= h=
0 0.0000 0 0.0737
0.0033 1.0000 0.5811 1.0000
Logarithmic Transformations
13
>> f = imread('spectrum.tif');
>> g = im2uint8(mat2gray(log(1+double(f))));
>> imshow(g)
Logarithmic Transformations
14
I=imread('tire.tif');
imshow(I) I2=im2double(I);
J=1*log(1+I2);
J2=2*log(1+I2); Original c =1
J3=5*log(1+I2); figure,
imshow(J) figure,
imshow(J2) figure,
imshow(J3)
c=2 c =5
Contrast-Stretching Transformations
15
Used to compress the dynamic range of values within an image.
s = T(r) = 1 / (1 + (m/r)E),
where 0 < m < r
Output values are scaled to the range [0, 1]
>> g = 1./(1 + (m./(double(f) + eps)).^E)
E = 20 E ->
Contrast-Stretching Transformations
16
Contrast-Stretching Transformations
17
I=imread('tire.tif');
I2=im2double(I);
m=mean2(I2)
contrast1=1./(1+(m./(I2+eps)).^4);
contrast2=1./(1+(m./(I2+eps)).^5); Original E =4
contrast3=1./(1+(m./(I2+eps)).^10);
imshow(I2)
figure,imshow(contrast1)
figure,imshow(contrast2)
figure,imshow(contrast3)
E=5 E =10
Contrast-Stretching Transformations: Exercise
18
Original Transformed
MatLab Code ?
Intensity Transformations: Exercise
19
Identify which intensity transformation was used on MATLAB's
built-in liftingbody.png to create each of the four results below.
Write a script to reproduce the results using the intensity
transformation functions.
Original
The nth plane in the pixels are multiplied by the constant 2^n-1
For instance, consider the matrix
A=[ 167 133 111
144 140 135
159 154 148] and the respective bit format
Repeat this process for all the values in the matrix and the final result will be
[ 128 128 64
128 128 128
128 128 128 ]
Bit-Plane Slicing in Matlab
25
h(rk) = nk
Normalized Histogram:
p(rk) = h(rk)/n
= nk/n, (Relative frequency)
where n = MxN, the size of image
Histogram Equalization 32
Histogram equalization attempts to obtain an even distribution of
intensity values in the image, often resulting in increased contrast.
An unequalized image
Histogram (red)
Cumulative histogram (black)
33
# Histogram Equalization
34
1.5
0.5
0
0 50 100
4
10
2
1.5
0.5
0
0 100 200 300
#Histogram Matching or Specification
46
j 0 MN j 0
nj
with z 0, z 1, z 2, z 3, z 4, z 5, z 6, z 7.
0 1 2 3 4 5 6 7
#@Histogram Matching or Specification
51
First we will equalize both input and output (as before):
r pr(rk) Cumulative ch(r) sk=ch(r)x7 z pz(zk) Cumulative ch(z) G(zq)=ch(z)x7
0 0.19 0.19 1 0 0 0 0
1 0.25 0.44 3 1 0 0 0
2 0.21 0.65 5 2 0 0 0
3 0.16 0.81 6 3 0.15 0.15 1
4 0.08 0.89 6 4 0.2 0.35 2
5 0.06 0.95 7 5 0.3 0.65 5
6 0.03 0.98 7 6 0.2 0.85 6
7 0.02 1 7 7 0.15 1 7
sk T (ri ) G ( zq ) sk zq
every pixel with
s0 1 G ( z0 ) 0 1 3 value s0=1 in the
histogram-
s1 3 G ( z1 ) 0 equalized image
3 4 would have a
s2 5 G ( z2 ) 0 value of 3 (z3) in
s3 6 G ( z3 ) 1 5 5 the histogram-
specified image
s4 6 G ( z4 ) 2 6 6
s5 7 G ( z5 ) 5
7 7
s6 7 G ( z6 ) 6
s7 7 G ( z7 ) 7
#@Histogram Matching or Specification
53
>> imshow(f); 3
>> imshow(g);
1
0.5
>> imhist(g); 0
% desired histogram
>> p = twomodegauss(0.15, 0.05, 0.75, 0.05, 1, 0.07, 0.002);
>> h = histeq(f, p); x 10
4
>> plot(p);
3.5
0.02
3
0.018
2.5
0.016
2
0.014
0.012 1.5
0.01 1
0.008
0.5
0.006
0
0.004
0 50 100 150 200 250
0.002
0
0 50 100 150 200 250 300
#@Histogram Matching or Specification
Example
57
>> imshow(g); 3.5
x 10
4
>> imhist(h); 3
2.5
1.5
0.5
4
x 10
3.5
4
x 10 3
3.5
2.5
3
2
2.5
1.5
2
1
1.5
0.5
1
0
0.5
0 50 100 150 200 250
Mask Size: m n
We assume: m = 2a+1, n = 2b+1
where a and b are non-negative integers
Spatial Filtering (Neighborhood Processing)
62
w1 w2 w3 f4 f5 f6
w4 w5 w6 f7 f8 f9
w7 w8 w9
0 -1 0
2 3 5 7 2
-1 4 -1
3 4 7 5 2
0 -1 0
5 6 5 3 1
2 6 2 2 1
7 3 3 2 0
Spatial Filtering (Neighborhood Processing)
Linear Spatial Filtering
64
0 -1 0
-1 4 -1
2 3 5 7 2
0 -1 0
3 4 7 5 2
5 6 5 3 1
2 6 2 2 1
7 3 3 2 0
Spatial Filtering (Neighborhood Processing)
Linear Spatial Filtering
65
0 -1 0
-1 4 -1
2 3 5 7 2
0 -1 0
3 4 7 5 2
2 5 6 5 3 1
2 6 2 2 1
7 3 3 2 0
Spatial Filtering (Neighborhood Processing)
Linear Spatial Filtering
66
0 -1 0
-1 4 -1
2 3 5 7 2
0 -1 0
3 4 7 5 2
2 1 5 6 5 3 1
2 6 2 2 1
7 3 3 2 0
Spatial Filtering (Neighborhood Processing)
Linear Spatial Filtering
67
0 -1 0
-1 4 -1
2 3 5 7 2
0 -1 0
3 4 7 5 2
2 1 3 5 6 5 3 1
2 6 2 2 1
7 3 3 2 0
Spatial Filtering (Neighborhood Processing)
Linear Spatial Filtering
68
0 -1 0
-1 4 -1
2 3 5 7 2
0 -1 0
3 4 7 5 2
2 1 3 16 5 6 5 3 1
2 6 2 2 1
7 3 3 2 0
Spatial Filtering (Neighborhood Processing)
Linear Spatial Filtering
69
0 -1 0
-1 4 -1
2 3 5 7 2
0 -1 0
3 4 7 5 2
2 1 3 16 -1 5 6 5 3 1
2 6 2 2 1
7 3 3 2 0
Spatial Filtering (Neighborhood Processing)
Linear Spatial Filtering
70
0 -12 03 5 7 2
-1 43 -14 7 5 2
0 -1 0
2 1 3 16 -1 5 6 5 3 1
1 2 6 2 2 1
7 3 3 2 0
Spatial Filtering (Neighborhood Processing)
Linear Spatial Filtering
71
02 -13 0 5 7 2
-13 44 -1 7 5 2
0 -1 0
2 1 3 16 -1 5 6 5 3 1
1 -3 2 6 2 2 1
7 3 3 2 0
Basics of Spatial Filtering
72
• In spatial filtering (vs. frequency domain filtering), the output
image is computed directly by simple calculations on the pixels of
the input image.
• Spatial filtering can be either linear or non-linear.
• For each output pixel, some neighborhood of input pixels is used
in the computation.
• In general, linear filtering of an image f of size MN with a filter
mask of size mn is given by a b
g ( x, y ) w(s, t ) f ( x s, y t )
where a=(m-1)/2 and b=(n-1)/2 s a t b
• This concept is called convolution. Filter masks are sometimes
called convolution masks or convolution kernels.
Basics of Spatial Filtering
73 • N
o
n
l
i
n
e
a
r
s
p
a
Smoothing Spatial Filters
74
• Smoothing linear filters
– Averaging filters (Lowpass filters in Chapter 4))
• Box filter
• Weighted average filter
w(s, t ) f ( x s, y t )
g ( x, y ) s a t b
a b
w(s, t )
s a t b
Weighted average
Spatial Filtering (Neighborhood Processing)
77
>> f = imread('boxes.tif');
>> class(f)
ans =
uint8
>> w = ones(31);
>> g = imfilter(f, w);
>> imshow(f), figure, imshow(g, [ ])
Why?
>> fd = im2double(f);
>> gd = imfilter(fd, w);
>> imshow(fd), figure, imshow(gd, [ ])
Spatial Filtering (Neighborhood Processing)
Linear Spatial Filtering 85
Exercise 1a: Explain the following
a. Why image boxes.tif is not blurred around the outside
boundary of black boxes?
b. Why corners of black boxes don’t join when we use uint8
data type of image?
1b: Consider an image f=[1 0 0 0 0; 0 2 0 0 0; 0 0 3 0 0; 0 0 0 4 0; 0 0 0 0 5].
Filter it by hand with mask w=[-1 -1 -1; -1 8 -1; -1 -1 -1] with
padding of
c. Zeroes
d. Symmetric
e. Replicate
f. Circular
Verify your results through Matlab.
#Spatial Filtering (Neighborhood Processing)
Linear Spatial Filtering 86
[1, P3.21] (a) In a character recognition application, text pages are reduced to binary form
using a thresholding transformation function of the form shown in figure. This is followed by
a procedure that thins the characters until they become strings of binary 1’s on a background
of 0’s. Due to noise, the binarization and thinning processes result in broken strings of
characters with gaps ranging from 1 to 3 pixels. One way to “repair” the gaps is to run an
averaging mask over the binary image to blur it, and thus create bridges of nonzero pixels
between gaps. Give the (odd) size of the smallest averaging mask capable of performing this
task.
(b) After bridging the gaps, it is desired to threshold the image in order to convert it back to
binary form. For your answer in (a), what is the minimum value of the threshold required to
accomplish this, without causing the segments to break up again.
#Spatial Filtering (Neighborhood Processing)
Linear Spatial Filtering 87
Exercise 3 [1]: (due in next class)
Hint: Vertical bars are 5 pixels wide and their separation is 20 pixels
Use of Second Derivatives for Enhancement
88
O O O
f(x-1) f(x) f(x+1)
O O
f(x)- f(x-1) f(x+1) - f(x) 1st derivative
O
[ f(x+1) - f(x)]-[f(x)- f(x-1)] 2nd derivative
x y
– The Laplacian is a linear operator.
2 f
f ( x 1, y ) f ( x 1, y ) 2 f ( x, y )
x 2
2 f
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 )
Use of Second Derivatives for Enhancement
The Laplacian 95
2 f [ f ( x 1, y ) f ( x 1, y ) f ( x, y 1) f ( x, y 1)] 4 f ( x, y )
Use of Second Derivatives for Enhancement
(The Laplacian) 96
1
Function: fspecial(‘laplacian’, alpha) 1 1 1
implements a more general Laplacian mask: 1 4 1
1 1 1
1
1 1 1
Use of Second Derivatives for Enhancement
(The Laplacian) 97
Example: 1/2
>> f = imread('moon.tif'); % Image of the north pole of the moon - Courtesy of NASA
>> w4 = fspecial('laplacian', 0);
>> d = im2double(f);
>> dw4 = imfilter(d, w4, 'replicate');
>> g4 = d-dw4;
>> subplot(1,3,1), imshow(f), subplot(1,3,2),imshow(dw4), subplot(1,3,3),
imshow(g4)
Enhancement
using the
Laplacian
filter with -4
in the center
Use of Second Derivatives for Enhancement
(The Laplacian) 98
Example: 2/2
>> f = imread('moon.tif');
>> w8 = [1 1 1; 1 -8 1; 1 1 1];
>> d = im2double(f);
>> dw8 = imfilter(d, w8, 'replicate');
>> g8 = d-dw8;
>> subplot(1,3,1), imshow(f), subplot(1,3,2),imshow(dw8), subplot(1,3,3),
imshow(g8)
Enhancement
using the
Laplacian
filter with -8
in the center
# Use of Second Derivatives for Enhancement
(The Laplacian) 99
Exercise:
Find suitable Laplacian filters to simplify the process of
image enhancement without subtraction or addition of
original image. for
(a) –ve center
(b) +ve center
and for
(i) 4 at center
(ii) 8 at center
@#
100
colfilt:
Example:
f = imread(‘rose.tif');
imshow(f)
g = uint8(colfilt(f,[5 5],'sliding', @mean)); figure, imshow(g)
Exercise: itrans.m
is capable of doing all of the intensity transformations mentioned above.
function g = intrans(f, varargin)
Intensity Transformation
Transformation Corresponding intrans Call
Function
photographic negative neg=imcomplement(I); neg=intrans(I,'neg');
I2=im2double(I);
logarithmic log=5*log(1+I2);
log=intrans(I,'log',5);