You are on page 1of 70

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
Outline
• Introduction
• Noise remo
removalal using
sing lo
low-pass
pass filters
• Sharpening by edge enhancement
– Edge detection using high-pass filters
– Edge enhancement by high emphasis filters
• Edge detection
– First order gradient
g
– Second order gradient
• Summary
Yao Wang, NYU-Poly EL5123: Spatial Filtering 2
Problems
• Smoothing
– Noise removal
– Detail preserving image smoothing
• Sharpening
Sh i
– Edge enhancement
– Detail focusing
• Edge detection

Yao Wang, NYU-Poly EL5123: Spatial Filtering 3


Approaches
• Spatial domain operation or filtering (the
processed value for the current pixel
depends on both itself and surrounding
pixels)
– Linear filtering
– Non-linear
Non linear filtering
• Rank order filtering including median
• Morphological filtering
– Adaptive filtering

Yao Wang, NYU-Poly EL5123: Spatial Filtering 4


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)
– Can be removed by taking mean or median values of
neighboring pixels (e.g. 3x3 window)
– Equivalent to low
low-pass
pass filtering
• Problem with low-pass filtering
– May blur edges
– More advanced techniques: adaptive, edge preserving

Yao Wang, NYU-Poly EL5123: Spatial Filtering 5


Example

Yao Wang, NYU-Poly EL5123: Spatial Filtering 6


Averaging Filter: An Intuitive Approach
• Replace each pixel by the average of
pixels in a square window surrounding this
pixel
1
g (m, n)  ( f (m  1, n  1)  f (m  1, n)  f (m  1, n  1)
9
 f (m, n  1)  f (m, n)  f (m, n  1)
 f (m  1, n  1)  f (m  1, n)  f (m  1, n  1))

• Trade-off between noise removal and


detail preserving:
– Larger window -> can remove noise more
effectively,
y, but also blur the details/edges
g

Yao Wang, NYU-Poly EL5123: Spatial Filtering 7


Example: 3x3 average

100 100 100 100 100


100 200 205 203 100
100 195 200 200 100
100 200 205 195 100
100 100 100 100 100

100 100 100 100 100


100 144 167 145 100
100 167 200 168 100
100 144 166 144 100
100 100 100 100 100

Yao Wang, NYU-Poly EL5123: Spatial Filtering 8


Example

Yao Wang, NYU-Poly EL5123: Spatial Filtering 9


Weighted Averaging Filter
• Instead of averaging all the pixel values in the
window, give the closer-by
closer by pixels higher
weighting, and far-away pixels lower weighting.
k1 l1
g (m, n)   h(k , l ) f (m  k , n  l )  f (m, n) * h(m, n)
k  k 0 l  l0

• This type of operation is in fact 2-D


2 D linear
convolution of f(m,n) by a filter h(m,n).
• Weighted
g averageg filter retains low frequency
q y
and suppresses high frequency = low-pass filter

Yao Wang, NYU-Poly EL5123: Spatial Filtering 10


Example Weighting Mask

1 1 1 1 2 1
1
 1 1 1 1
 2 4 2
9 16
1 1 1 1 2 1

All weights must sum to one

Yao Wang, NYU-Poly EL5123: Spatial Filtering 11


Example: Weighted Average
100 100 100 100 100
100 200 205 203 100
100 195 200 200 100
100 200 205 195 100
1 1 1 1 2 1
1 100 100 100 100 100 1
 
9 1 1 1 16 2 4 2
1 1 1 1 2 1

100 100 100 100 100 100 100 100 100 100
100 144 167 145 100 100 156 176 158 100
100 167 200 168 100 100 174 201 175 100
100 144 166 144 100 100 156 175 156 100
100 100 100 100 100 100 100 100 100 100

Yao Wang, NYU-Poly EL5123: Spatial Filtering 12


Example

Average filtered image Weighted Average


Original image filtered image

Yao Wang, NYU-Poly EL5123: Spatial Filtering 13


Symmetrical Filter
• The smoothing filter is generally symmetrical in both
horizontal and vertical directions, as well as diagonal.
h(m, n)  h(m, n)  h( m, n)  h(m, n) -W

• Simplified calculation. Hh
k1 l1 k=0
0
g (m, n)   h(k , l ) f (m  k , n  l )  f (m, n) * h(m, n)
k  k 0 l  l0
W
-W l=0 W
k1  k0  l1  l0  W
g(m, n)  h(0,0) f (m, n)
W
 h(0,l)( f (m, n  l)  f (m, n  l)  f (m  l, n)  f (m  l, n))
l 1
W W
 h(k,l)( f (m  k, n  l)  f (m  k, n  l)  f (m  k, n  l)  f (m  k, n  l))
k 1 l 1

Yao Wang, NYU-Poly EL5123: Spatial Filtering 14


Computation Cost
• General (2W+1) by (2W+1) filter
– (2W+1)2 multiplications per processed pixel
W W
g (m, n)    h( k , l ) f ( m  k , n  l )
k  W l  W

• Symmetrical filter
– 1+W+W2 ~=(W+1)
(W+1)2 multiplications
• Saving
– W = 2,
2 and 512x512 image
– Saving 512*512*(52-32)=222
– More savings for larger W

Yao Wang, NYU-Poly EL5123: Spatial Filtering 15


Common Smoothing Filters

1 1 1 1 2 1  1 b 1
1   ; 1  2 4 2 ; b b 2 b , with b  1.
1
1 2 1
10   16   (b  2) 2 
1 1 1 1 2 1  1 b 1

• Are any of these separable?


• Criteria for designing a smoothing filter
– h(k,l)≥0
h(k l)≥0 (function as averaging)
k1 l1

–  h(k , l )  1 (the mean value is preserved)


k  k 0 l  l0

Yao Wang, NYU-Poly EL5123: Spatial Filtering 16


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 a llow pass filt
Applying filter removes th
the hi
high-frequency
hf partt off the
th
noise.
• Image edges and other details are blurred

Yao Wang, NYU-Poly EL5123: Spatial Filtering 17


Interpretation in Freq Domain

Filter response

Low-passed image spectrum

Original image spectrum

Noise spectrum

0 f

Yao Wang, NYU-Poly EL5123: Spatial Filtering 18


Spectrum of Example Low Pass Filterrs

1 1 1 1 1 b 1 1
H  1 1 1  11 1 1;
1 1 b b 2 b   b 1 b 1;
1 1
H
9 9 ( 2  b) 2  ( 2  b) 2  
1 1 1 1 b 1 1
1
H (u , v)  1  2 cos(2u ) 1  2 cos(2v)  / 9 H (u , v)  b  2 cos(2u ) b  2 cos(2v)  /( 2  b) 2

1
1

0.8
0.5
0.6

0 0.4

0.2
-0.5
30 0
30
25
20
20 25
15 20
20
10 10 15
5 10 10
0 0 5
0 0

b=2

Yao Wang, NYU-Poly EL5123: Spatial Filtering 19


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

• Di the i-th directional neighborhood

D3
• Usuallyy 4 or 8 directions
• 3 core steps
– Find σi2, the variance in Di D1
– Find i0, so that σi02 ≤σi2
– Filtering in i01direction
g (m, n) 
N io
 ( k ,l )Dio
f (m  k , n  l )

g (m, n)  f (m, n) * hi 0 (m, n)


p{(m cos  i  n sin  i ) 2 }
hi (m, n)  a exp{(

Yao Wang, NYU-Poly EL5123: Spatial Filtering 20


Noise Removal by Averaging Multiple
Images

Yao Wang, NYU-Poly EL5123: Spatial Filtering 21


Noise Removal by Averaging Multiple
Images
• Assume y = x + n, where n ~ N(0, σ2).
• Observe one image, the noise is n
• Observe N images: yi = x + ni, ii=1
1,…,N,
N where ni ~ N(0,
N(0 σ2),
) and ni
are independent.
N N N
1 1 1
y
N

i 1
yi 
N

i 1
( x  ni )  x 
N
n
i 1
i

y  x  n , where n ~ N (0,  2 / N )

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

Yao Wang, NYU-Poly EL5123: Spatial Filtering 22


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

Yao Wang, NYU-Poly EL5123: Spatial Filtering 23


Characterization of Edges
Ideal step edge

Real edge has a slope

First order derivative:


Maximum at edge location

Second order derivative:


Zero crossing at edge location

Yao Wang, NYU-Poly EL5123: Spatial Filtering 24


Edge Detection Based on First Order
Derivatives
High-pass filtering across edge
• Edge Low-pass filtering along edge

 1 1 1 
h 
 1  1  1

Non-Edge
Non Edge

No

g Yes
f H |g|>T Edge

What if we don’t know edge direction?

Yao Wang, NYU-Poly EL5123: Spatial Filtering 25


Edge Detection Based on Gradients in
Two Orthogonal Directions
• Combine results from directional edge
detectors in two orthogonal directions and
determine the magnitude and direction of
the edge
edge. Non-Edge

fx
g Edge
Hx fx2+fy2 |g|>T

Hy tan-1(fx/fy) theta
fy

Yao Wang, NYU-Poly EL5123: Spatial Filtering 26


Directional Edge Detector
• High-pass (or band-pass) in one direction (simulating
first order derivative)
• Low pass in the orthogonal direction (smooth noise)
LP BP
• Prewitt edge detector
 1  1  1  1  1 0 1 1
1  1  1  1 
H x   0 0 0    0 1 1 1; H y   1 0 1  1 1 0 1
3 3 3 3
 1 1 1   1   1 0 1 1
• Sobel edge detector
 1  2 1 1   1 0 1 1
1  1  1  1 
H x   0 0 0    0 1 2 1; H y   2 0 2  21 0 1
4 4 4 4
 1 2 1   1   1 0 1 1

The sobel filter provides better smoothing along the edge

Yao Wang, NYU-Poly EL5123: Spatial Filtering 27


Freq. Response of Sobel Filter
Sobel Filter for Horizontal Edges :
 1  2  1  1
H x   0 0    0 1 2 1
1 1
0
4 4
 1 2 1   1 
F
Frequency R
Response (DTFT) :

hx  [-1 0 1]  - H x   e j 2u  e  j 2u  2 j sin 2u 
4
1
4

h y  [1 2 1]  - H y  e j 2v  2  e  j 2v  1  cos 2v 
1 1
2

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

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

Band-pass
Low-pass
Yao Wang, NYU-Poly EL5123: Spatial Filtering 28
Spectrum of the Sobel Filter

10

5
Hx
0
40
40
20 20
0 0

10

5
Hy
0
40
40
20 20
0 0

L
Low pass along
l th
the edge,
d b
band
d pass cross th
the edge
d

Yao Wang, NYU-Poly EL5123: Spatial Filtering 29


Example of Sobel Edge Detector

Original image Filtered image by Hx Filtered image by Hy

Yao Wang, NYU-Poly EL5123: Spatial Filtering 30


How to set threshold?
• Trial and error
• According to edge magnit
magnitude
de distrib
distribution
tion
– E.g assuming only 5% pixels should be edge
pixels then the threshold should be the 95%
pixels,
percentile of the edge magnitude
– Illustrate on board

Yao Wang, NYU-Poly EL5123: Spatial Filtering 31


gx gy gm

Histogram of gm T=100 T=50 T=20


2500

2000

1500

1000

500

0
0 100 200

Yao Wang, NYU-Poly EL5123: Spatial Filtering 32


Problems of previous approach
• Cannot locate edges precisely
• Ramp edges can lead to many edge pixels
detected depending on the threshold T
– T too high:
g may y not detect weak edges
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,
edge or try all possible 8 direction in a 3x3
neighbor
– Onlyy consider pixels
p with |g| > T

Yao Wang, NYU-Poly EL5123: Spatial Filtering 33


Edge Detection with Many Directional
Filters
• Instead of using two orthogonal directions,
can design multiple directional filters
– 0, 45, 90, 135
• S
See which
hi h one gives
i th
the hi
highest
h t response
in the normal direction

Yao Wang, NYU-Poly EL5123: Spatial Filtering 34


Edge Detection Based on Second Order
Derivative
• Convolve an image with a filter
corresponding to taking second order
derivative (e.g. Laplacian or LoG operator)
• Locate zero
zero-crossing
crossing in the filtered image

Yao Wang, NYU-Poly EL5123: Spatial Filtering 35


Laplacian Operator
2 f 2 f
  2  2
2

x y
f

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

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

 2f   f ( x  1, y )  f ( x  1, y )  f ( x, y  1)  f ( x, y  1)  4 f ( x, y )

0 1 0   0  1 0 
1  4 1;  1 4  1;
   
0 1 0  0  1 0 

Yao Wang, NYU-Poly EL5123: Spatial Filtering 36


Fourier Transform of Laplacian Operator

 0 1 0   1  1  1
 1 4  1  1 8  1
   
 0  1 0   1  1  1

6 12

10
4
8

2 6

4
0
30 2

0
20 30
25
10 20
25 20
20 15
15 10
10 10
0 5 5
0
0 0

More isotropic

Yao Wang, NYU-Poly EL5123: Spatial Filtering 37


Isotropic Edge Detector
• Laplacian operator can detect changes in
all directions

Yao Wang, NYU-Poly EL5123: Spatial Filtering 38


Pros and Cons
• Can locate edges more accurately
• Can detect edges in various
ario s direction
• But more prone to noise
• Remedy:
– Smoothing
g before applying
pp y g Laplacian
p

Yao Wang, NYU-Poly EL5123: Spatial Filtering 39


Laplacian of Gaussian (LoG)
• To suppress noise, smooth the signal
using a Gaussian filter first
– F(x,y)* G(x,y)
G
• Then
Th apply
l L
Laplacian
l i filt
filter
– F(x,y)*G(x,y)*L(x,y) = F(x,y)* (L(x,y)*G(x,y))
• Equivalent filter: LoG
– H(x,y)=L(x,y)*G(x,y)

Yao Wang, NYU-Poly EL5123: Spatial Filtering 40


Derivation of LoG Filter
• Analog form
x2  y2

G ( x, y )  e 2 2

x2  y2
 2G  2G x 2  y 2  2 2 
2 2
 G ( x, y ) 
2
  e
2x 2 y 4

• Take samples to create filter mask


– Size
Si off maskk nxn, n>=6,
> 6 odd
dd
– Exn

Yao Wang, NYU-Poly EL5123: Spatial Filtering 41


LoG Filter

Yao Wang, NYU-Poly EL5123: Spatial Filtering 42


Laplacian filtered LOG filtered

Note that each strong g edge


g in the original
g image
g corresponds
p to a thin stripe
p
with high intensity in one side and low intensity in the other side.

Yao Wang, NYU-Poly EL5123: Spatial Filtering 43


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

Yao Wang, NYU-Poly EL5123: Spatial Filtering 44


Example

Yao Wang, NYU-Poly EL5123: Spatial Filtering 45


Summary of Edge Detection Method
• First order gradient based:
– Using edge detectors in two orthogonal directions
• For each direction: low-pass along edge, high-pass across
edge
– Using edge detectors in multiple (>2) directions
– Use threshold or detect local maximum across the
edge
g direction
• Second order gradient based
– Laplacian
p is noise-prone,
p LoG is better
– Detect zero crossing
– Isotropic

Yao Wang, NYU-Poly EL5123: Spatial Filtering 46


More on Edge Detection
• Methods discussed so far generally cannot yield
connected thin edge maps
• Need sophisticated post processing
– Thinning
– Connecting broken lines
• Noise can lead to many false edge points
• Even with many years of research, no perfect
edge
d d detectors
t t exist!
i t!
– 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


Results using MATLAB “edge( )” function
Sobel,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


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
image
• Line structures and edges can be obtained by
applying 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


Interpretation in Freq Domain
Filter response (high emphasis)

sharpened image spectrum

Original image spectrum

0 f

Yao Wang, NYU-Poly EL5123: Spatial Filtering 50


Designing Sharpening Filter Using High
Pass Filters
• The desired image is the original plus an
appropriately scaled high-passed image
• Sharpening filter

f s  f  f h

hs (m, n)   (m, n)  hh (m, n)

Yao Wang, NYU-Poly EL5123: Spatial Filtering 51


Interpretation in Freq Domain
high emphasis=allpass+highpass

all pass

high pass

0 f

Yao Wang, NYU-Poly EL5123: Spatial Filtering 52


How to design high pass filter
• High pass filter tries to detect changes
• Difference between a current pixel and its
neighbors
– Some coefficients should be negative
g
• Output should be zero if the image is flat
k1 l1

 h(k , l )  0
k  k 0 l  l0

• More rigorous design would start with desired


frequency response and take inverse FT.

Yao Wang, NYU-Poly EL5123: Spatial Filtering 53


Using Laplacian Operator as Highpass
Filter
 0 1 0   1  1  1
 1 4  1  1 8  1
   
 0  1 0   1  1  1

6 12

10
4
8

2 6

4
0
30 2

0
20 30
25
10 20
25 20
20 15
15 10
10 10
0 5 5
0
0 0

More isotropic

Yao Wang, NYU-Poly EL5123: Spatial Filtering 54


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

x x x

 0 1 0   0 1 0 
H h   1 4  1  H s   1 8  1 with   1.
1 1
4 4
 0  1 0   0  1 0 

 1  1  1  1  1  1
H h   1 8  1  H s   1 16  1 with   1.
1 1
8 8
 1  1  1  1  1  1

Yao Wang, NYU-Poly EL5123: Spatial Filtering 55


Example of Sharpening Using Laplacian
Operator

 0 1 0 
H h   1 8  1
1
4 
 0  1 0 

Yao Wang, NYU-Poly EL5123: Spatial Filtering 56


Example of Sharpening Using Laplacian
Operator

4

 8

Yao Wang, NYU-Poly EL5123: Spatial Filtering 57


Designing Sharpening Filtering Using Low
Pass Filters ((Unsharp
p Masking)
g)
• The sharpened = original – a*lowpass
fs 
1
 f  f l , 0    1
1 

• Sharpening filter

hs (m, n) 
1
 (m, n)  hl (m, n) 
1 

Yao Wang, NYU-Poly EL5123: Spatial Filtering 58


Interpretation in Freq Domain
high emphasis=allpass- a*lowpass

all pass

low pass

0 f

Yao Wang, NYU-Poly EL5123: Spatial Filtering 59


Example

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

x x x

1 2 1    1  2  1
1   1   1
H l   2 4 2  H s    2 60  2 with   .
16 48 4
1 2 1    1  2  1 

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

Yao Wang, NYU-Poly EL5123: Spatial Filtering 60


Example

Original image Sharpened image

Yao Wang, NYU-Poly EL5123: Spatial Filtering 61


Challenges of Noise Removal and Image
Sharpening
p g
• How to smooth the noise without blurring
the details too much?
• How to enhance edges without amplifying
noise?
• Still a active research area
– Wavelet domain processing

Yao Wang, NYU-Poly EL5123: Spatial Filtering 62


Wavelet-Domain Filtering

Courtesy of Ivan Selesnick

Yao Wang, NYU-Poly EL5123: Spatial Filtering 63


Feature Enhancement by
Subtraction

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


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 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
coefficients, and also through DTFT

Yao Wang, NYU-Poly EL5123: Spatial Filtering 65


Reading
• R. Gonzalez, “Digital Image Processing”,
Section 3
3.4
4~33.8,
8 10
10.1,
1 10.2
10 2
– Note that in Gonzalez book: Edge detection
filtering are referred to sharpening
sharpening.
• A. K. Jain, “Fundamentals of Digital Image
Processing ” Section 7
Processing, 7.4
4 (except median
filter).

Yao Wang, NYU-Poly EL5123: Spatial Filtering 66


Written Assignment
1. 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,
yes compute the FT separately and explain the function of each
1D filter. If not, compute the FT directly.

1 2 1   1  1  1  0 1 0 
H 1  2 4 2 H 2   1 8  1;
1
H 3   1 5  1
16
1 2 1   1  1  1  0  1 0 

Yao Wang, NYU-Poly EL5123: Spatial Filtering 67


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 by 
x2  y2

G ( x, y )  e 2 2

show that the LOG filter can be written as


x2  y2
x 2  y 2  2 2 
L( x , y )  e 2 2
4
3
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


Computer Assignment
1. 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 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 g m  g x  g y ; and iii) For a chosen
2 2

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
u t you find
d a T tthat
at gives
g es you a good result.
esu t Hand
a d in you
your p
program
og a aand
d tthe
e edge maps
aps
generated 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
matlab conv2( ) function or write your own code for the filtering part.

Yao Wang, NYU-Poly EL5123: Spatial Filtering 69


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/imdemos

Yao Wang, NYU-Poly EL5123: Spatial Filtering 70

You might also like