0% found this document useful (0 votes)
94 views112 pages

Image Enhancement-Spatial Domain - Updated

Histogram equalization is an image enhancement technique that attempts to obtain an even distribution of intensity values in an image. It often results in increased contrast. An unequalized image and its histogram show intensity values are not evenly distributed, with the cumulative histogram rising more slowly in some ranges. After histogram equalization, the same image and its histogram show the intensity values are now more evenly distributed, with the cumulative histogram rising more linearly.

Uploaded by

zain javaid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views112 pages

Image Enhancement-Spatial Domain - Updated

Histogram equalization is an image enhancement technique that attempts to obtain an even distribution of intensity values in an image. It often results in increased contrast. An unequalized image and its histogram show intensity values are not evenly distributed, with the cumulative histogram rising more slowly in some ranges. After histogram equalization, the same image and its histogram show the intensity values are now more evenly distributed, with the cumulative histogram rising more linearly.

Uploaded by

zain javaid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 112

Image Enhancement

(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)

# For the graduate level course


@ For DIP students only Prof. Dr. Zulfiqar Habib
$ For CV students only http://vig.ciitlahore.edu.pk
Background 2
Background 3

>> f = imread('rose.tif');
>> imtool(f)
Background 4

g(x, y) = T[f(x, y)] => s = T(r)


Intensity Transformation Functions
5

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

Result 1 Result 2 Result 3 Result 4


Bit-Plane Slicing
20
Digitally, an image is represented in terms of pixels. These
pixels can be expressed further in terms of bits. Consider
the image ‘coins.png’ and the pixel representation of the
image.
Bit-Plane Slicing
21
Consider the pixels that are bounded within the yellow line.
The binary formats for those values are (8-bit
representation)
Bit-Plane Slicing
22
o The binary format for the pixel
value 167 is 10100111
o Similarly, for 144 it is 10010000
o This 8-bit image is composed of
eight 1-bit planes.
o Plane 1 contains the lowest order
bit of all the pixels in the image.
Bit-Plane Slicing
23

o And plane 8 contains the highest


order bit of all the pixels in the
image.
Combining Bit Planes
24

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

Combine the 8 bit plane and 7 bit plane.


For 10100111, multiply the 8 bit plane with 128 and 7 bit plane with 64.
(1x128)+(0x64)+(1x0)+(0x0)+(0x0)+(1x0)+(1x0)+(1x0)=128

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

A=[167 133 111


      144 140 135
      159 154 148];
B=bitget(A,1) %Lowest order bit of all pixels
B=
1 1 1
0 0 1
1 0 0
>> B=bitget(A,8) %Highest order bit of all
pixels
B=
1 1 0
1 1 1
1 1 1
Bit-Plane Slicing in Matlab
26

A=[167 133 111


      144 140 135
      159 154 148];

B=bitget(A,1) %Lowest order bit of all pixels


B=
1 1 1
0 0 1
1 0 0

>> B=bitget(A,8) %Highest order bit of all


pixels
B=
1 1 0
1 1 1
1 1 1
Bit-Plane Slicing in Matlab: Example
27
>> A=imread('coins.png');
>> B=bitget(A,1);
>> imshow(logical(B)); title('Bit plane 1');
>> B=bitget(A,2);
>> imshow(logical(B)); title('Bit plane 2');
>> B=bitget(A,3);
>> imshow(logical(B)); title('Bit plane 3');
>> B=bitget(A,4);
>> imshow(logical(B));title('Bit plane 4');
Bit plane 1 Bit plane 2 Bit plane 3 Bit plane 4
Bit-Plane Slicing in Matlab: Example
28
>> B=bitget(A,5);
>> imshow(logical(B)); title('Bit plane 5');
>> B=bitget(A,6);
>> imshow(logical(B)); title('Bit plane 6');
>> B=bitget(A,7);
>> imshow(logical(B)); title('Bit plane 7');
>> B=bitget(A,8);
>> imshow(logical(B));title('Bit plane 8');

0-127 -> 0 128-255 -> 1


Bit plane 5 Bit plane 6 Bit plane 7 Bit plane 8
Bit-Plane Slicing in Matlab: Example
29
%Image reconstruction by combining 8 bit plane and 7 bit plane
>> B=zeros(size(A));
>> B=bitset(B,7,bitget(A,7));
>> B=bitset(B,8,bitget(A,8));
>> B=uint8(B);
>> imshow(B);
Bit-Plane Slicing in Matlab: Example
30
%Image reconstruction by combining 8,7,6 and 5 bit planes
>> B=zeros(size(A));
>> B=bitset(B,8,bitget(A,8));
>> B=bitset(B,7,bitget(A,7));
>> B=bitset(B,6,bitget(A,6));
>> B=bitset(B,5,bitget(A,5));
>> B=uint8(B);
>> imshow(B);
Histogram Processing & Function Plotting
31
Generating & Plotting Image Histograms:

h(rk) = nk

where rk is the kth intensity value,


nk is the number of pixels in the image with
intensity rk.

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)

The same image after


histogram equalization
# Histogram Equalization
33
The intensity levels in an image may be viewed as random variables in
the interval [0, L-1].
Let Pr(r) and Ps(s) denote the probability density function (PDF) of
random variables r and s, resp. r
s  T (r )  ( L  1)  pr ( w)dw
0
(3.3-4)

33
# Histogram Equalization
34

At first, the continuous case will be studied:


r is the intensity of the image in [0, L-1].
we focus on transformations s=T(r):
T(r) is strictly monotonically increasing.
T(r) must satisfy:
0  T (r )  L  1, for 0  r  L  1
# Histogram Equalization
35

The condition for T(r) to be monotonically


increasing guarantees that ordering of the output
intensity values will follow the ordering of the
input intensity values (avoids reversal of
intensities).
If T(r) is strictly monotonically increasing then the
mapping from s back to r will be 1-1.
The second condition (T(r) in [0,1]) guarantees that
the range of the output can be the same as the
range of the input.
# Histogram Equalization
36
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. We cannot perform inverse
mapping (from s to r).
(b) 0 ≤ T(r) ≤ 1 for 0 ≤ r ≤ 1. Inverse mapping is
possible.
# Histogram Equalization 37
Let pr(r) & ps(s) denote the probability density functions (PDF) of
random variables r & s, respectively. r
Let the transformation:

s  T (r )  ( L  1) p ( w)dw
0
r

We can view intensities r and s as random variables and their


histograms as PDFs pr(r) and ps(s).

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 )  ( L  1) pr ( rj ) ( L  1)
j 0 j 0 n
for k = 1, 2, …, L.
Histogram Equalization 38
k
nj
sk  T ( rk )  ( L  1)
j 0 n
Given histogram of image (gray-level values & their numbers)
Step 1: Normalize the number of gray-levels values by dividing by
the total number of pixels.
Step 2: Create a running sum (cumulative) of the histogram values.
Step 3: Multiply these values by the maximum gray-level value and
then round the result to the closest integer.
Step 4: Map the original values to the results from step 3 by a one-to-
one correspondence.
Histogram Equalization 39
Example: Consider a 55 image with 3 bits/pixel:
nj k
nj k
nj
n ( L  1)
n j 0 j 0 n
Gray-level Number Step 1 Step 2 Step 3 Step 4
1 1 3 3 2 value of pixels

1 1 0 0 1 r h(r) Normalized Cumulative ch(r) x 7 s(r)


h(r) ch(r)
4 0 7 6 0
0 5 5/25 5/25 1 5
1 4 7 7 0
1 8 8/25 13/25 4 11
1 4 5 5 1
2 1 1/25 14/25 4 0
3 2 2/25 16/25 4 0
4 4 4 4 4
4 3 3/25 19/25 5 3
4 4 1 1 4
5 2 2/25 21/25 6 3
5 1 7 6 1
6 1 1/25 22/25 6 0
4 5 7 7 1
7 3 3/25 25/25 7 3
4 5 6 6 4
Total 25 1 25
Histogram Equalization 40
Exercise: Consider the histogram equalized image of previous
example:
4 4 4 4 4
4 4 1 1 4
5 1 7 6 1
4 5 7 7 1
4 5 6 6 4

and apply again the process of histogram equalization. Do you notice


any change?
Histogram Equalization 41
Example: Given the frequency distribution (instead of image):
Gray-level
value
Number of
pixels Step 1 Step 2 Step 3 Step 4
r h(r) Normalized Cumulative
ch(r) x 7 s(r)
h(r) ch(r)
0 19 19/100 19/100 1 0
1 25 25/100 44/100 3 19
2 16 16/100 60/100 4 0
3 13 13/100 73/100 5 25
4 10 10/100 83/100 6 16
5 7 7/100 90/100 6 13
6 6 6/100 96/100 7 17
7 4 4/100 100/100 7 10
Total 100 1 100
Histogram Equalization 42
Example: Given the frequency distribution (instead of image):
Gray-level Number of
value pixels
r h(r)
0 19
ORIGINAL HISTOGRAM
1 25
25
2 16
20
3 13
15
4 10
10
5 7
5
6 6
7 4 0
0 1 2 3 4 5 6 7
Histogram Equalization 43
Example: Given the frequency distribution (instead of image):
Gray-level
value
Step 4
r s(r)
0 0
TRANSFORMED HISTOGRAM
1 19
25
2 0
20
3 25
15
4 16
10
5 13
5
6 17
7 10 0
0 1 2 3 4 5 6 7
Histogram Equalization - Summary
44
1. After histogram equalization, the intensity values are more
effectively distributed over the histogram range.
2. Contrast is increased.
3. Repeated histogram equalization does not change image.
4. If the pixel p is the brightest pixel in range x, it will remain the
brightest pixel.
5. Frequencies of gray-levels in histogram equalized image are not
exactly same due to the discrete values and round off.
6. # If the transformation is monotone increasing, grey level order of
the image is preserved, such that for any two grey levels t1 and t2 ,
and their corresponding transformed values T[t1] and T[t2]:
t1 < t2 if and only if T[t1] < T[t2]
Histogram Equalization in Matlab
45
>> f = imread('pollen.tif');
>> h = histeq(f);
>> subplot(2,2,1), imshow(f),
subplot(2,2,2), histogram(f),
subplot(2,2,3), imshow(h),
subplot(2,2,4), histogram(h)
4
10
2

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

Histogram matching is similar to histogram


equalization, except that instead of trying to make
the output image have a flat histogram, we would
like it to have a histogram of a specified shape, say
pz(z).
#Histogram Matching or Specification
47
#@Histogram Matching or Specification
48
1. We obtain pr(r) from the input image and use
r
s  T (r )  ( L  1)  pr ( w)dw
0
to obtain intensity values s.
2. Use the specified PDF in z
s  G ( z )  ( L  1)  p z ( w)dw
0

to obtain the transformed function G(z).


3. We can find z = G-1(s) whose PDF is equal to the specified PDF
pz(z).

Note: w is dummy variable of integration


#@Histogram Matching or Specification
49
Equalize the initial histogram of the image:
k
sk  T (rk )  ( L  1) pr (rj )  ( L  1) k

j 0 MN j 0
 nj

Equalize the target histogram:


q
sk  G ( zq )  ( L  1) pz (ri )
i 0

which is labelled (1) in Figure 3.25 (b),


for a value of q so that G(zq) = sk = T(rk)
where
z  Gthe
1 inverse transformation
( s )  G 1 (T (r )) is
q k k

which is labelled (2) in Figure 3.25 (b).


#@Histogram Matching or Specification
50
Consider the 3-bit 64x64 image:

It is desired to transform this histogram to:


pz ( z0 )  0.00 pz ( z1 )  0.00 p z ( z2 )  0.00 pz ( z3 )  0.15
pz ( z4 )  0.20 pz ( z5 )  0.30 pz ( z6 )  0.20 p z ( z7 )  0.15

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

Notice that G(z) is not strictly monotonic. We must resolve


this ambiguity by choosing, e.g., the smallest value for the
inverse mapping.
#@Histogram Matching or Specification
52
Perform inverse mapping: For every value of sk find the smallest value
of G(zq) that is closest to sk: and store these mappings from sk to zq.

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

r pr(rk) sk G(zq) Mapping of sk & G(zq) pz(zq) pz(zq)


(result) (desired)
0 0.19 1 0 3 0 0
1 0.25 3 0 4 0 0
2 0.21 5 0 5 0 0
3 0.16 6 1 6 0.19 0.15
4 0.08 6 2 6 0.25 0.2
5 0.06 7 5 7 0.21 0.3
6 0.03 7 6 7 0.24 0.2
7 0.02 7 7 7 0.11 0.15
#@Histogram Matching or Specification
54 N
ot
ic
e
th
at
du
e
to
di
sc
re
tiz
#@Histogram Matching or Specification
A Specified Histogram
55
function p = twomodegauss(m1, sig1, m2, sig2, A1, A2, k)
% Generates a two-mode Gaussian function. in the interval [0,1].
% P is a 256-element vector normalized so that SUM(P) equals 1.
% The mean and standard deviation of the modes are (M1, SIG1) and (M2, SIG2), resp.
% A1 and A2 are the amplitude values of the two modes.
% Since the output is normalized, only the relative values of A1 and A2 are important.
% K is an offset value that raises the "floor" of the function.
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(:));
#@Histogram Matching or Specification
Example
56
>> f = imread('Mars-Moon.tif'); 3.5
x 10
4

>> imshow(f); 3

>> imhist(f); 2.5

>> g = histeq(f, 256); 1.5

>> imshow(g);
1

0.5

>> imhist(g); 0

0 50 100 150 200 250

% 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

0 50 100 150 200 250

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

0 50 100 150 200 250


#@Histogram Matching or Specification
Algorithm
58
INPUT: Input image, Target image
OUTPUT: Output image that has the same characteristic as the
target image
Steps:
1) Read the Input image and the Target image.
2) Obtain the histogram of the input image and the target
image.
3) Equalize the input and the target images.
4) Calculate the transformation function of the target Image.
5) Map the original image gray level to the final gray level.
#Local or Adaptive Histogram Processing
59
It is the process of small regions of the image
(called tiles) using histogram equalization for each
tile individually.
#Local or Adaptive Histogram Processing
60
>> imshow(f);
>> g1 = adapthisteq(f); % default is 'NumTiles', [8 8]
>> imshow(g1)
>> g2 = adapthisteq(f, 'NumTiles', [25 25]);
>> imshow(g2)
% 'ClipLimit‘: Real scalar in the range [0 1] that specifies a
contrast enhancement limit. Higher numbers result in more
contrast. Default: 0.01
>> g3 = adapthisteq(f, 'NumTiles', [25 25], 'Cliplimit', 0.05);
>> imshow(g3)
Spatial Filtering (Neighborhood Processing)
61

Linear Spatial Filtering


@ Nonlinear Spatial Filtering

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

Linear Spatial Filtering f1 f2 f3

w1 w2 w3 f4 f5 f6

w4 w5 w6 f7 f8 f9

w7 w8 w9

g5 = f1*w1+ f2*w2+ f3*w3+ f4*w4+ f5*w5+ f6*w6+ f7*w7+ f8*w8+ f9*w9


Spatial Filtering (Neighborhood Processing)
Linear Spatial Filtering 63

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 MN with a filter
mask of size mn 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

Box filter Weighted average


Smoothing Spatial Filters
75

Which mask is suitable for anti-aliasing?

Box filter Weighted average


Smoothing Spatial Filters
76
• The general implementation for filtering an M  N image with
a weighted averaging filter of size m  n is given by
a b

  w(s, t ) f ( x  s, y  t )
g ( x, y )  s  a t b
a b

  w(s, t )
s  a t b

where a=(m-1)/2 and b=(n-1)/2

Weighted average
Spatial Filtering (Neighborhood Processing)
77

Linear Spatial Filtering


Given a filter w and an image f, we can compute a
filtered image g such that g(x,y) is a linear
combination of f(x,y) and its neighbors.
Spatial Filtering (Neighborhood Processing)
78

Linear Spatial Filtering


• Correlation
• Convolution w(i,j)
• Padding
• Same or Full
f(i,j)
Spatial Filtering (Neighborhood Processing)
79
Given an image f and mask w: Correlation
f= w= g=
1 4 7 9 6 3
0 0 0 0 0
2 5 8 8 5 2
0 0 0 0 0 7 4 1
3 6 9
0 0 1 0 0
0 0 0 0 0
Convolution
0 0 0 0 0
w1 = g1 =
9 8 7 1 2 3
Solution 6 5 4 4 5 6
3 2 1 7 8 9

Another solution: Keep mask symmetric


Spatial Filtering (Neighborhood Processing)
80

Correlation & Convolution


with Zero Padding
Spatial Filtering (Neighborhood Processing)
81
Linear Spatial Filtering: Syntax
g = imfilter(f, w, filtering_mode, boundary_options, size_options)

filtering_mode: 'corr' (default) or 'conv'


boundary_options: (Input array values outside the bounds of the array)
P Implicitly assumed to have the value P. (Default is P = 0).
'symmetric' Computed by mirror-reflecting the array across the array border.
'replicate' Assumed to equal the nearest array border value.
'circular' Computed by implicitly assuming the input array is periodic.
size_options 'same' (default) or 'full'
Spatial Filtering (Neighborhood Processing)
Linear Spatial Filtering >> fp=padarray(f, [2 2], 0)
82
fp =
>> 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]
0 0 0 0 0 0 0 0 0
f=
0 0 0 0 0 0 0 0 0
1 0 0 0 0
0 0 1 0 0 0 0 0 0
0 2 0 0 0
0 0 0 2 0 0 0 0 0
0 0 3 0 0
0 0 0 0 3 0 0 0 0
0 0 0 4 0
0 0 0 0 0 4 0 0 0
0 0 0 0 5
0 0 0 0 0 0 5 0 0
>> w=[-1 -1 -1; -1 8 -1; -1 -1 -1]
0 0 0 0 0 0 0 0 0
w=
0 0 0 0 0 0 0 0 0
-1 -1 -1
>> gp = imfilter(fp, w)
-1 8 -1
gp =
-1 -1 -1
0 0 0 0 0 0 0 0 0
>> g = imfilter(f, w)
0 -1 -1 -1 0 0 0 0 0
g=
0 -1 6 -3 -2 0 0 0 0
6 -3 -2 0 0
0 -1 -3 12 -5 -3 0 0 0
-3 12 -5 -3 0
0 0 -2 -5 18 -7 -4 0 0
-2 -5 18 -7 -4
0 0 0 -3 -7 24 -9 -5 0
0 -3 -7 24 -9
0 0 0 0 -4 -9 36 -5 0
0 0 -4 -9 36
0 0 0 0 0 -5 -5 -5 0
0 0 0 0 0 0 0 0 0
Spatial Filtering (Neighborhood Processing)
Linear Spatial Filtering – Padding Options 83
>> f=[1 2; 3 4]
f=
1 2
3 4

>> fp=padarray(f, [2 2], 0) >> fp=padarray(f, [2 2], 'replicate')


fp = fp =
0 0 0 0 0 0 1 1 1 2 2 2
0 0 0 0 0 0 1 1 1 2 2 2
0 0 1 2 0 0 1 1 1 2 2 2
0 0 3 4 0 0 3 3 3 4 4 4
0 0 0 0 0 0 3 3 3 4 4 4
0 0 0 0 0 0 3 3 3 4 4 4
>> fp=padarray(f, [2 2], 'symmetric') >> fp=padarray(f, [2 2], 'circular')
fp = fp =
4 3 3 4 4 3 1 2 1 2 1 2
2 1 1 2 2 1 3 4 3 4 3 4
2 1 1 2 2 1 1 2 1 2 1 2
4 3 3 4 4 3 3 4 3 4 3 4
4 3 3 4 4 3 1 2 1 2 1 2
2 1 1 2 2 1 3 4 3 4 3 4
Spatial Filtering (Neighborhood Processing)
84
Linear Spatial Filtering:

>> 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

Exercise 2 [1]: (due in next class)

[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

= f(x+1)+ f(x-1)- 2f(x)


Linear Spatial Filters
Sharpening Spatial Filters 89
• Sharpening filters are based on computing
spatial derivatives of an image.
• The first-order derivative of a one-dimensional
function f(x) is f
 f ( x  1)  f ( x)
x
• The second-order derivative of a one-
dimensional function f(x) is
2 f
 f ( x  1)  f ( x  1)  2 f ( x)
x 2
Sharpening Spatial Filters
An Example 90
Linear Spatial Filters
Sharpening Spatial Filters 91
Summary: [2, pp. 149]
First-order derivatives:
– Generally produce thicker edges in an image
– Generally have a stronger response to a gray-level step
Linear Spatial Filters
Sharpening Spatial Filters 92
Summary: [2, pp. 149]
Second-order derivatives:
– Stronger response to fine detail, such as thin lines and isolated points
– Produce a double response at step changes in gray-level
– More stronger response to a point than a line, and to a line than a step
Linear Spatial Filters
Sharpening Spatial Filters 93
Conclusion:
• The principal use of first derivatives in image processing is
edge detection.
• In most applications, the second derivatives are better suited
than the first derivatives for image enhancement.
Use of Second Derivatives for Enhancement
The Laplacian 94
• Development of the Laplacian method
– The two dimensional Laplacian operator for
continuous functions:  f  f
2 2
 f  2  2
2

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

Because the Laplacian is a derivative operator, its use


highlights gray-level discontinues.
To sharpen an image, the Laplacian is based on the
equation:
 f ( x, y )   2 f if the center coefficient of the Laplacian mask is negative.
g ( x, y )  
 f ( x, y )   f if the center coefficient of the Laplacian mask is positive.
2

 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

Remaining part is for DIP Graduate students only


Nonlinear Spatial Filtering
101
Nonlinear Spatial Filtering

Matlab functions are nlfilter and colfilt

colfilt:

Given an input image of size MN, and a neighborhood of


size mn
Output image would be of size mnMN
Syntax:
g = colfilt(f, [m n], ‘sliding’, @fun, parameters)
Nonlinear Spatial Filtering
102
colfilt: Creates a Temporary Matrix say A for Sliding Neighborhood. It can
perform similar operations to blkproc and nlfilter, but often executes much faster.
In this figure, a 6-by-5 image matrix is processed in 2-by-3 neighborhoods.
colfilt creates one column for each pixel in the image, so there are a total of 30
columns in the temporary matrix. Each pixel’s column contains the value of the
pixels in its neighborhood, so there are six rows. colfilt zero-pads the input
image as necessary. For example, the neighborhood of the upper left pixel in the
figure has two zero-valued neighbors, due to zero padding.
Nonlinear Spatial Filtering
103
>> f=[1 2; 3 4] Matrix A
f= 0 0 0 1
1 2 0 0 0 0 0 0 1 2
3 4 0 1 2 0 0 0 2 0
>> g = colfilt(f,[3 3],'sliding',@mean)
0 3 4 0 0 1 0 3
% 'sliding' or [mblock nblock]
g= 0 0 0 0 1 2 3 4
1.1111 1.1111 2 0 4 0
1.1111 1.1111 0 3 0 0
3 4 0 0
>> g = colfilt(f,[3 3],'sliding',@gmean)
g= 4 0 0 0
0 0
0 0 % gmean.m
function v = gmean(A)
mn = size(A,1); % The length of the columns of A is always mn
Why? v = prod(A,1).^(1/mn);
Nonlinear Spatial Filtering
104
>> f=[1 2; 3 4]
f=
1 2
3 4
>> fp = padarray(f,[2 2], 'replicate')
fp =
1 1 1 2 2 2
1 1 1 2 2 2
1 1 1 2 2 2
3 3 3 4 4 4
3 3 3 4 4 4
3 3 3 4 4 4
>> g = colfilt(fp,[3 3],'sliding', @mean) >> g = colfilt(fp,[3 3],'sliding', @gmean)
g= g=
0.4444 0.6667 0.8889 1.1111 1.3333 0.8889
0.6667 1.0000 1.3333 1.6667 2.0000 1.3333 0 0 0 0 0 0
1.1111 1.6667 2.0000 2.3333 2.6667 1.7778 0 1.0000 1.2599 1.5874 2.0000 0
1.5556 2.3333 2.6667 3.0000 3.3333 2.2222 0 1.4422 1.7371 2.0922 2.5198 0
2.0000 3.0000 3.3333 3.6667 4.0000 2.6667 0 2.0801 2.3949 2.7574 3.1748 0
1.3333 2.0000 2.2222 2.4444 2.6667 1.7778 0 3.0000 3.3019 3.6342 4.0000 0
0 0 0 0 0 0
Nonlinear Spatial Filtering
105

Example:
f = imread(‘rose.tif');
imshow(f)
g = uint8(colfilt(f,[5 5],'sliding', @mean)); figure, imshow(g)

Exercise: (due in next class)


Modify above example with
(a) geometric mean
(b) max
Nonlinear Spatial Filters
Order Statistics or Ranked Filters 106
Syntax: g = ordfilt2(f, order, domain)

domain is mn matrix of 1s and 0s.


Example: >> g = ordfilt2(f, 1, ones(m, n))
implements a min filter of size mn.

What about?: >> g = ordfilt2(f, m*n, ones(m, n))

Median filter: >> g = ordfilt2(f, median(1:m*n), ones(m, n))

What about?: >> g = ordfilt2(f, 5, ones(3, 3))

Median filtering is a useful tool for reducing salt-and-pepper noise.


Nonlinear Spatial Filters
Order Statistics or Ranked Filters 107
>> 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)

(b) (c) (d)


References
108

1. Gonzalez R. C., Woods R. E., Digital Image Processing, Pearson Education,


Second Edition, 2006.
2. Gonzalez R. C., Woods R. E., Eddins S. L., Digital Image Processing Using
Matlab, Pearson Education, 2006.
3. Institute of Signal Processing, Tampere University of Technology, Finland,
http://www.cs.tut.fi/~karaoglu/
4. MatLab Code download:
https://beagle.whoi.edu/redmine/projects/ifcb-web/repository/revisions/2/entry/t
runk/dipum_toolbox_registered/dipum_1.1.3/
109
M-Functions for Intensity Transformations
(Self Reading)
110
Input and Output Arguments to Functions
Function Description
nargin returns the number of arguments input into the function
nargout returns the number of arguments output from a function
This checks if the input arguments (nargin) are between low and high (inclusive). If
they aren't, then execution stops and an error message (with the string returned
from nargchk) is printed
narginchk( A sample usage of nargchk inside a function is:
low, high) error (nargchk (2, 3, nargin));
Returns Examples
"Not enough input parameters" if number is less than low and returns
"Too many input parameters" if number is greater than high.

varargin accepts a variable number of arguments


varargout returns a variable number of arguments
#M-Functions for Intensity Transformations
111

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);

gamma gamma=imadjust(I,[],[],0.4); gamma=intrans(I,'gamma',0.4);


I2=im2double(I);
contrast-stretching contrast=1./(1+(0.2./(I2+eps)).^5);
contrast=intrans(I,'stretch',0.2,5);
#An M-Function for Intensity Scaling 112

Exercise ([2, page 89]):

Write an M-Function to scale the image to the full,


maximum range, [0, 255] or [0, 65535]. In addition, the
function should map the output levels to a specified range.

Syntax: g = gscale(f, method, low, high)

Valid methods: full8 or full16

You might also like