filtering and edge detection. local neighborhoods hard to tell anything from a single pixelhard to...

36
Filtering and Edge Filtering and Edge Detection Detection

Post on 20-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Filtering and Edge DetectionFiltering and Edge Detection

Local NeighborhoodsLocal Neighborhoods

• Hard to tell anything from a single pixelHard to tell anything from a single pixel– Example: you see a reddish pixel. Is this Example: you see a reddish pixel. Is this

the object’s color? Illumination? Noise?the object’s color? Illumination? Noise?

• The next step in order of complexity is The next step in order of complexity is to look at local neighborhood of a pixelto look at local neighborhood of a pixel

Linear FiltersLinear Filters

• Given an image Given an image InIn((xx,,yy) generate a) generate anew image new image OutOut((xx,,yy):):– For each pixel (For each pixel (xx,,yy), ), OutOut((xx,,yy) is a linear ) is a linear

combination of pixels in the neighborhood combination of pixels in the neighborhood of of InIn((xx,,yy))

• This algorithm isThis algorithm is– Linear in input intensityLinear in input intensity

– Shift invariantShift invariant

Discrete ConvolutionDiscrete Convolution

• This is the discrete analogue of This is the discrete analogue of convolutionconvolution

• Pattern of weights = “filter kernel”Pattern of weights = “filter kernel”

• Will be useful in smoothing, edge Will be useful in smoothing, edge detectiondetection

t

dttxgtfxgxf )()()()(

t

dttxgtfxgxf )()()()(

Example: SmoothingExample: Smoothing

Original: MandrillOriginal: Mandrill Smoothed withSmoothed withGaussian kernelGaussian kernel

Gaussian FiltersGaussian Filters

• One-dimensional GaussianOne-dimensional Gaussian

• Two-dimensional GaussianTwo-dimensional Gaussian

2

2

21

2

1)(

x

exG

2

2

21

2

1)(

x

exG

2

22

22

2

1),(

yx

eyxG

2

22

22

2

1),(

yx

eyxG

Gaussian FiltersGaussian Filters

Gaussian FiltersGaussian Filters

Gaussian FiltersGaussian Filters

• Gaussians are used because:Gaussians are used because:– SmoothSmooth

– Decay to zero rapidlyDecay to zero rapidly

– Simple analytic formulaSimple analytic formula

– Limit of applying multiple filters is GaussianLimit of applying multiple filters is Gaussian(Central limit theorem)(Central limit theorem)

– Separable:Separable:

G G22(x,y) = G(x,y) = G11(x) G(x) G11(y)(y)

Computing Discrete ConvolutionsComputing Discrete Convolutions

• What happens near edges of image?What happens near edges of image?– Ignore (Ignore (OutOut is smaller than is smaller than InIn))

– Pad with zeros (edges get dark)Pad with zeros (edges get dark)

– Replicate edge pixelsReplicate edge pixels

– Wrap aroundWrap around

– ReflectReflect

– Change filterChange filter

i j

jyixInjifyxOut ),(),(),( i j

jyixInjifyxOut ),(),(),(

Computing Discrete ConvolutionsComputing Discrete Convolutions

• If If InIn is is nnnn, f is , f is mmmm, takes time, takes time O( O(mm22nn22))

• OK for small filter kernels, bad for large OK for small filter kernels, bad for large onesones

i j

jyixInjifyxOut ),(),(),( i j

jyixInjifyxOut ),(),(),(

Fourier TransformsFourier Transforms

• Define Define Fourier transformFourier transform of function of function ff as as

• F is a function of frequency – describes F is a function of frequency – describes how much of each frequency how much of each frequency ff contains contains

• Fourier transform is invertibleFourier transform is invertible

dxexf xixf

)(

2

1)(F )(F dxexf xixf

)(

2

1)(F )(F

Fourier Transform and Fourier Transform and ConvolutionConvolution

• Fourier transform turns convolutionFourier transform turns convolutioninto multiplication:into multiplication:

FF ((ff((xx) ) gg((xx)))) = = FF ((ff((xx)))) FF

((gg((xx))))

Fourier Transform and Fourier Transform and ConvolutionConvolution

• Useful application #1: Use frequency Useful application #1: Use frequency space to understand effects of filtersspace to understand effects of filters– Example: Fourier transform of a GaussianExample: Fourier transform of a Gaussian

is a Gaussianis a Gaussian

– Thus: attenuates high frequenciesThus: attenuates high frequencies

==

FrequencyFrequency

Am

plit

ude

Am

plit

ude

FrequencyFrequency

Am

plit

ude

Am

plit

ude

FrequencyFrequency

Am

plit

ude

Am

plit

ude

Fourier Transform and Fourier Transform and ConvolutionConvolution

• Useful application #2: Efficient Useful application #2: Efficient computationcomputation– Fast Fourier Transform (FFT) takes timeFast Fourier Transform (FFT) takes time

O( O(n n log log nn))

– Thus, convolution can be performed in timeThus, convolution can be performed in time O( O(nn log log nn + + mm log log mm))

– Greatest efficiency gains for large filtersGreatest efficiency gains for large filters

Edge DetectionEdge Detection

• What do we mean by edge detection?What do we mean by edge detection?

• What is an edge?What is an edge?

What is an Edge?What is an Edge?

Edge easy to findEdge easy to find

What is an Edge?What is an Edge?

Where is edge? Single pixel wide or multiple pixels?Where is edge? Single pixel wide or multiple pixels?

What is an Edge?What is an Edge?

Noise: have to distinguish noise from actual edgeNoise: have to distinguish noise from actual edge

What is an Edge?What is an Edge?

Is this one edge or two?Is this one edge or two?

What is an Edge?What is an Edge?

Texture discontinuityTexture discontinuity

Formalizing Edge DetectionFormalizing Edge Detection

• Look for strong step edgesLook for strong step edges

• One pixel wide: look for One pixel wide: look for maximamaxima in in dI dI / / dxdx

• Noise rejection: smooth (with a Noise rejection: smooth (with a Gaussian) over a neighborhood Gaussian) over a neighborhood

dx

dI dx

dI

Canny Edge DetectorCanny Edge Detector

• SmoothSmooth

• Find derivativeFind derivative

• Find maximaFind maxima

• ThresholdThreshold

Canny Edge DetectorCanny Edge Detector

• First, smooth with a Gaussian ofFirst, smooth with a Gaussian ofsome width some width

Canny Edge DetectorCanny Edge Detector

• Next, find “derivative”Next, find “derivative”

• What is derivative in 2D? Gradient:What is derivative in 2D? Gradient:

y

f

x

fyxf ,),(

y

f

x

fyxf ,),(

Canny Edge DetectorCanny Edge Detector

• Useful fact #1: differentiationUseful fact #1: differentiation“commutes” with convolution“commutes” with convolution

• Useful fact #2: Gaussian isUseful fact #2: Gaussian isseparable:separable:

gdx

dfgf

dx

d g

dx

dfgf

dx

d

)()(),( 112 yGxGyxG )()(),( 112 yGxGyxG

Canny Edge DetectorCanny Edge Detector

• Thus, combine first two stages of Thus, combine first two stages of Canny:Canny:

)()(),(

)()(),(

)()(),(

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

11

11

11

112 yGxGyxf

yGxGyxf

yGxGyxf

yGxGyxfyxGyxf

)()(),(

)()(),(

)()(),(

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

11

11

11

112 yGxGyxf

yGxGyxf

yGxGyxf

yGxGyxfyxGyxf

Canny Edge DetectorCanny Edge Detector

Original ImageOriginal Image Smoothed Gradient MagnitudeSmoothed Gradient Magnitude

Canny Edge DetectorCanny Edge Detector

• Nonmaximum suppressionNonmaximum suppression– Eliminate all but local maxima in Eliminate all but local maxima in magnitudemagnitude

of gradientof gradient

– At each pixel look along At each pixel look along direction direction of of gradient:gradient:if either neighbor is bigger, set to zeroif either neighbor is bigger, set to zero

– In practice, quantize direction to horizontal, In practice, quantize direction to horizontal, vertical, and two diagonalsvertical, and two diagonals

– Result: “thinned edge image”Result: “thinned edge image”

Canny Edge DetectorCanny Edge Detector

• Final stage: thresholdingFinal stage: thresholding

• Simplest: use a single thresholdSimplest: use a single threshold

• Better: use two thresholdsBetter: use two thresholds– Find chains of edge pixels, all greater than Find chains of edge pixels, all greater than

lowlow

– Each chain must contain at least one pixel Each chain must contain at least one pixel greater than greater than high high

– Helps eliminate dropouts in chains, without Helps eliminate dropouts in chains, without being too susceptible to noisebeing too susceptible to noise

– ““Thresholding with hysteresis”Thresholding with hysteresis”

Canny Edge DetectorCanny Edge Detector

Original ImageOriginal Image EdgesEdges

Other Edge DetectorsOther Edge Detectors

• Can build simpler, faster edge detector Can build simpler, faster edge detector by omitting some steps:by omitting some steps:– No nonmaximum suppressionNo nonmaximum suppression

– No hysteresis in thresholdingNo hysteresis in thresholding

– Simpler filters (approx. to gradient of Simpler filters (approx. to gradient of Gaussian)Gaussian)

• Sobel:Sobel:

• Roberts:Roberts:

121

000

121

121

000

121

101

202

101

101

202

101

11

11

11

11

11

11

11

11

Second-Derivative-BasedSecond-Derivative-BasedEdge DetectorsEdge Detectors

• To find local maxima in derivative, look To find local maxima in derivative, look for zeros in second derivativefor zeros in second derivative

• Analogue in 2D: LaplacianAnalogue in 2D: Laplacian

• Marr-Hildreth edge detectorMarr-Hildreth edge detector

2

2

2

22 ),(

y

f

x

fyxf

2

2

2

22 ),(

y

f

x

fyxf

LOGLOG

• As before, combine Laplacian with As before, combine Laplacian with Gaussian smoothing: Laplacian of Gaussian smoothing: Laplacian of Gaussian (LOG)Gaussian (LOG)

LOGLOG

• As before, combine Laplacian with As before, combine Laplacian with Gaussian smoothing: Laplacian of Gaussian smoothing: Laplacian of Gaussian (LOG)Gaussian (LOG)

Problems withProblems withLaplacian Edge DetectorsLaplacian Edge Detectors

• Local minimum vs. local maximumLocal minimum vs. local maximum

• Symmetric – poor performance near Symmetric – poor performance near cornerscorners

• Sensitive to noiseSensitive to noise– Higher-order derivatives = greater noise sensitivityHigher-order derivatives = greater noise sensitivity

– Combines information along edge, not just Combines information along edge, not just perpendicularperpendicular