Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one •...

59
Edges, interpolation, t lt templates Nuno Vasconcelos ECE Department, UCSD (with thanks to David Forsyth)

Transcript of Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one •...

Page 1: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

Edges, interpolation, t l ttemplates

Nuno Vasconcelos ECE Department, UCSDp ,

(with thanks to David Forsyth)

Page 2: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

Edge detectionedge detection has many applicationsin image processingg p gan edge detector implements thefollowing steps:• compute gradient magnitude

2

00

2

002

00 ),(),(),( ⎟⎠

⎞⎜⎜⎝

⎛∂∂

+⎟⎠⎞

⎜⎝⎛∂∂

=∇ yxyfyx

xfyxf

• thin and follow edge points• find locations of maximum gradient

⎠⎜⎝ ∂⎠⎝ ∂ yx

gmagnitude

• follow these maxima to form contours• discard points that are not maxima

2

p• declare maxima as edges

Page 3: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

Derivativesto compute the derivatives

( ) ⎞⎛ ∂∂ ff

we rely on a sequence of

( ) ⎟⎟⎠

⎞⎜⎜⎝

⎛∂∂

∂∂

= ),(),,(),(),,( 0000 yxyfyx

xfyxfyxf yx

we rely on a sequence of • smoothing with a Gaussian (to eliminate noise)• convolution with difference filter• fx: 0 0

1 -10 0

1 -11 -11 -1 n

n2

• fy: 0 0 1 1 n1

0 -1 00 1 0

-1 -1 -11 1 1

n2

3

0 1 0 1 1 1 n1

Page 4: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

Derivativesaccomplished in a single step• by convolving image with two derivative of a

DoG along n1

by convolving image with two derivative of a Gaussian (DoG) filters

),(),1(),( 212121 nngnngnnhx −+=

• where DoG along n2

),(),(),( 212121 ggx

),()1,(),( 212121 nngnngnnhy −+=

⎟⎟⎠

⎞⎜⎜⎝

⎛ +−= 2

22

21

221 2exp

21),(

σπσnnnng

⎠⎝

4

Page 5: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

The Canny edge detector

i i l i (L )

5

original image (Lena)

Page 6: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

The Canny edge detector

6

norm of the gradient

Page 7: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

Non-maximum suppressionis there a maximum at q?yes if value at q is largeryes, if value at q is larger than those at both p and rp and r are the pixels in the p pdirection of the gradient that are 1 pixel apart from qt i ll th d t f ll itypically they do not fall in the pixel gridwe need to interpolate e gwe need to interpolate, e.g.

abr )1( αα −+=α 1−α

a b

7

Page 8: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

Predicting the next edge pointassume the marked point is an edge pointg pwe construct the tangent to the edge curve (which is

l t th di t tnormal to the gradient at that point)

( )Tfft )()()(

use this to predict the next points (here either r or s)

( )xy yxfyxfyxt ),(),,(),( −=

points (here either r or s).

8

Page 9: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

Cleaning upeven when gradient is ~ zero, there are maxima d t idue to noisecheck that maximum value of gradient value is large g genough (threshold)once we are following an edge we must avoid gapsedge we must avoid gaps due to similarity with background

h t iuse hysteresis• use a high threshold to start

edge curves and a low th h ld t ti th

9

threshold to continue them.

Page 10: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

The Canny edge detector

i i l i (L )

10

original image (Lena)

Page 11: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

The Canny edge detector

11

norm of the gradient

Page 12: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

The Canny edge detector

12

thinning(non-maximum suppression)

Page 13: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

Hysteresissuppose this is a curve that we are following• thickness represents the magnitude of the gradient

• we require a large magnitude to start, i.e. above a threshold T1

• once we start we keep going even if the magnitude falls below the thresholdthe threshold

• we only declare the contour as done if it falls below a second threshold T2, where T2, <T1

13

• once again, the optimal values of these thresholds are image dependent

Page 14: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

Parameter tuningin summary, the combination of • smoothed derivatives,

d t ti f i f di t it d• detection of maxima of gradient magnitude, • edge following

is the essence of most modern edge detectorsgthe classical is the “Canny edge detector” which implements all this steps

h h b fas we have seen there are a number of parameters• smoothing scale• two hysteresis thresholdstwo hysteresis thresholds

in practice these can have significant effect on the quality of the resulting edge maps

14

unfortunately there are no universally good values

Page 15: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

15

Page 16: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

fine scalehigh threshold

too manyfalse dedges

16

Page 17: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

coarse scale,high thresholdthreshold

we looseedgeedge points

17

Page 18: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

coarsescalescalelowthreshold

we stillhave gapsbut onceagain falsefalseedges

18

Page 19: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

The Canny edge detectorthere are many implementations available• matlab has onematlab has one• there is freely available C code on the web• there are various applets that allow you to play with the

parameters• an example is• http://www cs washington edu/research/imagedatabase/demo/edhttp://www.cs.washington.edu/research/imagedatabase/demo/ed

ge/• make sure you experiment and get a feel for how the parameters

influence the edge detection resultsinfluence the edge detection results• the Canny edge detectors is the closest that you will find to a

standard solution to a vision problem

19

Page 20: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

problem: various parameters, for all values we tried result was not perfect

20

Page 21: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

Effects of noiseIs there an alternative?• recall we followed this path to overcome the noise problem

21

are there other alternatives?

Page 22: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

Solution: smooth first

22this is what we get with 1st order derivatives

Page 23: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

Derivative theorem of convolution

23

can we extend this idea?

Page 24: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

Laplacian of GaussianConsider

Laplacian of Gaussianoperatoroperator

24where is the edge? zero-crossings of bottom graph

Page 25: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

The Laplacian of Gaussiananother way to detect max of first derivative is to look for a zero second derivative2D analogy is the Laplacian

with second-order derivatives

),(),(),( 2

2

2

22 yx

yfyx

xfyxf

∂∂

+∂∂

=∇

with second order derivatives, noise is even greater concernsmoothing• smooth with Gaussian, apply

Laplacian• this is the same as filtering with a

),(2 yxGσ∇

25

• this is the same as filtering with a Laplacian of Gaussian filter

Page 26: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

2D edge detection filters

L l i f G iLaplacian of Gaussian

Gaussian derivative of Gaussian

is the Laplacian operator:is the Laplacian operator:

26

Page 27: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

The Laplacian of Gaussianthis is very close to what the early stages of the brain seem to be doingrecordingsof retinalganglion cellsganglion cellscalled“center-surround”cellstwo types:two types:• on-center• off-center

27

Page 28: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

Edge detection strategyfilter with Laplacian of Gaussiandetect zero crossingsdetect zero crossingsmark the zero points where:• there is a sufficiently large derivative

LoG

• there is a sufficiently large derivative,• and enough contrast

once again we have parametersZD

g p• scale of Gaussian smoothing• thresholds

once again no set of universal parametersdoes not seem to be better than the strategy of looking for ma ima of gradient magnit de

28

for maxima of gradient magnitude.

Page 29: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

sigma=4

contrast=1 contrast=4LOG zero crossings

sigma=2

29

Page 30: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

Non-maximum suppressionwe have seen that to find if q is a maximumqwe need to know what is the image value at rbut this does not fall on the pixel gridthi i ll d i t l tithis is called interpolationit is a very frequent operation in imageoperation in image processing α 1−α

a b

30

Page 31: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

Interpolationthe most obvious application is to improve the resolution

image super-resolved

t th i d d t il th d d tif t th

31

note the increased detail, e.g. the reduced artifacts on the lines

Page 32: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

Interpolationbut there are many otherse g the restoration of degraded moviese.g. the restoration of degraded movies

32

Page 33: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

Interpolationimage synthesis

33

Page 34: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

Interpolationtexture mapping

34

Page 35: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

Interpolationhow does one do this?the simplest method is nearest neighbor interpolationthe simplest method is nearest-neighbor interpolationwe simply replicate the image intensity(or color) of the closest pixel( ) pe.g. in this case, because the desired location p is closest to (x,y+1)

(x,y+1) (x+1,y+1)

p

we make (x,y) (x+1,y)

)1,()( += yxIpIthis is not very good because it generates artifacts• one location replicated from one pixel

),()( yp

35

o e ocat o ep cated o o e p e• an infinitesimally close neighbor replicated from another

Page 36: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

Interpolationmuch better is bilinear interpolationassume image varies linearly weight each pixelassume image varies linearly, weight each pixel according to their distance to plet a = px – x, b = py – y and makepx , py y

)1,()1()( +××−= yxIbapI (x,y+1) (x+1,y+1)

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

+×−×+×−×−+

yxIbayxIba

(x,y) (x+1,y)

p

a

b

)1,1( ++××+ yxIbaa

36

works much better than nearest neighbor

Page 37: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

Interpolationnote that these can be implemented with filteringfor nearest neighborsfor nearest neighbors

37

Page 38: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

Interpolationfor bilinear interpolation

38

Page 39: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

Interpolationand there are obviously many other filtersthe best method is frequently bi cubic interpolationthe best method is frequently bi-cubic interpolation

39

Page 40: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

Interpolationhow do the three methods compare?iimageinterpolatedwith nearest neighbor

40

Page 41: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

Interpolationhow do the three methods compare?iimageinterpolatedwith bilinearmethod

41

Page 42: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

Interpolationhow do the three methods compare?iimageinterpolatedwith bi-cubicmethod

42

Page 43: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

Interpolationso, what method should I use?• the higher order the filter, the more computation requiredg , p q• the gains are diminishing after some point• bilinear usually justified over nearest neighbor

bi bi ti th it b t j d b b i• bi-cubic sometimes worth it, but judge on a case by case basis• higher order than cubic is usually not worth it

to play with this:p y• the matlab interp2 function implements all the methods• plus a spline-based method that we will not get into

d l t t• very good applet at

http://www.s2.chalmers.se/research/image/Java/NewApplets/Interpolation/index.htm

43

p

Page 44: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

Filters as templatesapplying a filter at some point can be seen as taking a dot-product between the image and some vectorp gfiltering the image is a set of dot productsinsight g• filters look like the effects they are intended to find• filters find effects they look like

44

Page 45: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

Positive responses

45

Page 46: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

46

Page 47: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

The z transformonce again, it is a straightforward extension of 1DDefinition: the z transform of the sequence x[n n ] isDefinition: the z-transform of the sequence x[n1,n2] is

21

1 2

212121 ],[),( nn

n nzznnxzzX −−∑∑=

the region of the (z1,z2) plane where this sum is finite is called the Region of Convergence (ROC)

1 2

called the Region of Convergence (ROC)it turns out that:• in 2D the ROC is much more complicated than in 1Dp• while in 1D the ROC is bounded by poles (0D subspace of the 2D

complex plane)in 2D is bounded by pole surfaces (2D subspaces of the 4D

47

• in 2D is bounded by pole surfaces (2D subspaces of the 4D space of two complex variables)

Page 48: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

The z-transformcomputation is also much harder:• as you might remember from 1D• most useful tool in computing z-transforms is polynomial

factorization• z-transform is a ratio of two polynomialsp y

)()()(

zDzNzY =

• we factor in to a sum of low order terms, e.g.

1

d th i t h f th t t t [ ]

∑ −−=

i i zazY 11

1)(

48

• and then invert each of the terms to get y[n]

Page 49: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

z-transformin 2D we only have one of two situations1) the sequence is separable, in which case everything

reduces to the 1D case

d)(f)()(),(][][],[ 221121221121

XROCROCzXzXzzXnxnxnnx =↔=

)( of

and )(of:

222

111

zXROCz

zXROCzROC

the proof is identical to that of the DSFT2) the signal is not separable• here our polynomials are of the form z1

mz2n and, in general, it is not

know how to factor them• we can solve only if sequence is simple enough that we can do it

49

we can solve only if sequence is simple enough that we can do it by inspection (from the definition of the z-transform)

Page 50: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

Exampleconsider the sequence

][][ 212121 nnubannx nn=

the z-transform is

],[],[ 2121 nnubannx

( ) ( )

( ) ( )

azazzzXn n

nn=

∞ ∞

=

=

−−∑∑0 0

12

1121 ),(

1 2

21

ROC

|z2|

( ) ( )azazn n

nn=

=

=

−−∑ ∑0 0

12

11

11

1 2

21 ROC

b

bzazbzaz

>>−−

= −− 2112

11

, ,1

11

1 |z1|a

50

Page 51: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

Sampling in 2Dconsider an analog signal xc(t1,t2) and let its analog Fourier transform be Xc(Ω1,Ω2)c( 1, 2)• we use capital Ω to emphasize that this is analog frequency

sample with period (T1,T2) to obtain a discrete-space signal

222111 ;2121 ),(],[TntTntc ttxnnx

===

51

Page 52: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

Sampling in 2Drelationship between the Discrete-Space FT of x[n1,n2]and the FT of xc(t1,t2) is simple extension of 1D resultc( 1, 2) p

∑ ∑∞ ∞

⎟⎠

⎞⎜⎜⎝

⎛ −−= 1111

212,21),( c T

rT

rXTT

X πωπωωω

DSFT of x[n1,n2] FT of xc(ω1,ω2) “discrete spectrum” “analog spectrum”

−∞= −∞= ⎠⎝1 2 1121 r r TTTT

discrete spectrum analog spectrum

Discrete Space spectrum is sum of replicas of analog p p p gspectrum• in the “base replica” the analog frequency Ω1 (Ω2) is mapped into

the digital frequency Ω1T1 (Ω2T2)

52

the digital frequency Ω1T1 (Ω2T2)• discrete spectrum has periodicity (2π,2π)

Page 53: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

For exampleΩ2

Ω’

Ω”ω2

Ω’

Ω1

Ω”T2

ω1

2π−Ω”T2

2

1

''''''

TT

Ω=→ΩΩ=→Ω

βα

ω1

Ω’T1

2π2π

no aliasing if2π−Ω’T1

⎧ Ω≤Ω '2' TT π

ω1

2π⇔⎩⎨⎧

Ω−≤ΩΩ−≤Ω

22

11

'2''2

TTTT

ππ

⎨⎧ Ω≤

⇔'/1 πT

53

⎩⎨ Ω≤

⇔''/2 πT

Page 54: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

Aliasingthe frequency (Ω’/π,Ω’’/π)is the critical sampling freq encfrequencybelow it we havealiasing

ω2

aliasing

ω1

this is just like the 1D case, but now there are more possibilities for overlap

54

Page 55: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

Reconstructionif there is no aliasing we can recover the signal in a way similar to the 1D case

( ) ( )∑ ∑∞ ∞

−−=

2222

1111

2121

sinsin],[),(c

TntT

TntTnnxtty π

π

π

π

note: in 2D there are many more possibilities than in 1D

( ) ( )∑ ∑−∞= −∞= −−1 2

2222

1111

n n TntT

TntT

ππ

note: in 2D there are many more possibilities than in 1D• e.g. the sampling grid does not have to be rectangular, e.g.

hexagonal sampling when T2 = T1/sqrt(3) and

⎩⎨⎧

= ==

otherwise0odd or even both21;21

21,),(

],[ 222111nnttx

nnx TntTntc

55

• in practice, however, one usually adopts the rectangular grid

Page 56: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

a sequence of images obtained by downobtained by down-sampling without any filteringaliasing: the low-frequency parts are replicated throughout the

56

replicated throughout the low-res image

Page 57: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

The role of smoothing

none

some

l ta lot

57

too little leads to aliasingtoo much leads to loss of information

Page 58: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

Aliasing in videovideo frames are the result of temporal sampling• fast moving objects are above the critical frequency• above a certain speed they are aliased and appear to move

backwards• this was common in old western movies and become known as

the “wagon wheel” effect• here is an example: super-resolution increases the frame rate

and eliminates aliasinggfrom “Space-TimeResolution in Video” by yE. Shechtman, Y. Caspi and M. Irani

(PAMI 2005).

58

Page 59: Edges, interpolation, tlttemplates - · PDF file• matlab has onematlab has one • there is freely available C code on the web ... Interpolation much better is bilinear interpolation

59