CPE409 Image Processing
Part 5
Spatial Filtering
Assist. Prof. Dr. Caner ÖZCAN
If the facts don't fit the theory, change the facts.
~Einstein
Outline
3. Intensity Transformations and Spatial
Filtering
► Some Basic Intensity Transformation Functions
► Histogram Processing
► Fundamentals of Spatial Filtering
► Smoothing Spatial Filters
► Sharpening Spatial Filters
► Combining Spatial Enhancement Methods
► Using Fuzzy Techniques for Intensity
Transformations and Spatial Filtering
2
Importance of Neighborhood
► Both zebras and dalmatians have black and white
pixels in similar numbers.
► The difference between the two is the characteristic
appearance of small group of pixels rather than
individual pixel values.
3
Spatial Filtering
► A spatial filter consists of (a) a neighborhood, and
(b) a predefined operation
► Linear spatial filtering of an image of size MxN
with a filter of size mxn is given by the expression
a b
g ( x, y ) = w(s, t ) f ( x + s, y + t )
s =− a t =− b
4
Spatial Filtering
5
Spatial Filtering (Moving Average In 2D)
6
Slide credit: S. Seitz
Spatial Filtering (Moving Average In 2D)
7
Slide credit: S. Seitz
Spatial Filtering (Moving Average In 2D)
8
Slide credit: S. Seitz
Spatial Filtering (Moving Average In 2D)
9
Slide credit: S. Seitz
Spatial Filtering (Moving Average In 2D)
10
Slide credit: S. Seitz
Spatial Filtering (Moving Average In 2D)
11
Slide credit: S. Seitz
Smoothing Spatial Filters
► Smoothing filters are used for blurring and for
noise reduction.
► Blurring is used in removal of small details and
bridging of small gaps in lines or curves.
► Smoothing spatial filters include linear filters and
nonlinear filters.
12
Two Smoothing Averaging Filter Masks
Python Code:
kernel1 = [Link]((3, 3))
img = cv2.filter2D(src=image, ddepth=-1, kernel=kernel1)
13
Two Smoothing Averaging Filter Masks
14
Blur an image with a 2D convolution matrix
15
Smoothing (Averaging) Filter Masks
► Ortalama filtresini gerçekleştirmek için
[Link]() ve [Link]() işlevleri
kullanılabilir.
► Her iki işlev de çekirdeği kullanarak bir
görüntüyü düzgünleştirir.
16
Smoothing (Averaging) Filter Masks
17
Spatial Smoothing Linear Filters
The general implementation for filtering an M N image
with a weighted averaging filter of size m n is given
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 m = 2a + 1, n = 2b + 1.
18
Spatial Smoothing Linear Filters
19
Example: Gross Representation of Objects
20
Order-statistic (Nonlinear) Filters
— Nonlinear
— Based on ordering (ranking) the pixels contained in the
filter mask
— Replacing the value of the center pixel with the value
determined by the ranking result
E.g., median filter, max filter, min filter
21
Order-statistic (Nonlinear) Filters
22
Example: Use of Median Filtering for Noise Reduction
Python Code (median):
medianBlur(source_image, kernel_size)
23
Sharpening Spatial Filters
► The aim is to emphasize the transitions in intensity.
► Laplacian Operator
► Unsharp Masking and Highboost Filtering
► Using First-Order
Derivatives for Nonlinear Image
Sharpening — The Gradient
24
Sharpening Spatial Filters: Foundation
► The first-order derivative of a one-dimensional function
f(x) is the difference
f
= f ( x + 1) − f ( x )
x
► The second-order derivative of f(x) as the difference
2 f
= f ( x + 1) + f ( x − 1) − 2 f ( x)
x 2
25
26
Sharpening Spatial Filters: Laplace Operator
► The second-order isotropic derivative operator is the
Laplacian for a function (image) f(x,y)
2
f 2
f
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
2 f = f ( x + 1, y ) + f ( x − 1, y ) + f ( x, y + 1) + f ( x, y − 1)
- 4 f ( x, y )
27
Sharpening Spatial Filters: Laplace Operator
28
Sharpening Spatial Filters: Laplace Operator
► Image sharpening in the way of using the Laplacian:
g ( x, y ) = f ( x, y ) + c 2 f ( x, y )
where,
f ( x, y ) is input image,
g ( x, y ) is sharpenend images,
c = -1 if 2 f ( x, y ) corresponding to Fig. 3.37(a) or (b)
and c = 1 if either of the other two filters is used.
29
Sharpening Spatial Filters: Laplace Operator
Python Code:
Laplacian( src_gray, dst, ddepth, kernel_size,
scale, delta, BORDER_DEFAULT );
• src_gray: The input image.
• dst: Destination (output) image
• ddepth: Depth of the destination image. Since our input
is CV_8U we define ddepth = CV_16S to avoid overflow
• kernel_size: The kernel size of the Sobel operator to be applied
internally. We use 3 in this example.
• scale, delta and BORDER_DEFAULT: We leave them as default
values.
30
Sharpening Spatial Filters: Laplace Operator
31
32
Image Sharpening based on First-Order Derivatives
For function f ( x, y ), the gradient of f at coordinates ( x, y )
is defined as
f
g x x
f grad( f ) =
g y f
y
The magnitude of vector f , denoted as M ( x, y )
Gradient Image M ( x, y ) = mag(f ) = g x 2 + g y 2
33
Image Sharpening based on First-Order Derivatives
The magnitude of vector f , denoted as M ( x, y )
M ( x, y ) = mag(f ) = g x 2 + g y 2
M ( x, y ) | g x | + | g y |
z1 z2 z3
M ( x, y ) =| z8 − z5 | + | z6 − z5 |
z4 z5 z6
z7 z8 z9
34
Image Sharpening based on First-Order Derivatives
35
Image Sharpening based on First-Order Derivatives
Roberts Cross-gradient Operators
M ( x, y ) | z9 − z5 | + | z8 − z6 |
Sobel Operators
M ( x, y ) | ( z7 + 2 z8 + z9 ) − ( z1 + 2 z2 + z3 ) |
z1 z2 z3 + | ( z3 + 2 z6 + z9 ) − ( z1 + 2 z4 + z7 ) |
z4 z5 z6
z7 z8 z9
36
Example
37
Example:
Combining
Spatial
Enhancement
Methods
Goal:
Enhance the
image by
sharpening it
and by bringing
out more of the
skeletal detail
38
Example:
Combining
Spatial
Enhancement
Methods
Goal:
Enhance the
image by
sharpening it
and by bringing
out more of the
skeletal detail
39
References
► Sayısal Görüntü İşleme, Palme Publishing, Third Press Trans. (Orj:
R.C. Gonzalez and R.E. Woods: "Digital Image Processing",
Prentice Hall, 3rd edition, 2008).
► “Digital Image Processing Using Matlab”, Gonzalez & Richard E.
Woods, Steven L. Eddins, Gatesmark Publishing, 2009
► Lecture Notes, CS589-04 Digital Image Processing, Frank
(Qingzhong) Liu, [Link]
► Lecture Notes, BIL717-Image Processing, Erkut Erdem
► Lecture Notes, EBM537-Image Processing, [Link]
[Link]
► [Link]
function/
► [Link]
python-and-opencv-2bce390be0d1
► Bekir Aksoy, Python ile İmgeden Veriye Görüntü İşleme ve
Uygulamaları, Nobel Akademik Yayıncılık
40