0% found this document useful (0 votes)
7 views12 pages

07 - Spatial Filtering

The document discusses spatial filtering techniques which operate on images by taking intensity values from a neighborhood of each pixel. Linear spatial filtering obtains the filtered pixel as a weighted average of pixels in the neighborhood. Correlation and convolution are described as performing linear filtering using different alignments of the weight matrix and image. Vector representations of filters are also introduced.
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)
7 views12 pages

07 - Spatial Filtering

The document discusses spatial filtering techniques which operate on images by taking intensity values from a neighborhood of each pixel. Linear spatial filtering obtains the filtered pixel as a weighted average of pixels in the neighborhood. Correlation and convolution are described as performing linear filtering using different alignments of the weight matrix and image. Vector representations of filters are also introduced.
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/ 12

.

Spatial filtering

Stefano Ferrari

Università degli Studi di Milano


[email protected]

Methods for Image Processing

academic year 2022–2023

Filtering

I The term filtering refers to a technique developed for the


frequency domain.
I Rapid variations of intensity can be associated to high
frequency components, while slow changes can be associated
to low frequency components.
I The effects of the filtering process is the attenuation or the
enhancement of the components in a target interval of
frequency.

Stefano Ferrari— Methods for Image processing— a.y. 2022/23 1


.

Spatial filtering

I Similar effects can be obtained also in the spatial domain:


techniques of this kind are defined as spatial filtering.
I In general, the spatial filtering techniques operate on an image
taking into consideration the intensity values in an suitable
neighborhood of each pixel.
I For each pixel of the original image, the intensity of the
corresponding pixel in the “filtered” image is computed.
I The transformation rule is often described by a matrix, called
filter, with the same size of the considered neighborhood.
I If the transforming rule is a linear function of the intensities in
the considered neighborhood, the technique is called spatial
linear filtering (non-linear, otherwise).

Linear filtering

Stefano Ferrari— Methods for Image processing— a.y. 2022/23 2


.

Linear filtering (2)

I The pixel (x, y ) in the filtered image, g , is obtained as the


weighted average of the pixels in the original image, f , of a
suitable neighborhood of (x, y ):
a
X b
X
g (x, y ) = w (s, t) f (x + s, y + t)
s=−a t=−b

I The weight matrix, w , is called filter.


I Or mask, template, window.
I For convenience, often a matrix with an odd number of rows
2a + 1, and columns, 2b + 1, is used.

Correlation and convolution

I Correlation
a
X b
X
g (x, y ) = w (x, y )?f (x, y ) = w (s, t) f (x +s, y +t)
s=−a t=−b

I Convolution
a
X b
X
g (x, y ) = w (x, y )∗f (x, y ) = w (s, t) f (x −s, y −t)
s=−a t=−b

I Vectorial representation
mn
X
R = w1 z1 + · · · + wmn zmn = wk zk = w T z
k=1

Stefano Ferrari— Methods for Image processing— a.y. 2022/23 3


.

Correlation

f w
0 0 0 1 0 0 0 0 1 2 3 2 8


f 0 0 0 1 0 0 0 0
w 1 2 3 2 8
↑ starting position alignment

zero padding
f 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
w 1 2 3 2 8

Correlation (2)

f 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
w 1 2 3 2 8
w ?f 0

f 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
w 1 2 3 2 8
w ?f 0 0

f 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
w 1 2 3 2 8
w ?f 0 0 0

f 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
w 1 2 3 2 8
w ?f 0 0 0 8

Stefano Ferrari— Methods for Image processing— a.y. 2022/23 4


.

Correlation (3)

f 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
w 1 2 3 2 8
w ?f 0 0 0 8

f 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
w 1 2 3 2 8
w ?f 0 0 0 8 2

f 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
w 1 2 3 2 8
w ?f 0 0 0 8 2 3

f 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
w 1 2 3 2 8
w ?f 0 0 0 8 2 3 2

Correlation (4)

f 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
w 1 2 3 2 8
w ?f 0 0 0 8 2 3 2 1

...
f 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
w 1 2 3 2 8
w ?f 0 0 0 8 2 3 2 1 0 0 0 0

w ?f 0 0 0 8 2 3 2 1 0 0 0 0
Cropping 0 8 2 3 2 1 0 0
f 0 0 0 1 0 0 0 0

Stefano Ferrari— Methods for Image processing— a.y. 2022/23 5


.

Convolution

f w rotated 180◦
0 0 0 1 0 0 0 0 8 2 3 2 1


f 0 0 0 1 0 0 0 0
w 8 2 3 2 1
↑ starting position alignment

...
w ∗f 0 0 0 1 2 3 2 8 0 0 0 0
Cropping 0 1 2 3 2 8 0 0
f 0 0 0 1 0 0 0 0

Correlation and convolution on images (1)

Stefano Ferrari— Methods for Image processing— a.y. 2022/23 6


.

Correlation and convolution on images (2)

Vectorial representation

I When relative position of the coefficients is not important, it


is more convenient to represent the effect of the filter using a
vectorial representation.
I A conventional indexing have to be specified.

mn
X
R = w1 z1 + · · · + wmn zmn = wk zk = w T z
k=1

Stefano Ferrari— Methods for Image processing— a.y. 2022/23 7


.

Filter specification

I The filter coefficients have to be specified.


I Their values depend on the desired effect.
I Direct specification:
9
1X 1
R= zi ⇒ wi = , ∀i
9 9
i=1

I Function based specification:


x 2 +y 2
h(x, y ) = e − 2σ 2 ⇒ w (s, t) = h(s, t)
I Algorithmic specification
I Non linear filters
I e.g., “max” filter

Smoothing filters

I Results in defocused images (blur).


I Small details (smaller that the size of the filter) removal:
I more attention on large objects;
I small gaps bridging;
I noise reduction.
I Depending on the type of noise that affects the image, the
filter can be linear or non-linear.

Stefano Ferrari— Methods for Image processing— a.y. 2022/23 8


.

Linear smoothing filters

I Averaging filters (or low pass)


I Arithmetic average
I Weighted average

Smoothing effects (1)

Original image and image filtered using a 3 × 3 averaging filter.

Stefano Ferrari— Methods for Image processing— a.y. 2022/23 9


.

Smoothing effects (2)

Filtering with averaging filters (5 × 5 and 9 × 9).

Smoothing effects (3)

Filtering with averaging filters (15 × 15 and 35 × 35).

Stefano Ferrari— Methods for Image processing— a.y. 2022/23 10


.

Example: details removal

f g =f ∗h m=g >θ

a b c (a) f , image 353 × 382


d (b) g , image filtered with
13 × 13 smoothing filter
(c) m, thresholded image
(d) k, masked image:
k(x, y ) = f (x, y ) × m(x, y )
k

Non-linear filters

I Statistics-based filters are the most used non-linear filters.


I Ranking (which requires sorting) is more computationally
expensive than linear operations.
I In particular:
I Median filter
I Max and Min filters
I Percentiles based filters
I Example: f = [100, 120, 98, 99, 110, 255, 100, 200, 10]
I mean: 121
I median: 100
I sorted: [10, 98, 99, 100, 100, 110, 120, 200, 255]
I min: 10
I max: 255

Stefano Ferrari— Methods for Image processing— a.y. 2022/23 11


.

Example: median filter

Salt-and-pepper noise removal

a b c

(a) Original image, corrupted by salt-and-pepper noise.


(b) Image filtered with averaging filter 3 × 3.
(c) Image filtered with median filter 3 × 3.

Homeworks and suggested readings

DIP 3e, Sects. 3.4, 3.5 DIP 4e, Sects. 3.4, 3.5
I pp. 144–156 I pp. 177–198

GIMP
I Filters
I Blur
I Enhance
I Despeckle
I Generic
I Convolution matrix

Stefano Ferrari— Methods for Image processing— a.y. 2022/23 12

You might also like