0% found this document useful (0 votes)
25 views65 pages

Ch03-Intensity Transformations and Spatial Filtering

The document discusses various intensity transformations and spatial filtering techniques in digital image processing. It introduces concepts like image negative, power-law transformations, logarithmic transformations, and contrast stretching. It provides examples of code to apply these transformations using functions like imadjust and imcomplement. It also discusses generating and plotting histograms from images and the technique of histogram equalization to increase contrast.

Uploaded by

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

Ch03-Intensity Transformations and Spatial Filtering

The document discusses various intensity transformations and spatial filtering techniques in digital image processing. It introduces concepts like image negative, power-law transformations, logarithmic transformations, and contrast stretching. It provides examples of code to apply these transformations using functions like imadjust and imcomplement. It also discusses generating and plotting histograms from images and the technique of histogram equalization to increase contrast.

Uploaded by

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

Digital Image Processing, 2nd ed.

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

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 3
www.imageprocessingbook.com

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

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 4
www.imageprocessingbook.com

Background

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


© 2002 R. C. Gonzalez & R. E. Woods
Digital Image Processing, 2nd ed. 5
www.imageprocessingbook.com

Intensity Transformation Functions

1. Image Negative
2. Power-Law
3. Logarithmic
4. Contrast Stretching

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 6
www.imageprocessingbook.com

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

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 7
www.imageprocessingbook.com

Power-Law Transformations
Useful for brightening the
darkest regions or
darkening the brightest
regions in an image.

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 8
www.imageprocessingbook.com

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

© 2002 R. C. Gonzalez & R. E. Woods d e f


Digital Image Processing, 2nd ed. 9
www.imageprocessingbook.com

Logarithmic Transformations

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 10
www.imageprocessingbook.com

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

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 12
www.imageprocessingbook.com

M-Functions for Intensity Transformations

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
nargchk( from nargchk) is printed
A sample usage of nargchk inside a function is:
low, high, error (nargchk (2, 3, nargin));
number) 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
© 2002 R. C. Gonzalez & R. E. Woods
Digital Image Processing, 2nd ed. 13
www.imageprocessingbook.com

M-Functions for Intensity Transformations


Assignment # 1 (Due on coming Monday)
Write a function which implements all of the intensity transformations
mentioned below. The prototype of this function is given below:
function g = intrans(f, varargin)
•Apply this function on three images
•For each image apply all the four transformations Transformation
•Provide a report containing
• Code photographic negative
• Original Images
logarithmic
• Results of transformations with parameters.
•Apply All the four transformations to three images gamma
• Spectrum.tif
• Grain.tif contrast-stretching
• Choose one by your self

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 14
www.imageprocessingbook.com

An M-Function for Intensity Scaling

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

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 15
www.imageprocessingbook.com

Histogram Processing & Function Plotting

Generating & Plotting Image Histograms:

h(rk) = nk

Normalized Histogram:
p(rk) = h(rk)/n
= nk/n, (Relative frequency)

>> imhist(f);

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 16
www.imageprocessingbook.com

Histogram Equalization
Histogram equalization attempts to obtain an even distribution of
intensity values in the image, often resulting in increased contrast.

Original image: Histogram equalized image:

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 17
www.imageprocessingbook.com

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

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 18
www.imageprocessingbook.com

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

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]

* grey level order will only be preserved if the


transformation is monotone increasing
© 2002 R. C. Gonzalez & R. E. Woods
Digital Image Processing, 2nd ed. 22
www.imageprocessingbook.com

Histogram Equalization
>> f = imread('pollen.tif');
>> g = histeq(f, 256);
>> imshow(f), figure, imshow(g)

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 23
www.imageprocessingbook.com

Histogram Matching or Specification

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

We skip the details of implementation.

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 24
www.imageprocessingbook.com

Histogram Matching or Specification

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 25
www.imageprocessingbook.com

Histogram Matching or Specification

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

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 26
www.imageprocessingbook.com

Histogram Matching or Specification

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 27
www.imageprocessingbook.com

Histogram Matching or Specification

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 28
www.imageprocessingbook.com

Histogram Matching or Specification


Lookup Table

Resulting Histogram

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 29
www.imageprocessingbook.com

Spatial Filtering (Neighborhood Processing)

• 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

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 30
www.imageprocessingbook.com

Basics of Spatial Filtering

• 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 MXN with a filter
mask of size mn is given by a b
g ( x, y )    w(s, t ) f ( x  s, y  t )
s  a t b
where a=(m-1)/2 and b=(n-1)/2
• This concept is called convolution. Filter masks are sometimes
called convolution masks or convolution kernels.
© 2002 R. C. Gonzalez & R. E. Woods
Digital Image Processing, 2nd ed. 31
www.imageprocessingbook.com

Basics of Spatial Filtering

• Nonlinear spatial filtering usually uses a


neighborhood too, but some other mathematical
operations are used. These can include conditional
operations (if …, then …), statistical (sorting pixel
values in the neighborhood, mean, median), etc.
• Because the neighborhood includes pixels on all sides
of the center pixel, some special procedure must be
used along the top, bottom, left and right sides of the
image so that the processing does not try to use pixels
that do not exist.

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 32
www.imageprocessingbook.com

Smoothing Spatial Filters


• Smoothing linear filters
– Averaging filters (Lowpass filters in Chapter 4))
• Box filter
• Weighted average filter

Box filter Weighted average


© 2002 R. C. Gonzalez & R. E. Woods
Digital Image Processing, 2nd ed. 33
www.imageprocessingbook.com

Smoothing Spatial Filters


• 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

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 34
www.imageprocessingbook.com

Spatial Filtering (Neighborhood Processing)

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

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 35
www.imageprocessingbook.com

Spatial Filtering (Neighborhood Processing)

inear 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


© 2002 R. C. Gonzalez & R. E. Woods
Digital Image Processing, 2nd ed. 36
www.imageprocessingbook.com

Spatial Filtering (Neighborhood Processing)


Linear Spatial Filtering

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

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 37
www.imageprocessingbook.com

Spatial Filtering (Neighborhood Processing)


Linear Spatial Filtering
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

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 38
www.imageprocessingbook.com

Spatial Filtering (Neighborhood Processing)


Linear Spatial Filtering
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

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 39
www.imageprocessingbook.com

Spatial Filtering (Neighborhood Processing)


Linear Spatial Filtering
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

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 40
www.imageprocessingbook.com

Spatial Filtering (Neighborhood Processing)


Linear Spatial Filtering
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

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 41
www.imageprocessingbook.com

Spatial Filtering (Neighborhood Processing)


Linear Spatial Filtering
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

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 42
www.imageprocessingbook.com

Spatial Filtering (Neighborhood Processing)


Linear Spatial Filtering
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

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 43
www.imageprocessingbook.com

Spatial Filtering (Neighborhood Processing)


Linear Spatial Filtering

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

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 44
www.imageprocessingbook.com

Spatial Filtering (Neighborhood Processing)


Linear Spatial Filtering

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

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 45
www.imageprocessingbook.com

Spatial Filtering (Neighborhood Processing)

near Spatial Filtering


Padding
Correlation
Convolution
Same
Full

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 46
www.imageprocessingbook.com

Spatial Filtering (Neighborhood Processing)

near Spatial Filtering

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 47
www.imageprocessingbook.com

Spatial Filtering (Neighborhood Processing)

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'
© 2002 R. C. Gonzalez & R. E. Woods
Digital Image Processing, 2nd ed. 48
www.imageprocessingbook.com

Spatial Filtering (Neighborhood Processing)

inear 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, [ ])
© 2002 R. C. Gonzalez & R. E. Woods
Digital Image Processing, 2nd ed. 49
www.imageprocessingbook.com

Spatial Filtering (Neighborhood Processing)

inear Spatial Filtering:

Exercise: Study & visualize the difference between different


boundary options:
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.

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 50
www.imageprocessingbook.com

Spatial Filtering (Neighborhood Processing)

onlinear 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)
© 2002 R. C. Gonzalez & R. E. Woods
Digital Image Processing, 2nd ed. 51
www.imageprocessingbook.com

Spatial Filtering (Neighborhood Processing)

colfilt: Creates a Temporary Matrix for Sliding Neighborhood


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.

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 52
www.imageprocessingbook.com

Spatial Filtering (Neighborhood Processing)

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

>> g = colfilt(f,[3 3],'sliding',@gmean)


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

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 53
www.imageprocessingbook.com

Spatial Filtering (Neighborhood Processing)


>> f=[1 2; 3 4]
Example & Exercise:
f=
1 2
f = imread(‘rose.tif');
3 4
imshow(f)
>> fp = padarray(f,[2 2], 'replicate') g = uint8(colfilt(f,[5 5],'sliding', @mean));
fp = figure, imshow(g)
1 1 1 2 2 2
1 1 1 2 2 2
1 1 1 2 2 2 Try with geometric mean (@gmean)
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 0 0 0 0 0
0.6667 1.0000 1.3333 1.6667 2.0000 1.3333 0 1.0000 1.2599 1.5874 2.0000 0
1.1111 1.6667 2.0000 2.3333 2.6667 1.7778 0 1.4422 1.7371 2.0922 2.5198 0
1.5556 2.3333 2.6667 3.0000 3.3333 2.2222 0 2.0801 2.3949 2.7574 3.1748 0
2.0000 3.0000 3.3333 3.6667 4.0000 2.6667 0 3.0000 3.3019 3.6342 4.0000 0
1.3333 2.0000 2.2222 2.4444 2.6667 1.7778 0 0 0 0 0 0
© 2002 R. C. Gonzalez & R. E. Woods
Digital Image Processing, 2nd ed. 54
www.imageprocessingbook.com

Exercises
1. Do practice from [3]
2. Solve questions [2]: 3.1, 3.2, 3.5, 3.6, 3.21, 3.22

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 55
www.imageprocessingbook.com

Linear Spatial Filters


Sharpening Spatial Filters

• 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

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 56
www.imageprocessingbook.com

Sharpening Spatial Filters


An Example

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 57
www.imageprocessingbook.com

Linear Spatial Filters


Sharpening Spatial Filters

Summary: [2, pp. 149]


• First-order derivatives:
– Generally produce thicker edges in an image
– Generally have a stronger response to a gray-level step
• 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
Conclusion:
• In most applications, the second derivatives are better suited
than the first derivatives for image enhancement.
• The principal use of first derivatives in image processing is
edge detection.
© 2002 R. C. Gonzalez & R. E. Woods
Digital Image Processing, 2nd ed. 58
www.imageprocessingbook.com

Use of Second Derivatives for Enhancement


The Laplacian
• 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 )
© 2002 R. C. Gonzalez & R. E. Woods
Digital Image Processing, 2nd ed. 59
www.imageprocessingbook.com

Use of Second Derivatives for Enhancement


The Laplacian

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 60
www.imageprocessingbook.com

Use of Second Derivatives for Enhancement


(The Laplacian)

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 coefficien t of the Laplacian mask is negative.
g ( x, y )  
 f ( x, y )   f if the center coefficien t 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 
© 2002 R. C. Gonzalez & R. E. Woods
1  1  1 
Digital Image Processing, 2nd ed. 61
www.imageprocessingbook.com

Use of Second Derivatives for Enhancement


(The Laplacian)

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)

© 2002 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 2nd ed. 62
www.imageprocessingbook.com

Use of Second Derivatives for Enhancement


(The Laplacian)

Original Enhanced using the Laplacian Enhanced using the Laplacian


filter with -4 in the center filter with -8 in the center

Image of the north pole of the moon - Courtesy of NASA


© 2002 R. C. Gonzalez & R. E. Woods
Digital Image Processing, 2nd ed. 63
www.imageprocessingbook.com

Nonlinear Spatial Filters


Order Statistics or Ranked Filters
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.


© 2002 R. C. Gonzalez & R. E. Woods
Digital Image Processing, 2nd ed. 64
www.imageprocessingbook.com

Nonlinear Spatial Filters


Order Statistics or Ranked Filters

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

© 2002 R. C. Gonzalez & R. E. Woods


(b) (c) (d)
Digital Image Processing, 2nd ed. 65
www.imageprocessingbook.com

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/

© 2002 R. C. Gonzalez & R. E. Woods

You might also like