(3

)
(3)
Intensity Transformation and Spatial Filtering
Intensity Transformation and Spatial Filtering
- Intensity transformation
* Change the intensity of each pixel in order to enhance the image:
g(x, y) = T[f(x, y)],
where f(x, y): input image, g(x, y): processed image, and T: operator
* Or: s = T(r), where r: input pixel, and s: output pixel
(a) (a) Histogram Histogram
- Histogram: occurrences of pixels
h(r
k
) = n
k
* r
k
: gray level, r
1
= 0, r
2
= 1, … r
L
= G, k = 1, 2, …, L
* MATLAB: G = 255 for uint8, 65,536 for uint16, and 1.0 for double
* n
k
: number of pixels of gray level r
k
* Normalize: p(r
k
) = n
k
/n
* n: total number of pixels
3-1
* May consider p(r) as the pixel’s probability: 0 ≤ p(r) ≤ 1
* E.g., Tiffany: pixels concentrate at the high gray levels (low contrast)

* Examples of histograms:
p(r)
r L
Dark Bright

Bright object, dark background

Good exposure
3-2
- MATLAB imhist function
* imhist(im): Display histogram
* h = imhist(im, b): h: histogram, b: number of histogram bins
(b) (b) Enhancing images using point processing Enhancing images using point processing
- No change on pixel values
s = T(r) = r
* s: output pixel value
* r: input pixel value
L
s
r
L
T(r)
- Contrast reversal: negative
s = T(r) = L – r
* Matlab:
im1 = 255 - im;
T(r)
Tiffany
3-3
- Contrast stretching: enhance contrast through histogram stretching
* Underexposed
s=
¦
255r
r
1
, if rr
1
255, otherwise.
r
1
# Matlab:
im = double(imread('lena.tif')); % Convert to type 'double'
r1 = 220;
t = im >= r1;
im1 = 255 * im / r1;
im1(t==1) = 255;
figure, imshow(uint8(im)), figure, imshow(uint8(im1));
* Overexposed
3-4

s=
¦
0, if rr
1
255
255−r
1
¦ r−r
1
) , otherwise.
r
1
# Matlab:
im = double(imread('tiffany.tif')); % Convert to type 'double'
r1 = 50; t = im < r1;
im1 = 255*(im-r1)/(255-r1);
im1(t==1) = 0;
figure, imshow(uint8(im)), figure, imshow(uint8(im1));
* Low-contrast, mid-gray-level
3-5
s=
¦
0, if rr
1
255
r
2
−r
1
¦ r−r
1
) , if r
1
≤r≤r
2
255, otherwise.
r
1
r
2
# Matlab:
im = double(imread('lena.tif')); % Convert to type 'double'
r1 = 50; r2 = 200; t1 = im < r1; t2 = im >= r2;
im1 = 255*(im-r1)/(r2-r1);
im1(t1==1) = 0; im1(t2==1) = 255;
im1 = uint8(im1); im = uint8(im);
figure, imhist(im), figure, imhist(im1);
figure, imshow(im), figure, imshow(im1);
* Most images:
r
1
r
2
s
1
s
2
3-6
s=
¦
s
1
r
1
r , if rr
1
s
2
−s
1
r
2
−r
1
¦ r−r
1
)+s
1,
if r
1
≤rr
2
255−s
2
255−r
2
¦ r−255)+255, otherwise.
(r
1
, s
1
)
(r
2
, s
2
)
* Non-linear stretching

- MATLAB imadjust function
* Syntax: g = imadjust(f, [low_in high_in], [low_out high_out], gamma)
* Map input gray level range [low_in high_in] to range [low_out high_out]
* gamma: a real number indicating the mapping curve
3-7
out
h
out
l
in
l
in
h
Gamma < 1 Gamma = 1 Gamma > 1
# E.g.: Fig. (a) original mammogram
- g = imadjust(f, [0 1], [1 0]): Fig. (b) → contrast reversal
- g = imadjust(f, [0.5 0.75], [0 1]): Fig. (c)
- g = imadjust(f, [ ], [ ], 2): Fig. (d)
(a) (b) (c) (d)
3-8
(c) (c) Histogram equalization Histogram equalization
- Histogram equalization
* Stretch histogram such that the image contrast is optimal
* Idea: full coverage and with equal probability
# Pixel values should cover entire gray level range
# The number of pixels in each gray level bin should be equal
* Continuous quantities
# Assume gray levels are continuous quantities normalized to range [0, 1]
# Intensity transformation: s = T(r)
# Probability of input pixel: p(r) = n
r
/N
2
# Probability of output pixel: q(s) = n
s
/N
2
# Accumulated number of pixels from gray level 0 to r:
N
r
=N
2

0
r
p¦ w) dw
(w: dummy variable)
# Define cumulative distribution function:
cdf ¦ r)≡

0
r
p¦ w) dw
# To generate an output image whose gray levels are equally likely, q(s)
should be a constant (Q)
3-9
p(r)
r
q(s)
s
1/L
Since

0
L
Qdw=1
, ∴ Q = 1/L,
N
r
=N
2

0
r
p¦w) dw=N
2
⋅cdf ¦ r)
N
s
=N
2

0
s
1/ Ldw=N
2
⋅s/ L=N
2
T ¦ r)/ L, Since N
r
= N
s
→ T(r) = L cdf(r)
* Discrete quantities
# Probability of output pixels cannot be constant; however, in terms of
regions, the number of pixels in a certain region can be constant

0
L
c=1, .
.
. c=1/ L - T ¦r )=Lcdf ¦r )
# E.g.: an image consisting of 100 pixels and gray levels: 0 ~ 7
3-10
r
Input gray
level r
Number
of pixels
p(r) cdf
Output gray
level s
Gray
level s
Number of
pixels
0 1 0.01 0.01 7×0.01 = 0 0 4
1 1 0.01 0.02 7×0.02 = 0 1 12
2 1 0.01 0.03 7×0.03 = 0 2 18
3 1 0.01 0.04 7×0.04 = 0 3 0
4 12 0.12 0.16 7×0.16 = 1 4 26
5 18 0.18 0.34 7×0.34 = 2 5 0
6 26 0.26 0.60 7×0.60 = 4 6 0
7 40 0.40 1.00 7×1.00 = 7 7 40
0 1 2 3 4 5 6 7
1 1 1 1
12
18
26
40
0 1 2 3 4 5 6 7
4 12
18
26
40
- MATLAB histeq function
* im1 = histeq(im), e.g., car:
3-11


- Advantages of histogram equalization
* Optimal contrast
* Fully automated (adaptive, no human intervention)
* Image normalization: easy to compare two images taken under different
lighting conditions
3-12
(d) (d) Image averaging Image averaging
- There is always noise in natural images
* Assume noise is added into every pixel of an ideal image:
g(x, y) = f(x, y) + η(x, y),
where g: resulting image, f: ideal image, η: noise
* f cannot be computed; it is estimated
- Image averaging: several images are taken for the same scene, and then
their average is computed
* Assume Gaussian noise: η ~ N(0, σ
η
2
)
* Totally M images: g
i
(x, y) = f(x, y) + η
i
(x, y), i = 1, 2, …, M
# Average:
¯
g¦ x , y)=
1
M

i
g
i
¦ x , y)
#
Expected value:
E ¦
¯
g¦ x , y)¦=E
¦
1
M

i
g
i
¦ x , y)
¦
=E
¦
1
M
|

i
f ¦ x , y)+

i
n
i
¦ x , y)
¦
¦
3-13
=
1
M
E
¦

i
f ¦ x , y)
¦
+
1
M
¦

i
n
i
¦ x , y)
¦
#
1
M
E
¦

i
f ¦ x , y)
¦
= f ¦ x , y)

, independent noise: E
¦

i
n
i
¦ x , y)
¦
≈0
# Hence,
¯
g¦ x , y)≈ f ¦ x , y)
→ ideal image
-
c
¯
g
2
=
1
M
c
n
2
- c
¯
g
=
1
.
M
c
n
: increasing M reduces the variance of noise
(e) (e) Spatial filtering Spatial filtering
- Spatial filtering
* Using a mask (filter) to process the image
* Mask operation: output pixel value = some algorithm performed on all
the pixels in the neighborhood of the corresponding input pixel
# Size of neighborhood = size of mask
# Mask slides from left to right, top to bottom
# Same operation is performed on every pixel
# Neighborhood exceeds image boundary: zero padding or replication of
3-14
border pixels
# Example:
1 2 3 0.11 0.11 0.11 1.33 2.33 1.78
4 5 6 * 0.11 0.11 0.11 = 3.00 5.00 3.67
7 8 9 0.11 0.11 0.11 2.67 4.33 3.11
- Smoothing filter
* Spatial domain: pixel averaging, blurring
* Frequency domain: attenuate high frequency components
# Smooth area: low frequency
# Noise and edge: high frequency
* Lowpass spatial filtering
# Image blurring, noise reduction
3-15
# 3×3 and5×5 mean filters:
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1
1 1
1 1
1 1 1 1 1
1 1 1 1 1
1
9
×
1
25
×
# Gaussian smoothing:
G¦ x , y)=
1
2nc
2
e
−¦ x
2
+y
2
)/ 2c
2
σ =1
5×5 Gaussian mask (σ = 0.5):
0.00000007 0.00002809 0.00020755 0.00002809 0.00000007
0.00002809 0.01133177 0.08373106 0.01133177 0.00002809
0.00020755 0.08373106 0.61869351 0.08373106 0.00020755
0.00002809 0.01133177 0.08373106 0.01133177 0.00002809
0.00000007 0.00002809 0.00020755 0.00002809 0.00000007
3-16
* MATLAB
# w = fspecial('gaussian', 3, 0.5), im1 = imfilter(im, w)
→ Generate a 3×3 Gaussian filter with σ = 0.5 and filter the image
# Filter: 'gaussian', 'sobel', 'prewitt', 'laplacian', 'log', 'average', 'unsharp'
Original image

Result of smoothing
- Sharpening filter
* Enhance details and edges
* Spatial domain: differentiation; frequency domain: attenuate low-
frequency components (highpass)
* Gradient filter
# Gradient: difference between two neighboring points
3-17
x direction:

∂ x
=∣ f ¦ x+1, y)− f ¦ x , y)∣
y direction:

∂ y
=∣ f ¦ x , y+1)− f ¦ x , y)∣
# Gradient vector:
∇ f =
|
∂ f
∂ x
∂ f
∂ y
¦
, magnitude: mag¦∇ f )=
|
¦
∂ f
∂ x
)
2
+
¦
∂ f
∂ y
)
2
¦
1/2
# Roberts cross-gradient operators
1 0 0 1
g(x, y) = |f(x, y) – f(x+1, y+1)| + | f(x+1, y) – f(x, y+1)|
0 −1 −1 0
# Prewitt operators (G = |G
x
| + |G
y
|)
G
x
−1 0 1
G
y
−1 −1 −1
−1 0 1 0 0 0
−1 0 1 1 1 1
# Sobel operators (G = |G
x
| + |G
y
|)
3-18
G
x
−1 0 1
G
y
−1 −2 −1
−2 0 2 0 0 0
−1 0 1 1 2 1
# Sharpening: g(x, y) = f(x, y) + G(x, y)
- Example of Roberts operation:
Input image (F): Gx:
50 70 70 70 70 70 70 70 0 0 0 0 0 0 0
50 50 70 70 70 70 70 70 0 0 0 0 0 0 0
50 50 50 70 70 70 70 70 0 0 0 0 0 0 0
50 50 50 50 70 70 70 70 0 0 0 0 0 0 0
50 50 50 50 50 70 70 70 0 0 0 0 −20 0 0
50 50 50 50 50 70 70 70 0 0 0 −20 −20 0 0
50 50 50 50 70 70 70 70 0 −20 −20 −20 0 0 0
50 50 70 70 70 70 70 70 −20 −20 0 0 0 0 0
50 70 70 70 70 70 70 70
Output image = F + Gx + Gy: Gy:
70 90 70 70 70 70 70 70 20 20 0 0 0 0 0
50 70 90 70 70 70 70 70 0 20 20 0 0 0 0
50 50 70 90 70 70 70 70 0 0 20 20 0 0 0
50 50 50 70 90 70 70 70 0 0 0 20 20 0 0
50 50 50 50 50 70 70 70 0 0 0 0 20 0 0
3-19
50 50 50 30 30 70 70 70 0 0 0 0 0 0 0
50 30 10 30 70 70 70 70 0 0 −20 0 0 0 0
30 30 70 70 70 70 70 70 0 0 0 0 0 0 0
50 70 70 70 70 70 70 70
# E.g.:
 G
x
G
y
G
* Laplacian filter

2
f ¦ x , y)=

2
f ¦ x , y)
∂ x
2
+

2
f ¦ x , y)
∂ y
2
[f(x+1, y) + f(x–1, y) – 2f(x, y)] + [f(x, y+1) + f(x, y–1) – 2f(x, y)]
= f(x+1, y) + f(x–1, y) + f(x, y+1) + f(x, y–1) – 4f(x, y)
# Laplacian mask:
3-20
0 1 0
1 −4 1
0 1 0
# Sharpening: g(x, y) = f(x, y) – ∇
2
f(x, y), mask:
0 0 0 0 1 0 0 −1 0
0 1 0 − 1 −4 1 = −1 5 −1
0 0 0 0 1 0 0 −1 0
# Example of Laplacian operation:
Input image (F): G:
50 70 70 70 70 70 70 70 20 −40 0 0 0 0 0 0
50 50 70 70 70 70 70 70 0 40 −40 0 0 0 0 0
50 50 50 70 70 70 70 70 0 0 40 −40 0 0 0 0
50 50 50 50 70 70 70 70 0 0 0 40 −40 0 0 0
50 50 50 50 50 70 70 70 0 0 0 0 40 −20 0 0
50 50 50 50 50 70 70 70 0 0 0 0 40 −20 0 0
50 50 50 50 70 70 70 70 0 0 20 40 −40 0 0 0
50 50 70 70 70 70 70 70 0 40 −40 −20 0 0 0 0
50 70 70 70 70 70 70 70 20 −40 0 0 0 0 0 0
3-21
Output image = F – G:
30 110 70 70 70 70 70 70
50 10 110 70 70 70 70 70
50 50 10 110 70 70 70 70
50 50 50 10 110 70 70 70
50 50 50 50 10 90 70 70
50 50 50 50 10 90 70 70
50 50 30 10 110 70 70 70
50 10 110 90 70 70 70 70
30 110 70 70 70 70 70 70
# Lena:
(a) Original (b) Result of Laplacian
filtering
(c) Result: = (a) – (b)
3-22
# Laplacian filter is very sensitive to noise
- Improvement: reduce noise using Gaussian filter before Laplacian
filtering (Laplacian of Gaussian filter, LOG)
LOG¦ x , y)=∇
2
| G¦ x , y)¦=

2
G
∂ x
2
+

2
G
∂ y
2
;

2
G
∂ x
2
=

2
∂ x
2
|
1
2nc
2
e
−¦ x
2
+y
2
)/ 2c
2
¦
=
1
2nc
2


∂ x
|
−x
c
2
e
−¦ x
2
+y
2
)/ 2c
2
¦
=
1
2nc
2
|
¦
−1
c
2
e
−¦ x
2
+y
2
)/ 2c
2
+
−x
c
2

−x
c
2
e
−¦ x
2
+y
2
)/ 2c
2
)
¦
=
1
2nc
2
|
x
2
−c
2
c
4
e
−¦ x
2
+y
2
)/ 2c
2
¦
;

2
G
∂ y
2
=

2
∂ y
2
|
1
2nc
2
e
−¦ x
2
+y
2
)/ 2c
2
¦
=
1
2nc
2


∂ y
|
−y
c
2
e
−¦ x
2
+y
2
)/ 2c
2
¦
=
1
2nc
2
|
¦
−1
c
2
e
−¦ x
2
+y
2
)/ 2c
2
+
−y
c
2

−y
c
2
e
−¦ x
2
+y
2
)/ 2c
2
)
¦
=
1
2nc
2
|
y
2
−c
2
c
4
e
−¦ x
2
+y
2
)/ 2c
2
¦
3-23
.
.
. LOG¦ x , y)=
1
2nc
2
|
x
2
+y
2
−2c
2
c
4
e
−¦ x
2
+y
2
)/ 2c
2
¦
, mask:
0 0 −1 0 0
0 −1 −2 −1 0
−1 −2 16 −2 −1
0 −1 −2 −1 0
0 0 −1 0 0
# Matlab: w = fspecial('log')
0.0448 0.0468 0.0564 0.0468 0.0448
0.0468 0.3167 0.7146 0.3167 0.0468
0.0564 0.7146 -4.9048 0.7146 0.0564
0.0468 0.3167 0.7146 0.3167 0.0468
0.0448 0.0468 0.0564 0.0468 0.0448
* MATLAB:
# im = double(imread('moon.tif'));
w = fspecial('laplacian', 0);
im1 = imfilter(im, w, 'replicate');
% Scale im1
pos = double(im1>0) .* im1 / max(im1(:)); % Scale positive values: [0 1]
neg = -double(im1<0) .* im1 / min(im1(:)); % Scale negative values: [-1 0]
scale = pos + neg; % Range: [-1 1]
im2 = im-50*scale; % 50: strength
im2(im2<0)=0; im2(im2>255)=255; figure, imshow(uint8(im2));
3-24
im scale im2
- Median filtering
* Ordering the pixels under the mask, and replace the output center pixel
with the median one (remove extreme values)
* Best for salt-and-pepper noise: g = imnoise(im, 'salt & pepper')
Salt-and-pepper noise

Result of median filtering
3-25
* MATLAB, e.g.:
# im1 = medfilt2(im) → Default: 3×3 mask
# im1 = medfilt2(im, [5 5], 'zeros') → 5×5 median filter, padded with zeros
# im1 = ordfilt2(im, 5, ones(3)) → 3×3 ordered filter (median: 5
th
element)
# im1 = ordfilt2(im, median[1:m*n], ones[m n]) → m×n ordered filter
3-26