70
Spatial Domain Linear Spatial Domain Linear Filtering Yao Wang Polytechnic University, Brooklyn, NY 11201 With contribution from Zhu Liu, Onur Guleryuz, and Gonzalez/Woods, Digital Image Processing, 2ed

Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Embed Size (px)

Citation preview

Page 1: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Spatial Domain LinearSpatial Domain Linear Filtering

Yao WangPolytechnic University, Brooklyn, NY 11201

With contribution from Zhu Liu, Onur Guleryuz, andGonzalez/Woods, Digital Image Processing, 2ed

Page 2: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Outline

• IntroductionNoise remo al sing lo pass filters• Noise removal using low-pass filters

• Sharpening by edge enhancement– Edge detection using high-pass filters– Edge enhancement by high emphasis filters

• Edge detection– First order gradientg– Second order gradient

• Summary

Yao Wang, NYU-Poly EL5123: Spatial Filtering 2

• Summary

Page 3: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Problems

• SmoothingNoise removal– Noise removal

– Detail preserving image smoothingSh i• Sharpening– Edge enhancement– Detail focusing

• Edge detection

Yao Wang, NYU-Poly EL5123: Spatial Filtering 3

Page 4: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Approaches

• Spatial domain operation or filtering (the processed value for the current pixelprocessed value for the current pixel depends on both itself and surrounding pixels)pixels)– Linear filtering

Non linear filtering– Non-linear filtering• Rank order filtering including median• Morphological filtering• Morphological filtering

– Adaptive filtering

Yao Wang, NYU-Poly EL5123: Spatial Filtering 4

Page 5: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Noise Removal (Image Smoothing)• An image may be “dirty” (with dots, speckles,stains)• Noise removal:

– To remove speckles/dots on an image– Dots can be modeled as impulses (salt-and-pepper or speckle)

or continuously varying (Gaussian noise)or continuously varying (Gaussian noise)– Can be removed by taking mean or median values of

neighboring pixels (e.g. 3x3 window)Equivalent to low pass filtering– Equivalent to low-pass filtering

• Problem with low-pass filtering– May blur edges– More advanced techniques: adaptive, edge preserving

Yao Wang, NYU-Poly EL5123: Spatial Filtering 5

Page 6: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Example

Yao Wang, NYU-Poly EL5123: Spatial Filtering 6

Page 7: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Averaging Filter: An Intuitive Approach

• Replace each pixel by the average of pixels in a square window surrounding thispixels in a square window surrounding this pixel

)1,1(),1()1,1((91),( nmfnmfnmfnmg

))1,1(),1()1,1()1,(),()1,(

9

nmfnmfnmfnmfnmfnmf

• Trade-off between noise removal and detail preserving:

)),(),(),( fff

detail preserving:– Larger window -> can remove noise more

effectively, but also blur the details/edges

Yao Wang, NYU-Poly EL5123: Spatial Filtering 7

y, g

Page 8: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Example: 3x3 average

100 100 100 100 100

100 200 205 203 100100 200 205 203 100

100 195 200 200 100

100 200 205 195 100

100 100 100 100 100

100 100 100 100 100100 144 167 145 100100 167 200 168 100100 167 200 168 100

100 144 166 144 100100 100 100 100 100

Yao Wang, NYU-Poly EL5123: Spatial Filtering 8

Page 9: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Example

Yao Wang, NYU-Poly EL5123: Spatial Filtering 9

Page 10: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Weighted Averaging Filter• Instead of averaging all the pixel values in the

window, give the closer-by pixels higherwindow, give the closer by pixels higher weighting, and far-away pixels lower weighting.

)(*)()()()(1 1

nmhnmflnkmflkhnmgk l

• This type of operation is in fact 2-D linear

),(*),(),(),(),(0 0

nmhnmflnkmflkhnmgkk ll

This type of operation is in fact 2 D linear convolution of f(m,n) by a filter h(m,n).

• Weighted average filter retains low frequency g g q yand suppresses high frequency = low-pass filter

Yao Wang, NYU-Poly EL5123: Spatial Filtering 10

Page 11: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Example Weighting Mask

1 1 1 1 2 1

1 1 1

1 1 1

2 4 2

1 2 1

91

161

1 1 1 1 2 1

All weights must sum to one

Yao Wang, NYU-Poly EL5123: Spatial Filtering 11

Page 12: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Example: Weighted Average

100 100 100 100 100100 200 205 203 100100 195 200 200 100100 200 205 195 100100 100 100 100 100 1 2 1

11 1 1

100 100 100 100 1002 4 21 2 1

161

1 1 11 1 1

91

100 100 100 100 100100 156 176 158 100

100 100 100 100 100100 144 167 145 100

100 174 201 175 100100 156 175 156 100100 100 100 100 100

100 167 200 168 100100 144 166 144 100100 100 100 100 100

Yao Wang, NYU-Poly EL5123: Spatial Filtering 12

100 100 100 100 100100 100 100 100 100

Page 13: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Example

Original image Average filtered image Weighted Average filtered image

Yao Wang, NYU-Poly EL5123: Spatial Filtering 13

Page 14: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Symmetrical Filter• The smoothing filter is generally symmetrical in both

horizontal and vertical directions, as well as diagonal.

• Simplified calculation.),(),(),(),( nmhnmhnmhnmh

k lHhk=0

-W

),(*),(),(),(),(1

0

1

0

nmhnmflnkmflkhnmgk

kk

l

ll

0

W-W Wl=0

W

nlmfnlmflnmflnmflh

nmfhnmgWllkk 0101

))()()()()(0(

),()0,0(),(

W

k

W

l

l

lnkmflnkmflnkmflnkmflkh

nlmfnlmflnmflnmflh

1 1

1

)),(),(),(),()(,(

)),(),(),(),()(,0(

Yao Wang, NYU-Poly EL5123: Spatial Filtering 14

k l1 1

Page 15: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Computation Cost• General (2W+1) by (2W+1) filter

– (2W+1)2 multiplications per processed pixel(2W+1) multiplications per processed pixel

W

k

W

llnkmflkhnmg ),(),(),(

• Symmetrical filter– 1+W+W2 ~=(W+1)2 multiplications

Wk Wl

1+W+W (W+1) multiplications• Saving

– W = 2 and 512x512 imageW 2, and 512x512 image– Saving 512*512*(52-32)=222

– More savings for larger W

Yao Wang, NYU-Poly EL5123: Spatial Filtering 15

More savings for larger W

Page 16: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Common Smoothing Filters

11121111 b.1,

11

11

)2(1;

121242121

161;

111121111

101 2

2

bwith

bbbb

b

b

• Are any of these separable?

11121111 b

Are any of these separable?• Criteria for designing a smoothing filter

h(k l)≥0 (function as averaging)– h(k,l)≥0 (function as averaging)– (the mean value is preserved)1),(

1

0

1

0

k

kk

l

lllkh

Yao Wang, NYU-Poly EL5123: Spatial Filtering 16

Page 17: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Smoothing Filter is a Low-pass Filter• The 2D Fourier Transform of a smoothing filter

• Image signal usually varies slowly, and noise is usually a wide band signal.A l i l filt th hi h f t f th• Applying a low pass filter removes the high-frequency part of the noise.

• Image edges and other details are blurred

Yao Wang, NYU-Poly EL5123: Spatial Filtering 17

Page 18: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Interpretation in Freq Domain

Filter response

Low-passed image spectrum

Filter response

Noise spectrum

Original image spectrum

Noise spectrum

f0

Yao Wang, NYU-Poly EL5123: Spatial Filtering 18

Page 19: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Spectrum of Example Low Pass Filterrs

;11111

91111

111

91H

22

2 ;111

)2(1

11

)2(1 bb

bbbb

b

bH

9/)2cos(21)2cos(21),(1

9111

9

vuvuH

2)2/()2cos(2)2cos(2),(

1)2(11)2(

bvbubvuH

bbb

11

0.2

0.4

0.6

0.8

-0.5

0

0.5

05

1015

2025

0

10

20

300

05

1015

2025

0

10

20

30

Yao Wang, NYU-Poly EL5123: Spatial Filtering 19

b=2

Page 20: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Directional Smoothing• Smoothing filter tend to blur details• Challenging problem: detail preserving noise removal!

• Di the i-th directional neighborhood• Usually 4 or 8 directions D

3

y• 3 core steps

– Find σi2, the variance in Di

– Find i0, so that σi02 ≤σi

2D1

Find i0, so that σi0 σi

– Filtering in i0 direction

ioDlk

io

lnkmfN

nmg),(

),(1),(

})sincosexp{(),(

),(*),(),(2

0

iii

i

nmanmh

nmhnmfnmg

Yao Wang, NYU-Poly EL5123: Spatial Filtering 20

})p{(),( iii

Page 21: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Noise Removal by Averaging Multiple ImagesImages

Yao Wang, NYU-Poly EL5123: Spatial Filtering 21

Page 22: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Noise Removal by Averaging Multiple ImagesImages

• Assume y = x + n, where n ~ N(0, σ2).• Observe one image, the noise is n• Observe N images: yi = x + ni i=1 N where ni ~ N(0 σ2) and niObserve N images: yi x ni, i 1,…,N, where ni N(0, σ ), and ni

are independent.

NNN

nxnxyy 1)(11

i

ii

ii

i nN

xnxN

yN

y111

)(

)/,0(~, 2 NNnwherenxy

• Averaging over N images reduces the noise variance by 1/N.

Yao Wang, NYU-Poly EL5123: Spatial Filtering 22

Page 23: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Edge Detection

• What is an edge?Difference bet een edge and line and• Difference between edge and line and point

Yao Wang, NYU-Poly EL5123: Spatial Filtering 23

Page 24: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Characterization of EdgesIdeal step edge

Real edge has a slope

First order derivative:Maximum at edge location

Second order derivative:Second order derivative:Zero crossing at edge location

Yao Wang, NYU-Poly EL5123: Spatial Filtering 24

Page 25: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Edge Detection Based on First Order DerivativesDerivatives

• Edge High-pass filtering across edgeLow-pass filtering along edge

111

111h

Non-Edge

g Yes

No

Non Edge

H |g|>Tf g Yes Edge

Yao Wang, NYU-Poly EL5123: Spatial Filtering 25

What if we don’t know edge direction?

Page 26: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Edge Detection Based on Gradients in Two Orthogonal DirectionsTwo Orthogonal Directions

• Combine results from directional edge detectors in two orthogonal directions anddetectors in two orthogonal directions and determine the magnitude and direction of the edgethe edge.

H f 2+f 2 |g|>Tg

fx Edge

Non-Edge

Hx fx +fy |g|>T

f

Hy tan-1(fx/fy) theta

f

Yao Wang, NYU-Poly EL5123: Spatial Filtering 26

Hy tan (fx/fy)fy

Page 27: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Directional Edge Detector • High-pass (or band-pass) in one direction (simulating

first order derivative)• Low pass in the orthogonal direction (smooth noise)• Prewitt edge detector LP BP

101111

31

101101101

31;111

101

31

111000111

31

yx HH

• Sobel edge detector11011111

11011121 101

121

41

101202101

41;121

101

41

121000121

41

yx HH

Yao Wang, NYU-Poly EL5123: Spatial Filtering 27

The sobel filter provides better smoothing along the edge

Page 28: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Freq. Response of Sobel Filter

1121:Edges Horizontalfor Filter Sobel

H x

(DTFT)RF

121101

41

121000121

41

Hh

ujeeHh

vjvj

ujujxx

211211]2[112sin2- 1] 0 [-1

:(DTFT)ResponseFrequency

22

22

veeHh vjvjyy 2cos1

22

4- 1] 2 [1

422

|Hx(u)| |Hy(v)|

Yao Wang, NYU-Poly EL5123: Spatial Filtering 28

0 1/4 1/2 u 0 1/4 1/2 v

Band-pass Low-pass

Page 29: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Spectrum of the Sobel Filter

10

2040

2040

0

5

xH

020

020

10

0

5

10

yH

020

40

020

40

L l th d b d th d

Yao Wang, NYU-Poly EL5123: Spatial Filtering 29

Low pass along the edge, band pass cross the edge

Page 30: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Example of Sobel Edge Detector

Original image Filtered image by H Filtered image by HyOriginal image Filtered image by Hx Filtered image by Hy

Yao Wang, NYU-Poly EL5123: Spatial Filtering 30

Page 31: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

How to set threshold?

• Trial and errorAccording to edge magnit de distrib tion• According to edge magnitude distribution– E.g assuming only 5% pixels should be edge

pixels then the threshold should be the 95%pixels, then the threshold should be the 95% percentile of the edge magnitudeIllustrate on board– Illustrate on board

Yao Wang, NYU-Poly EL5123: Spatial Filtering 31

Page 32: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

gy gmgx gy gmgx

T=100 T=50 T=20

2000

2500

Histogram of gm

500

1000

1500

0

0 100 200

Yao Wang, NYU-Poly EL5123: Spatial Filtering 32

Page 33: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Problems of previous approach• Cannot locate edges precisely• Ramp edges can lead to many edge pixels• Ramp edges can lead to many edge pixels

detected depending on the threshold T– T too high: may not detect weak edgesg y g– T too small: detected edges too think, noisy points

falsely detected• Remedy:

– Detecting local maximum of |g| in the normal direction of the edge or try all possible 8 direction in a 3x3of the edge, or try all possible 8 direction in a 3x3 neighbor

– Only consider pixels with |g| > T

Yao Wang, NYU-Poly EL5123: Spatial Filtering 33

y p |g|

Page 34: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Edge Detection with Many Directional FiltersFilters

• Instead of using two orthogonal directions, can design multiple directional filterscan design multiple directional filters– 0, 45, 90, 135

S hi h i th hi h t• See which one gives the highest response in the normal direction

Yao Wang, NYU-Poly EL5123: Spatial Filtering 34

Page 35: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Edge Detection Based on Second Order DerivativeDerivative

• Convolve an image with a filter corresponding to taking second ordercorresponding to taking second order derivative (e.g. Laplacian or LoG operator)Locate zero crossing in the filtered image• Locate zero-crossing in the filtered image

Yao Wang, NYU-Poly EL5123: Spatial Filtering 35

Page 36: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Laplacian Operator

2

2

2

22

yf

xf

f

),(),1(

2 f

yxfyxfxf

)1,(),(2)1,(

),1(),(2),1(

2

2

2

yxfyxfyxff

yxfyxfyxfxf

),(4)1,()1,(),1(),1(

)1,(),(2)1,(

2

2

yxfyxfyxfyxfyxf

yxfyxfyxfy

f

;010141

010;

010141010

Yao Wang, NYU-Poly EL5123: Spatial Filtering 36

010010

Page 37: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Fourier Transform of Laplacian Operator

141010

181111

010

141

111181

6

8

10

12

20

300

2

4

300

2

4

6

8

10

05 10

1520 25

0

10

05

1015

2025

0

10

20

Yao Wang, NYU-Poly EL5123: Spatial Filtering 37

More isotropic

Page 38: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Isotropic Edge Detector

• Laplacian operator can detect changes in all directionsall directions

Yao Wang, NYU-Poly EL5123: Spatial Filtering 38

Page 39: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Pros and Cons

• Can locate edges more accuratelyCan detect edges in ario s direction• Can detect edges in various direction

• But more prone to noise• Remedy:

– Smoothing before applying Laplaciang pp y g p

Yao Wang, NYU-Poly EL5123: Spatial Filtering 39

Page 40: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Laplacian of Gaussian (LoG)

• To suppress noise, smooth the signal using a Gaussian filter firstusing a Gaussian filter first– F(x,y)* G(x,y)

Th l L l i filtG• Then apply Laplacian filter– F(x,y)*G(x,y)*L(x,y) = F(x,y)* (L(x,y)*G(x,y))

G

• Equivalent filter: LoG– H(x,y)=L(x,y)*G(x,y)

Yao Wang, NYU-Poly EL5123: Spatial Filtering 40

Page 41: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Derivation of LoG Filter• Analog form

22 yx

2

22

2

222222

2

2

),(

yx

y

yxGG

eyxG

22422

2 2),(

eyx

yG

xGyxG

• Take samples to create filter maskSi f k > 6 dd– Size of mask nxn, n>=6, odd

– Exn

Yao Wang, NYU-Poly EL5123: Spatial Filtering 41

Page 42: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

LoG Filter

Yao Wang, NYU-Poly EL5123: Spatial Filtering 42

Page 43: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Laplacian filtered LOG filtered

Note that each strong edge in the original image corresponds to a thin stripe g g g g p pwith high intensity in one side and low intensity in the other side.

Yao Wang, NYU-Poly EL5123: Spatial Filtering 43

Page 44: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

How to detect zero crossing?

• For each pixel that has low filtered value, check a 3x3 neighbor to see whether itscheck a 3x3 neighbor, to see whether its two neighbors in opposite direction have opposite sign and their difference exceedsopposite sign and their difference exceeds a threshold (Marr-Hildreth edge detection method)method)

Yao Wang, NYU-Poly EL5123: Spatial Filtering 44

Page 45: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Example

Yao Wang, NYU-Poly EL5123: Spatial Filtering 45

Page 46: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Summary of Edge Detection Method• First order gradient based:

– Using edge detectors in two orthogonal directionsUsing edge detectors in two orthogonal directions• For each direction: low-pass along edge, high-pass across

edge

Using edge detectors in multiple (>2) directions– Using edge detectors in multiple (>2) directions– Use threshold or detect local maximum across the

edge directiong• Second order gradient based

– Laplacian is noise-prone, LoG is betterp p– Detect zero crossing– Isotropic

Yao Wang, NYU-Poly EL5123: Spatial Filtering 46

Page 47: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

More on Edge Detection• Methods discussed so far generally cannot yield

connected thin edge maps• Need sophisticated post processing

– Thinning– Connecting broken linesConnecting broken lines

• Noise can lead to many false edge points• Even with many years of research, no perfect

d d t t i t!edge detectors exist!– Canny edge detector: Gaussian smoothing along

edges, high pass in each possible edge direction• For more on edge detection, See Gonzalez Sec.

10.1, 10.2

Yao Wang, NYU-Poly EL5123: Spatial Filtering 47

Page 48: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Results using MATLAB “edge( )” functionSobel,T=0.14 LOG, T=0.0051 canny,T=[0.0313,0.0781]

Sobel,T=0.1 LOG, T=0.01 canny,T=[0.1,0.15]

Yao Wang, NYU-Poly EL5123: Spatial Filtering 48

Page 49: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Image Sharpening• Sharpening : to enhance line structures or other

details in an image• Enhanced image = original image + scaled

version of the line structures and edges in the imageimage

• Line structures and edges can be obtained by applying a high pass filter on the imageapplying a high pass filter on the image

• Combined operation is still a weighted averaging operation, but some weights can be negative.

• In frequency domain, the filter has the “high-emphasis” character

Yao Wang, NYU-Poly EL5123: Spatial Filtering 49

Page 50: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Interpretation in Freq DomainFilter response (high emphasis)

sharpened image spectrum

Original image spectrum

f0

Yao Wang, NYU-Poly EL5123: Spatial Filtering 50

Page 51: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Designing Sharpening Filter Using High Pass FiltersPass Filters

• The desired image is the original plus an appropriately scaled high-passed imageappropriately scaled high-passed image

• Sharpening filter

hs fff

),(),(),( nmhnmnmh hs

Yao Wang, NYU-Poly EL5123: Spatial Filtering 51

Page 52: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Interpretation in Freq Domainhigh emphasis=allpass+highpass

all pass

high pass

f0

Yao Wang, NYU-Poly EL5123: Spatial Filtering 52

Page 53: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

How to design high pass filter• High pass filter tries to detect changes • Difference between a current pixel and its• Difference between a current pixel and its

neighbors– Some coefficients should be negative g

• Output should be zero if the image is flat

k l

0),(1

0

1

0

k

kk

l

ll

lkh

• More rigorous design would start with desired frequency response and take inverse FT.

0 0kk ll

Yao Wang, NYU-Poly EL5123: Spatial Filtering 53

frequency response and take inverse FT.

Page 54: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Using Laplacian Operator as Highpass FilterFilter

141010

181111

010

141

111181

6

8

10

12

20

300

2

4

300

2

4

6

8

10

05 10

1520 25

0

10

05

1015

2025

0

10

20

Yao Wang, NYU-Poly EL5123: Spatial Filtering 54

More isotropic

Page 55: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Examplef(x) fs(x)=f(x)+ag(x)g(x)=f(x)*hh(x)

x xx

0101

0101

.1010181

41

010141

41

withHH sh

.11161111

81181

111

81

withHH sh

Yao Wang, NYU-Poly EL5123: Spatial Filtering 55

1118

1118

Page 56: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Example of Sharpening Using Laplacian OperatorOperator

010

010181

010

41

hH

Yao Wang, NYU-Poly EL5123: Spatial Filtering 56

Page 57: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Example of Sharpening Using Laplacian OperatorOperator

4

8

Yao Wang, NYU-Poly EL5123: Spatial Filtering 57

Page 58: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Designing Sharpening Filtering Using Low Pass Filters (Unsharp Masking)( p g)

• The sharpened = original – a*lowpass

1

• Sharpening filter

10,1

1

ls fff

• Sharpening filter

1 ),(),(1

1),( nmhnmnmh ls

Yao Wang, NYU-Poly EL5123: Spatial Filtering 58

Page 59: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Interpretation in Freq Domainhigh emphasis=allpass- a*lowpass

all pass

low pass

f0

low pass

Yao Wang, NYU-Poly EL5123: Spatial Filtering 59

Page 60: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Example

f(x) fl(x)=f(x)*hl(x) fs(x)=f(x)- fl(x)

x x x

1121

1121

1

.41

1212602

481

121242

161

withHH sl

Criteria for designing sharpening filters: There are both positive and negativeCoefficients h(0 0) > 0 and ∑h(k l)=1 (to keep the same mean value)

Yao Wang, NYU-Poly EL5123: Spatial Filtering 60

Coefficients, h(0,0) > 0, and ∑h(k, l) 1 (to keep the same mean value).

Page 61: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Example

Original image Sharpened image

Yao Wang, NYU-Poly EL5123: Spatial Filtering 61

Page 62: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Challenges of Noise Removal and Image Sharpeningp g

• How to smooth the noise without blurring the details too much?the details too much?

• How to enhance edges without amplifying noise?noise?

• Still a active research area– Wavelet domain processing

Yao Wang, NYU-Poly EL5123: Spatial Filtering 62

Page 63: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Wavelet-Domain Filtering

Yao Wang, NYU-Poly EL5123: Spatial Filtering 63

Courtesy of Ivan Selesnick

Page 64: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Feature Enhancement by SubtractionSubtraction

Taking an image without injecting a contrast agent first. Then take the image again after the organ is injected some special contrast agent (which go into the bloodstreams only). Then subtract the two images --- A popular technique in medical imaging

Yao Wang, NYU-Poly EL5123: Spatial Filtering 64

Then subtract the two images --- A popular technique in medical imaging

Page 65: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Summary• Noise removal using low-pass filters

– Averaging and weighted averaging filter• Edge detection

– Using isotropic high-pass filters: eg. 2D Laplacian– Using two orthogonal high-pass filters: eg. Sobel– Using many directional high-pass filters

• Image sharpening by high emphasis filters– High pass filtering using Laplacian operatorsHigh pass filtering using Laplacian operators– Sharpening using high pass filter (low pass+ a* high pass)– Sharpening using low pass filter (unsharp masking)(original-

a*lowpass)p )• Given a filter, you should be able to tell what does it do

(smoothing, edge detection, sharpening?) by looking at its coefficients and also through DTFT

Yao Wang, NYU-Poly EL5123: Spatial Filtering 65

its coefficients, and also through DTFT

Page 66: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Reading

• R. Gonzalez, “Digital Image Processing”, Section 3 4 ~ 3 8 10 1 10 2Section 3.4 ~ 3.8, 10.1, 10.2– Note that in Gonzalez book: Edge detection

filtering are referred to sharpeningfiltering are referred to sharpening.• A. K. Jain, “Fundamentals of Digital Image

Processing ” Section 7 4 (except medianProcessing, Section 7.4 (except median filter).

Yao Wang, NYU-Poly EL5123: Spatial Filtering 66

Page 67: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Written Assignment1. For each filter given below, compute its Fourier transform, and illustrate its magnitude

response. Determine what is its function (smoothing, edge enhancement or edge detection?) based on the filter coefficients as well as its frequency response. For each filter, determine whether it is separable? If yes compute the FT separately and explain the function of eachwhether it is separable? If yes, compute the FT separately and explain the function of each 1D filter. If not, compute the FT directly.

121 111

121242121

161

1H ;111181111

2

H

010151

010

3H

Yao Wang, NYU-Poly EL5123: Spatial Filtering 67

Page 68: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Written Assignment (cnt’d)2. Recall that the Laplacian of Gaussian filter is the Laplacian of a Gaussian function. Let the

Gaussian function be given by2

22

2),( yx

eyxG

show that the LOG filter can be written as

3 Define the a discrete Laplacian of Gaussian (LoG) filter of size 7x7 by sampling the above

2),( 2

22

24

222

yx

eyxyxL

3. Define the a discrete Laplacian of Gaussian (LoG) filter of size 7x7 by sampling the above continuous LoG function, assuming =1. Determine its discrete space Fourier transform. Illustrate its magnitude response. Explain its function. You can use the “freqz2( )” function to determine and illustrate its DTFT.

Yao Wang, NYU-Poly EL5123: Spatial Filtering 68

Page 69: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

Computer Assignment1. Write a Matlab or C-program to simulate noise removal. First create a noisy image, by adding

zero mean Gaussian random noise to your image (In matlab, the noise can be generated using “imnoise()”). You can specify the noise variance in “imnoise( )”). Then apply an averaging filter to the noise added image. For a chosen variance of the added noise, you need to try different window sizes (from 3x3 to 7x7) to see which one gives you the best trade-off between noisewindow sizes (from 3x3 to 7x7) to see which one gives you the best trade-off between noise removal and blurring. Hand in your program, the original noise-added images at two different noise levels (0.01 and 0.1) and the corresponding filtered images with the best window sizes. Write down your observation. For the filtering operation, don’t use the matlab “conv2()” function. Rather, write your own function for the averaging operation. Your program should allow the user to specify the window size as an input parameter.

2. Write a Matlab or C-program for implementing the following simple edge detection algorithm: For every pixel: i) find the horizontal and vertical gradients, gx and gy, using the Sobel operator; ii) calculate the magnitude of the gradient using ; and iii) For a chosen

22yxm ggg

threshold T, consider the pixel to be an edge pixel if gm ≥ T. Save the resulting edge map (Use a gray level of 255 to indicate an edge pixel, and a gray level of 0 for a non-edge pixel). Apply this program to your test image, and observe the resulting edge map with different T values, until you find a T that gives you a good result. Hand in your program and the edge maps u t you d a t at g es you a good esu t a d you p og a a d t e edge apsgenerated by two different threshold values. Write down your observation. Hint: one automatic way to determine T is by sorting the pixels based on the gradient magnitudes, and choose T such that a certain percentage (say 5%) of pixels will be edge pixels. You can use either the

Yao Wang, NYU-Poly EL5123: Spatial Filtering 69

matlab conv2( ) function or write your own code for the filtering part.

Page 70: Spatial Domain LinearSpatial Domain Linear Filteringeeweb.poly.edu/~yao/EL5123/lecture5_filtering.pdf · Spatial Domain LinearSpatial Domain Linear Filtering Yao Wang ... Averaging

3. Using the matlab “edge( )” function, evaluate the edge detection results using the following methods on an image: “sobel”, “log”,and “canny”. For each method, first use the default threshold value. Then try to use a threshold that is higher and lower, to see the effect. Comments on the advantages and disadvantages of each method.

For problems 2 and 3, try to use an image that has a lot of edges. A good set of test images can be found under the matlab directory …./toolbox/images/imdemosimages can be found under the matlab directory …./toolbox/images/imdemos

Yao Wang, NYU-Poly EL5123: Spatial Filtering 70