Ch03-Intensity Transformations and Spatial Filtering
Ch03-Intensity Transformations and Spatial Filtering
1
www.imageprocessingbook.com
Chapter 3
Intensity
Transformations
&
Spatial Filtering
www.ImageProcessingPlace.com
rove this with www.csie.ntnu.edu.tw/~violet/IP93/Chapter01.ppt
© 2002 R. C. Gonzalez & R. E. Woods
Digital Image Processing, 2nd ed. 2
www.imageprocessingbook.com
Background
Background
>> f = imread('rose.tif');
>> imtool(f)
Background
1. Image Negative
2. Power-Law
3. Logarithmic
4. Contrast Stretching
Image Negative
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 Transformations
Useful for brightening the
darkest regions or
darkening the brightest
regions in an image.
Power-Law Transformations
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
Logarithmic Transformations
Logarithmic Transformations
Used to compress the dynamic range of values within an image.
g = c * log(1 + double(f))
where c is constant >> f = imread('spectrum.tif');
>> g = im2uint8(mat2gray(log(1+double(f))));
>> f=[0.1 2; 3000 900000];
>> imshow(g)
>> g = log(1+double(f))
g=
0.0953 1.0986
8.0067 13.7102
>> h = mat2gray(g)
h=
0 0.0737
0.5811 1.0000
© 2002 R. C. Gonzalez & R. E. Woods
Digital Image Processing, 2nd ed. 11
www.imageprocessingbook.com
Contrast-Stretching Transformations
Used to compress the dynamic range of values within an image.
s = T(r) = 1 / (1 + (m/r)E)
Output values are scaled to the range [0, 1]
g = 1./(1 + (m./(double(f) + eps)).^E)
E = 20 E ->
h(rk) = nk
Normalized Histogram:
p(rk) = h(rk)/n
= nk/n, (Relative frequency)
>> imhist(f);
Histogram Equalization
Histogram equalization attempts to obtain an even distribution of
intensity values in the image, often resulting in increased contrast.
Histogram Equalization
Let the transformation:
s = T(r)
satisfies the following conditions:
(a) T(r) is single-valued and monotonically increasing in
the interval 0 ≤ r ≤ 1
(b) 0 ≤ T(r) ≤ 1 for 0 ≤ r ≤ 1
Histogram Equalization
Let pr(r) & ps(s) denote the probability density functions (PDF) of
random variables r & s, respectively.
r
Let the transformation: s T (r ) pr ( w)dw
0
The PDF of the output levels is uniform, i.e., the above
transformation generates an image whose intensity levels are
equally likely and cover the entire range [0, 1].
For discrete quantities we work with summations, and the above
transformation becomes: k k nj
sk T (rk ) pr (rj )
j 1 j 1 n
for k = 1, 2, …, L.
© 2002 R. C. Gonzalez & R. E. Woods
Digital Image Processing, 2nd ed. 19
www.imageprocessingbook.com
Histogram Equalization
Example: Consider a 1010 image with eight possible intensity
values (i.e, a three bit image) and apply transformation:
k nj
sk T (rk )
j 1 n
k 1 2 3 4 5 6 7 8
r 0 1 2 3 4 5 6 7
rk 0 1/7 2/7 3/7 4/7 5/7 6/7 1
rk 0 0.14 0.23 0.43 0.57 0.71 0.86 1
nj=h(rj) 19 25 16 13 10 7 6 4
nj/n=pr(rj) 0.19 0.25 0.16 0.13 0.1 0.07 0.06 0.04
sk 0.19 0.44 0.6 0.73 0.83 0.9 0.96 1
s 1 3 4 5 6 6 7 7
© 2002 R. C. Gonzalez & R. E. Woods
Digital Image Processing, 2nd ed. 20
www.imageprocessingbook.com
Histogram Equalization
r 0 1 2 3 4 5 6 7
h(rj) 19 25 16 13 10 7 6 4
pr(rj) 0.19 0.25 0.16 0.13 0.1 0.07 0.06 0.04
h(sj) 0 19 0 25 16 13 17 10
pr(sj) 0 0.19 0 0.25 0.16 0.13 0.17 0.1
ORIGINAL HISTOGRAM TRANSFORMED HISTOGRAM
.25 .25
.20 .20
.15 .15
.10 .10
.05 .05
.00 .00
0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
© 2002 R. C. Gonzalez & R. E. Woods
Digital Image Processing, 2nd ed. 21
www.imageprocessingbook.com
Histogram Equalization
Using above transformation, average intensity and
contrast are increased
Histogram Equalization
>> f = imread('pollen.tif');
>> g = histeq(f, 256);
>> imshow(f), figure, imshow(g)
Transformation
r
s T (r ) pr ( w)dw
0
results in intensity levels, s, that have
a uniform probability density
function ps(s).
Define a variable z with the property:
z
H ( z ) p z ( w)dw s
0
We can find z = H-1(s) = H-1[T(r)]
whose PDF is the specified pz(z).
Resulting Histogram
Mask Size: m n
We assume: m = 2a+1, n = 2b+1
where a and b are non-negative integers
w(s, t ) f ( x s, y t )
g ( x, y ) s a t b
a b
w(s, t )
s a t b
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
5 6 5 3 1
2 6 2 2 1
7 3 3 2 0
2 5 6 5 3 1
2 6 2 2 1
7 3 3 2 0
2 1 5 6 5 3 1
2 6 2 2 1
7 3 3 2 0
2 1 3 5 6 5 3 1
2 6 2 2 1
7 3 3 2 0
2 1 3 16 5 6 5 3 1
2 6 2 2 1
7 3 3 2 0
2 1 3 16 -1 5 6 5 3 1
2 6 2 2 1
7 3 3 2 0
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
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
>> 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, [ ])
© 2002 R. C. Gonzalez & R. E. Woods
Digital Image Processing, 2nd ed. 49
www.imageprocessingbook.com
colfilt:
>> f=[1 2; 3 4]
f=
1 2
3 4
>> g = colfilt(f,[3 3],'sliding',@mean)
g=
1.1111 1.1111
1.1111 1.1111
Exercises
1. Do practice from [3]
2. Solve questions [2]: 3.1, 3.2, 3.5, 3.6, 3.21, 3.22
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 )
© 2002 R. C. Gonzalez & R. E. Woods
Digital Image Processing, 2nd ed. 59
www.imageprocessingbook.com
1
Function: fspecial(‘laplacian’, alpha) 1 1 1
implements a more general Laplacian mask: 1 4 1
1 1 1
1
© 2002 R. C. Gonzalez & R. E. Woods
1 1 1
Digital Image Processing, 2nd ed. 61
www.imageprocessingbook.com
Example: Exercise:
Find suitable Laplacian filters
>> f = imread('moon.tif'); for both –ve and +ve centers
>> w4 = fspecial('laplacian', 0); and for both 4 and 8 at
center to simplify the
>> w8 = [1 1 1; 1 -8 1; 1 1 1]; process of image
>> f = im2double(f); enhancement without
subtraction or addition of
>> g4 = f-imfilter(f, w4, 'replicate'); original image.
>> g8 = f-imfilter(f, w8, 'replicate');
>> imshow(f), figure, imshow(g4), figure, imshow(g8)
>> f = imread('cktboard.tif');
>> fn = imnoise(f, 'salt & pepper', 0.2);
>> gm = medfilt2(fn);
>> gms = medfilt2(fn, 'symmetric');
>> imshow(f), figure, imshow(fn)
>> figure, imshow(gm), figure, imshow(gms) (a)
References
[1] Gonzalez R. C., Woods R. E., Eddins S. L., Digital Image Processing Using
Matlab, Pearson Education, 2006.
[2] Gonzalez R. C., Woods R. E., Digital Image Processing, Pearson Education,
Second Edition, 2006.
[3] Institute of Signal Processing, Tampere University of Technology, Finland,
http://www.cs.tut.fi/~karaoglu/