0% found this document useful (0 votes)
100 views98 pages

07 Spatial Filtering - PDF

The document discusses spatial filtering of digital images. Spatial filtering involves applying a filter or mask over an image to enhance it. The filter considers pixels in a neighborhood around each pixel. Common filters include averaging filters for blurring images. The output pixel value depends on weighted sums of the pixel and its neighbors based on the filter coefficients. Filters are commonly 3x3 but can be larger. Border issues are addressed through techniques like zero padding.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
100 views98 pages

07 Spatial Filtering - PDF

The document discusses spatial filtering of digital images. Spatial filtering involves applying a filter or mask over an image to enhance it. The filter considers pixels in a neighborhood around each pixel. Common filters include averaging filters for blurring images. The output pixel value depends on weighted sums of the pixel and its neighbors based on the filter coefficients. Filters are commonly 3x3 but can be larger. Border issues are addressed through techniques like zero padding.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 98

National University of Sciences and

Technology (NUST)
School of Electrical Engineering and Computer Science
(SEECS)

Digital Image Processing

1 Khawar Khurshid
Spatial Filtering

2 Khawar Khurshid
Spatial Filtering - Basics

Origin x

Neighbourhood operations:

Operate on a larger neighbourhood


of pixels than point operations
(x, y)
Neighbourhood

Neighbourhoods are mostly a


rectangle around a central pixel

y
Image f (x, y)

3 Khawar Khurshid
Spatial Filtering - Basics

Original Image Enhanced Image


x x
123 127 128 119 115 130

140 145 148 153 167 172

133 154 183 192 194 191

194 199 207 210 198 195

164 170 175 162 173 151

y y

4 Khawar Khurshid
Spatial Filtering - Basics
Origin x
a b c r s t
d
g
e
h
f
i
* u
x
v
y
w
z
Original Image Filter
Simple 3*3
e 3*3 Filter Pixels
Neighbourhood

eprocessed = v*e +
r*a + s*b + t*c +
y Image f (x, y) u*d + w*f +
x*g + y*h + z*i

The above is repeated for every pixel in the original image to generate the filtered image
5 Khawar Khurshid
Spatial Filtering - Basics

6 Khawar Khurshid
Spatial Filtering - Basics

7 Khawar Khurshid
Spatial Filtering - Basics

8 Khawar Khurshid
Spatial Filtering - Basics

9 Khawar Khurshid
Spatial Filtering - Basics

10 Khawar Khurshid
Spatial Filtering - Basics

11 Khawar Khurshid
Spatial Filtering - Basics
Mask operation near the image border
Problem arises when part of the mask is located outside the
image plane
Pixel replication:

Discard the problem pixels (e.g. 512x512 We normally use the gray levels
input 510x510 output if mask size is 3x3) of border pixels to fill up the
expanded region (for 3x3
mask). For larger masks a
border region equal to half of
Zero padding: Expand the input image by the mask size is mirrored on
padding zeros (512x512 original image, the expanded region.
514x514 padded image, 512x512 output)
Customized Processing:
Zero padding is not recommended as it
Using only those coefficients
creates artificial lines or edges on the of the mask which overlap with
border the image pixels on the borders
and ignoring the others.

12 Khawar Khurshid
Spatial Filtering - Basics

13 Khawar Khurshid
Spatial Filtering - Basics

 The output intensity value at (x,y) depends not only on the


input intensity value at (x,y) but also on the specified
number of neighboring intensity values around (x,y)

 Spatial masks (also called window, filter, kernel, template)


are used and convolved over the entire image for
enhancement (spatial filtering)

 The size of the masks determines the number of


neighboring pixels which influence the output value at (x,y)

 The values (coefficients) of the mask determine the


nature and properties of enhancing technique

14 Khawar Khurshid
Spatial Filtering - Basics

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

m 1 n 1
where a = , b=
2 2
x  0,1, 2,...., M  1, y  0,1, 2,..., N  1

Filtering can be given in equation


form as shown above

15 Khawar Khurshid
Spatial Filtering - Basics

 Given the 3×3 mask with coefficients: w1, w2,…, w9


 The mask cover the pixels with gray levels: z1, z2,…, z9

w1 w2 w3 z1 z2 z3
w4 w5 w6 z4 z5 z6
w7 w8 w9 z7 z8 z9

9
z 
 z1w1  z2 w2  z3 w3   z9 w9   zi wi
i 1

 z gives the output intensity value for the processed image (to be
stored in a new array) at the location of z5 in the input image.
16 Khawar Khurshid
Spatial Filtering - Basics

 For blurring/noise reduction

 Blurring is usually used in preprocessing steps, e.g., to remove


small details from an image prior to object extraction, or to
bridge small gaps in lines or curves

 Equivalent to Low-pass spatial filtering in frequency domain


because smaller (high frequency) details are removed based on
neighborhood averaging (averaging filters)

17 Khawar Khurshid
Spatial Filtering - Basics

 Simply average all of the pixels in a neighborhood around a


central value

1/ 1/ 1/
9 9 9

1/ 1/ 1/
9 9 9

1/ 1/ 1/
9 9 9

Simple averaging filter


18 Khawar Khurshid
Spatial Filtering - Basics
Origin x
104 100 108 1/ 1/ 1/
9 9 9

99 106 98

95 90 85
* 1/

1/
9
1/

1/
9
1/

1/
9

9 9 9

1/ 100
104
9
1/ 108
9
1/
9
Original Image Filter
Simple 3*3 1/ 1/ 1/
3*3 Smoothing Pixels
999 106
9 989
Neighbourhood 195
/9 190
/9 185
/9
Filter

e = 1/9*106 +
1/ *104 + 1/ *100 + 1/ *108 +
9 9 9
1/ *99 + 1/ *98 +
y Image f (x, y) 9 9
1/ *95 + 1/ *90 + 1/ *85
9 9 9
= 98.3333
The above is repeated for every pixel in the original image to generate the smoothed image
19 Khawar Khurshid
Smoothing Filter

Consider the
output pixel is
positioned at
the center

Box Filter all Weighted Average give


coefficients are more (less) weight to near
equal (away from) the output
location

20 Khawar Khurshid
Smoothing Filter - Example

original 3x3 average

21 Khawar Khurshid
Smoothing Filter - Example

original 3x3 average

22 Khawar Khurshid
Smoothing Filter - Example

original 3x3 average

23 Khawar Khurshid
Smoothing Filter - Example

original 3x3 average

24 Khawar Khurshid
Smoothing Filter - Example

Original
image Smooth by 3x3
Size: box filter
500x500

Smooth by Smooth by 9x9


5x5 box box filter
filter

Smooth by Smooth by 35x35


15x15 box box filter
filter
Notice how detail begins to disappear
25 Khawar Khurshid
MATLAB - Exercise

• Smoothing filters
– Implement a box filter
– Add noise yourself

N = imnoise(I,'salt & pepper',0.02);

26 Khawar Khurshid
MATLAB - Exercise

clear all;
II=imread('rain.jpg');
I=rgb2gray(II);
[x,y] = size(I)

I=double(I)/255;
I = imnoise(I,'salt & pepper',0.02);

w=(ones(3,3));
O=ones(x,y);
Or use the built in conv2 function
for i=2:x-2
for j=2:y-2
O(i,j)=1/9*(w(1,1)*I(i-1,j-1)+w(1,2)*I(i-1,j)+w(1,3)*I(i-1,j+1)+w(2,1)*I(i,j-1) +
w(2,2)*I(i,j)+w(2,3)*I(i,j+1)+w(3,1)*I(i+1,j-1)+w(3,2)*I(i+1,j) +w(3,3)*I(i+1,j+1));
end
end
27 Khawar Khurshid
Sharpening Filters

Previously we have looked at smoothing filters which


remove fine detail.

Sharpening spatial filters seek to highlight fine detail

 Remove blurring from images

 Highlight edges

Sharpening filters are based on spatial differentiation

28 Khawar Khurshid
Spatial Differentiation

• Let’s consider a simple 1 dimensional example

A B

29 Khawar Khurshid
Spatial Differentiation

A B

30 Khawar Khurshid
Spatial Differentiation

The 1st derivative of a function is given by:

f
 f ( x  1)  f ( x)
x
Its just the difference between subsequent values and
measures the rate of change of the function.

31 Khawar Khurshid
Spatial Differentiation
Image Strip

8
7
6
5
4
3
1st Derivative
2
1
0

5 5 4 3 2 1 0 0 0 6 0 0 0 0 1 3 1 0 0 0 0 7 7 7 7

-1 -1 -1 -1 -1 0 0 6 -6 01st 0Derivative
0 1 2 -2 -1 0 0 0 7 0 0 0

8
6
4
2
0
-2
-4
-6
32 -8
Khawar Khurshid
Spatial Differentiation

The 2nd derivative of a function is given by:

 f
2
 f ( x  1)  f ( x  1)  2 f ( x)
 x
2

Simply takes into account the values both before and


after the current value

33 Khawar Khurshid
Spatial Differentiation
Image Strip

8
7
6
5
4
3
2nd Derivative
2
1
0

5 5 4 3 2 1 0 0 0 6 0 0 0 0 1 3 1 0 0 0 0 7 7 7 7

-1 0 0 0 0 1 0 6 -12 6 0 0 1 1 -4 1 1 0 0 7 -7 0 0
2nd Derivative

10

-5

-10

34 -15
Khawar Khurshid
2nd Derivative – Image Enhancement

The 2nd derivative is more useful for image enhancement


than the 1st derivative - Stronger response to fine detail

We will come back to the 1st order derivative later on

The first sharpening filter we will look at is the Laplacian

35 Khawar Khurshid
Laplacian Filter

The Laplacian is defined as follows:

 f  f 2 2
 f  2  2
2

 x  y
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

36 Khawar Khurshid
Laplacian Filter

So, the Laplacian can be given as follows:

 2 f  [ f ( x  1, y)  f ( x  1, y )
 f ( x, y  1)  f ( x, y  1)]
 4 f ( x, y)

0 1 0
Can we implement
it using a mask? 1 -4 1

0 1 0
37 Khawar Khurshid
Laplacian Filter

38 Khawar Khurshid
Laplacian Filter – Image Enhancement

The result of a Laplacian filtering is not an enhanced image

To generate the final enhanced image

f ( x, y )   2 f , w5  0
g ( x, y ) 
f ( x, y )   2 f , w5  0

39 Khawar Khurshid
Laplacian Filter – Image Enhancement

The entire enhancement can be combined into a single


filtering operation.

g ( x, y)  f ( x, y)   2 f

 f ( x, y)  [ f ( x  1, y)  f ( x  1, y)
 f ( x, y  1)  f ( x, y  1)
0 1 0
 4 f ( x, y)]
1 -4 1

0 1 0
40 Khawar Khurshid
Laplacian Filter – Image Enhancement

The entire enhancement can be combined into a single


filtering operation

g ( x, y)  f ( x, y)   f 2

 5 f ( x, y)  f ( x  1, y)  f ( x  1, y)
 f ( x, y  1)  f ( x, y  1)
0 -1 0

-1 5 -1

0 -1 0
41 Khawar Khurshid
Laplacian Filter – Image Enhancement

This gives us a new filter which does the whole job for
us in one step.

0 -1 0

-1 5 -1

0 -1 0

42 Khawar Khurshid
Laplacian Filter – Image Enhancement

Applying the Laplacian to an image we get a new image


that highlights edges and other discontinuities

Original Laplacian
43 Image Filtered Image
Khawar Khurshid
Laplacian Filter – Image Enhancement

+ =
Original Laplacian Sharpened
Image Filtered Image Image

In the final sharpened image edges and fine detail are


much more obvious

44 Khawar Khurshid
Laplacian Filter – Image Enhancement

45 Khawar Khurshid
Laplacian Filter – Image Enhancement

46 Khawar Khurshid
Laplacian Filter – Image Enhancement

47 Khawar Khurshid
Gradient Operators

Simplest operator

f f
 ( z8  z5 ),  ( z6  z5 )
x y
f  ( z8  z5 ) 2  ( z6  z5 ) 2
f  ( z8  z5 )  ( z6  z5 )

48 Khawar Khurshid
Gradient Operators

Prewitt operator

f  ( z7  z8  z9 )  ( z1  z2  z3 )
 ( z3  z6  z9 )  ( z1  z4  z7 )

-1 -1 -1 -1 0 1
f f
 0 0 0  -1 0 1
y 1 1 1
x -1 0 1
Extract horizontal edges Extract vertical edges

49 Khawar Khurshid
Gradient Operators

Sobel operator

-1 0 1
f -1 -2 -1
f
 0 0 0  -2 0 2
y x -1 0 1
1 2 1
Extract horizontal edges Extract vertical edges

Emphasize more the current point (x direction)

f  ( z7  2 z8  z9 )  ( z1  2 z2  z3 )
 ( z3  2 z6  z9 )  ( z1  2 z4  z7 )

Emphasize more the current point (y direction) Pixel Arrangement


50 Khawar Khurshid
Gradient Operators

Sobel Operator along with the threshold.

An image of a
contact lens which
is enhanced in
order to make
defects more
obvious

51 Khawar Khurshid
Combining Spatial Enhancement Methods

Successful image enhancement is


typically not achieved using a single
operation

Rather we combine a range of


techniques in order to achieve a final
result

This example will focus on enhancing


the bone scan

52 Khawar Khurshid
Combining Spatial Enhancement Methods

(a)
Laplacian filter of
bone scan (a) (b)
Sharpened version of
bone scan achieved
(c)
by subtracting (a)
and (b) Sobel filter of bone
scan (a) (d)
53 Khawar Khurshid
Combining Spatial Enhancement Methods

Result of applying a (h)


power-law trans. to
Sharpened image (g)
which is sum of (a)
and (f) (g)
The product of (c)
and (e) which will be (f)
used as a mask
(e)

Image (d) smoothed with


54 a 5*5 averaging filter Khawar Khurshid
Combining Spatial Enhancement Methods

Original Enhanced
55 Khawar Khurshid
Noise Models

Most types of noise are modeled as known probability density functions.


Noise model is decided based on understanding of the physics of the sources
of noise.
Gaussian: Poor illumination
Impulse: Faulty Pixels
Uniform: Quantization
Parameters can be estimated based on histogram on small flat area of an
image.

56 Khawar Khurshid
Noise Models

57 Khawar Khurshid
Noise Models

58 Khawar Khurshid
Noise Filters

Spatial Domain for Noise Removal without caring for


Degradation
Mean Filters
Order Statistics Filter
Adaptive Filter (based on local statistics)

Frequency Domain
Band Reject/Notch Filtering

59 Khawar Khurshid
Noise Filters

Output is based on order of gray levels in the masked area.

Order filters
- Minimum
- Maximum
- Median
- Mid-Point
- Alpha-Trimmed

Mean filters
- Arithmetic Mean
- Geometric Mean
- Contra-Harmonic Mean
60 Khawar Khurshid
Order Filters

• Given an NxN window, the pixel values can be


ordered from smallest to largest as follows:

– I1 I2  I3..... IN2


– Where {I1,I2,I3,.....,IN2} are the gray-level values of the subset
of pixels in the image, that are in the NxN window.

• Different types of order filters select different values


from the ordered pixel list.

61 Khawar Khurshid
Order Filters
• Median filter
– Select the middle pixel value from the ordered set.
– Used to remove salt-and-pepper noise.
• Maximum filter
– Select the highest pixel value from the ordered set.
– Remove pepper-type noise.
• Minimum filter
– Select the lowest pixel value from the ordered set.
– Remove salt-type noise.
• As the size of the window gets bigger, the more information
loss occurs.
– With windows larger than about 5x5, the image acquires an
artificial, “painted”, effect.

62 Khawar Khurshid
Order Filters

Minimum Filter

Image with salt noise Result of minimum filtering


Probability = .04 Mask 3 x 3
63 Khawar Khurshid
Order Filters

Minimum filtering Minimum filtering


Mask 5 x 5 Mask 9 x 9
64 Khawar Khurshid
Order Filters

Maximum Filter

Image with pepper noise Maximum filtering


Probability = .04 Mask 3 x 3
65 Khawar Khurshid
Order Filters

Maximum filtering Maximum filtering


Mask 5 x 5 Mask 9 x 9
66 Khawar Khurshid
Order Filters

• Order filters can also be defined to select a specific pixel


rank within the ordered set.

– For example, we may find the second highest value is


the better choice than the maximum value for certain
pepper noise.
– This type of ordered selection is application specific.

• Minimum filter tend to darken the image and maximum


filter tend to brighten the image.

67 Khawar Khurshid
Median Filtering

Sort the values


Determine the median

Median = ? 20

 Particularly effective when


 The noise pattern consists of strong impulse noise
( salt-and-pepper)
68 Khawar Khurshid
Median Filtering - Application

Removal of Salt and Pepper Noise.

69 Khawar Khurshid
Mean-Median Filter - comparison

70 Khawar Khurshid
Mean-Median Filter - comparison

Noisy Original

71 Khawar Khurshid
Mean-Median Filter - comparison

Noisy Noisy

72 Khawar Khurshid
Mean-Median Filter - comparison

3x3-blur x 1 3x3-median x 1

73 Khawar Khurshid
Mean-Median Filter - comparison

3x3-blur x 2 3x3-median x 2

74 Khawar Khurshid
Mean-Median Filter - comparison

3x3-blur x 5 3x3-median x 5

75 Khawar Khurshid
Mean-Median Filter - comparison

3x3-blur x 10 3x3-median x 10

76 Khawar Khurshid
Order Filters

Midpoint filter

– Average of the maximum and minimum within


the window.
I1  I N 2
Midpoint =
2

77 Khawar Khurshid
Order Filters

Image with Gaussian noise Result of midpoint filter


Mask size = 3
78 Khawar Khurshid
Order Filters

Image with uniform noise Result of midpoint filter


Mask size = 3
79 Khawar Khurshid
Order Filters

• Alpha-trimmed mean filter


– The average of the pixel values within the window, but with some
endpoint-ranked values excluded.
N 2 T
1
Alpha-trimmed mean =
N 2  2T
I
i T 1
i

– T is the number of pixels excluded at each end of the ordered set


• The alpha-trimmed mean filter ranges from a mean to median
filter, depending on the value selected for the T parameter.

If T = 0,  mean filter.
If T = (N2 – 1) / 2,  median filter.

80 Khawar Khurshid
Order Filters

Image with Gaussian and salt Result of alpha-trimmed mean filter


pepper noise. Mask size = 3
Trim size = 0

81 Khawar Khurshid
Order Filters

Result of alpha-trimmed mean filter Result of alpha-trimmed mean filter


Mask size = 3 Mask size = 3
Trim size = 1 Trim size = 4

82 Khawar Khurshid
Mean Filters

• The mean filters function by finding some form of


an average within the NxN window.

• The most basic of these filters is the arithmetic


mean filter.

– This filter mitigates the noise effect, but at the same


time tend to blur the image.
– The blurring effect is not desirable, and therefore other
mean filters are designed to minimize this loss of detail
information.

83 Khawar Khurshid
Mean Filters

• Arithmetic mean filter


– Find the arithmetic average of the pixel values in the
window.

1
Arithmetic Mean =
N2
 d ( r, c )
( r ,c )w

– Smooth out local variations in an image.


– Tend to blur the image.

84 Khawar Khurshid
Mean Filters

Image with Gaussian noise Result of arithmetic mean filter


Mask size = 3

85 Khawar Khurshid
Mean Filters

Result of arithmetic mean filter Result of arithmetic mean filter


Mask size = 5 Mask size = 9

86 Khawar Khurshid
Mean Filters

• Geometric mean filter

 g (r , c)
1
Geometric Mean = N2
( r ,c )w

– Retains detail better than arithmetic mean filter.


– Drawback?

– Ineffective in the presence of pepper noise (if very low


values present in the window, the equation will return a
very small number).

87 Khawar Khurshid
Mean Filters

Image with salt noise Result of geometric filter


Probability=.04 Mask size = 3
88 Khawar Khurshid
Mean Filters

Image with pepper noise Result of geometric filter


Probability = .04 Mask size = 3

89 Khawar Khurshid
Mean Filters

• Contra-harmonic mean filter


 g (r , c)
( r ,c )w
R 1

Contra-Harmonic Mean =
 g (r , c)
( r ,c )w
R

– Works for salt OR pepper noise, depending on the


filter order R.
– Positive R  Eliminate pepper-type noise.
– Negative R  Eliminate salt-type noise.

90 Khawar Khurshid
Mean Filters

Image with pepper noise Result of contra-harmonic filter


Probability = .04 Mask size = 3; order = 0

91 Khawar Khurshid
Mean Filters

Result of contra harmonic filter Result of contra harmonic filter


Mask size = 3; order = +1 Mask size = 3; order = +5

92 Khawar Khurshid
Mean Filters

Image with salt noise Result of contra-harmonic filter


Probability = .04 Mask size = 3; order = 0

93 Khawar Khurshid
Mean Filters

Result of contra-harmonic filter Result of contra-harmonic filter


Mask size = 3; order = -1 Mask size = 3; order = -5

94 Khawar Khurshid
Mean Filters

• Harmonic mean filter


N2
Harmonic Mean =
1

( r ,c )w g ( r , c )

– By using the Value of R = -1 in the contra-


harmonic filter.

95 Khawar Khurshid
Mean Filters

Image with pepper noise Result of harmonic filter


Probability = .04 Mask size = 3

96 Khawar Khurshid
Mean Filters

Image with salt noise Result of harmonic filter


Probability=.04 Mask size = 3

97 Khawar Khurshid
End
Spatial Filtering

98 Khawar Khurshid

You might also like