Download as pdf or txt
Download as pdf or txt
You are on page 1of 60

CSE483 Computer Vision

Image Enhancement in the Spatial Domain

Prof. Mahmoud Khalil

1
Image Enhancement in the Spatial Domain
Image Enhancement in the Spatial Domain
Convolution Example – Step 1
1 1 1 2 2 2 3
-1 2 1 2 1 3 3
-1 -1 1 2 2 1 2
h 1 3 2 2
1 1 1
-1 2
4 2 2 3 5
-1 2
-2 1 3 3
2 2 1 2
f*h
1 3 2 2
f 4
Convolution Example – Step 2
1 1 1 2 2 2 3
-1 2 1 2 1 3 3
-1 -1 1 2 2 1 2
h
1 3 2 2
1 1 1
2
-2 2
4 2 3 5 4
2
-2 1
-1 3 3
2 2 1 2
f*h
1 3 2 2
f 5
Convolution Example – Step 3
1 1 1 2 2 2 3
-1 2 1 2 1 3 3
-1 -1 1 2 2 1 2
h
1 3 2 2
1 1 1
2 2
-2 2
4 3
5 4 4
2 1
-1 3
-3 3
2 2 1 2
1 3 2 2
f
f*h 6
Convolution Example – Step 4
1 1 1 2 2 2 3
-1 2 1 2 1 3 3
-1 -1 1 2 2 1 2
h
1 3 2 2
1 1 1
2 2 2
-2 3
6 1 5 4 4 -2
2 1 3
-3 3
-3 1
2 2 1 2
1 3 2 2
f f*h 7
Convolution Example – Step 5
1 1 1 2 2 2 3
-1 2 1 2 1 3 3
-1 -1 1 2 2 1 2
1 3 2 2
h

1 2 2 2 3 5 4 4 -2
-1 4
2 1 3 3 9
-1 -2
2 2 1 2
1 3 2 2
f f*h 8
Convolution Example – Step6
1 1 1 2 2 2 3
-1 2 1 2 1 3 3
-1 -1 1 2 2 1 2

h 1 3 2 2

2 2 2 3
5 4 4 -2
-2
2 2
1 3 3
9 6
2
-2 2
-2 1 2
1 3 2 2

f
f*h 9
Box Filter

What does it do? f [ , ]


• Replaces each pixel with an
average of its neighborhood 1 1 1

1 1 1
• Achieve smoothing effect
(remove ‘sharp’ features) 1 1 1

• Why does it sum to one?

Slide credit: David Lowe (UBC) 10


f [ , ]
Smoothing with box filter 1 1 1
1 1 1
1 1 1

11
David Lowe
Filtering practice
Think-Pair-Share

13
0 0 0
1. 0 1 0
0 0 0

0 0 0
2. 0 0 1
0 0 0

1 0 -1
3. 2 0 -2
1 0 -1

0 0 0

-
1 1 1
4. 0 2 0 1 1 1
0 0 0 1 1 1
14
1. Practice with linear filters

0 0 0
0 1 0 ?
0 0 0

Original

Source: D. Lowe 15
1. Practice with linear filters

0 0 0
0 1 0
0 0 0

Original Filtered
(no change)

Source: D. Lowe 16
2. Practice with linear filters

0 0 0
0 0 1 ?
0 0 0

Original

Source: D. Lowe 17
2. Practice with linear filters

0 0 0
0 0 1
0 0 0

Original Shifted left


By 1 pixel

Source: D. Lowe 18
3. Practice with linear filters

1 0 -1
2 0 -2
1 0 -1
Sobel

Vertical Edge
Source: D. Lowe (absolute value) 19
3. Practice with linear filters

1 2 1
0 0 0
-1 -2 -1
Sobel

Horizontal Edge
Source: D. Lowe (absolute value) 20
What is the 1 2 1 pattern?
• Binomial

• Approximates Gaussian using integer mathematics

• Smooths along edge

21
4. Practice with linear filters

-
0 0 0 1 1 1
0
0
2
0
0
0
1
1
1
1
1
1
?
(Note that filter sums to 1)
Original

Source: D. Lowe 22
4. Practice with linear filters

-
0 0 0 1 1 1
0 2 0 1 1 1
0 0 0 1 1 1

Original Sharpening filter


- Accentuates differences with local average

Source: D. Lowe 23
4. Practice with linear filters

Source: D. Lowe 24
Image Enhancement in the Spatial Domain

25
Image Enhancement in the Spatial Domain

26
Problem 3.21
The three images shown were blurred using square averaging
masks of sizes n = 23,25, and 45, respectively. The vertical bars
on the left lower part of (a) and (c) are blurred, but a clear
separation exists between them. However, the bars have
merged in image (b), in spite of the fact that the mask that
produced this image is significantly smaller than the mask that
produced image (c). Explain this.

27
Image Enhancement in the Spatial Domain

28
Problem 3.22
Consider an application such as the one shown in Fig. 3.34,
in which it is desired to eliminate objects smaller than those
enclosed in a square of size q X q pixels.
Suppose that we want to reduce the average gray level of
those objects to one-tenth of their original average gray level.
In this way, those objects will be closer to the gray level of the
background and they can then be eliminated by thresholding.
Give the (odd) size of the smallest averaging mask that will
accomplish the desired reduction in average gray level in only
one pass of the mask over the image.

29
Problem 3.20
(a)In a character recognition application, text pages are reduced to binary
form using a thresholding transformation function of the form shown in
Fig. 3.2(b). 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?
30
Order‐Statistics Filters
 Median Filter

 Maximum Filter

 Minimum Filter

31
Order‐Statistics Filters

32
Sharpening Spatial Filters

33
Sharpening Spatial Filters – Laplacian Filter
 First derivative

 Second derivative

34
Sharpening Spatial Filters

35
Sharpening Spatial Filters

36
Sharpening Spatial Filters

37
Problem 3.23

In a given application an averaging mask is


applied to input images to reduce noise, and then
a Laplacian mask is applied to enhance small
details. Would the result be the same if the order
of these operations were reversed?

38
Image Enhancement in the Spatial Domain

39
Image Enhancement in the Spatial Domain

40
Important filter: Gaussian
Weight contributions of neighboring pixels by nearness

0.003 0.013 0.022 0.013 0.003


0.013 0.059 0.097 0.059 0.013
0.022 0.097 0.159 0.097 0.022 y
0.013 0.059 0.097 0.059 0.013
0.003 0.013 0.022 0.013 0.003

x Kernel size 5 x 5,
y Standard deviation  = 1

Viewed
y
from top

44
Slide credit: Christopher Rasmussen
x
Smoothing with Gaussian filter

James Hays 45
Smoothing with box filter

James Hays 46
Gaussian filters
Gaussian convolved with Gaussian…
…is another Gaussian
– So can smooth with small‐width kernel, repeat, and get same result as larger‐
width kernel
– Convolving twice with Gaussian kernel of width σ is same as convolving once
with kernel of width σ√2

Separable kernel
– Factors into product of two 1D Gaussians

Source: K. Grauman 47
Separability of the Gaussian filter

Source: D. Lowe
48
Separability example
2D convolution =
(center location only)

The filter factors into a


product of 1D filters:

Perform convolution =
along rows: *

Followed by convolution =
along the remaining column: *
Source: K. Grauman 49
Separability
Why is separability useful in practice?

MxN image, PxQ filter


• 2D convolution: ~MNPQ multiply‐adds
• Separable 2D: ~MN(P+Q) multiply‐adds

Speed up = PQ/(P+Q)
9x9 filter = ~4.5x faster

50
Think‐Pair‐Share * = Convolution operator

1) _ = D * B D H
2) A = _ * _
3) F = D * _
4) _ = D * D

F I
A B

C
G

E
Hoiem 51
Think‐Pair‐Share * = Convolution operator

1) G = D * B D H
2) A = _ * _
3) F = D * _
4) _ = D * D

F I
A B

C
G

E
Hoiem 52
Think‐Pair‐Share * = Convolution operator

1) G = D * B D H
2) A = B * C
3) F = D * _
4) _ = D * D

F I
A B

C
G

E
Hoiem 53
Think‐Pair‐Share * = Convolution operator

1) G = D * B D H
2) A = B * C
3) F = D * E
4) _ = D * D

F I
A B

C
G

E
Hoiem 54
Think‐Pair‐Share * = Convolution operator

1) G = D * B D H
2) A = B * C
3) F = D * E
4) I = D * D

F I
A B

C
G

E
Hoiem 55
Image Enhancement in the Spatial Domain

56
Image Enhancement in the Spatial Domain

57
Gamma Correction

58
Gamma Correction

59
Gamma Correction

60
Contrast Stretching

61
Image Enhancement in the Spatial Domain

62
Histogram Equalization

63
Image Enhancement in the Spatial Domain

64

You might also like