model fitting - nus computing - homecs6240/lecture/fitting.pdfhere, we look at some simple examples...

34
Model Fitting CS6240 Multimedia Analysis Leow Wee Kheng Department of Computer Science School of Computing National University of Singapore (CS6240) Model Fitting 1 / 34

Upload: others

Post on 27-May-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Model Fitting - NUS Computing - Homecs6240/lecture/fitting.pdfHere, we look at some simple examples as illustrations. (CS6240) Model Fitting 10 / 34 Fitting to Image Content Point

Model Fitting

CS6240 Multimedia Analysis

Leow Wee Kheng

Department of Computer Science

School of Computing

National University of Singapore

(CS6240) Model Fitting 1 / 34

Page 2: Model Fitting - NUS Computing - Homecs6240/lecture/fitting.pdfHere, we look at some simple examples as illustrations. (CS6240) Model Fitting 10 / 34 Fitting to Image Content Point

Introduction

Introduction

Model fitting fits a model to input data.

It is equivalent to registration.

A model can be parametric or non-parametric.

Input data can be data points or images.

Let’s start with fitting of lines, planes, and curves to data points.

(CS6240) Model Fitting 2 / 34

Page 3: Model Fitting - NUS Computing - Homecs6240/lecture/fitting.pdfHere, we look at some simple examples as illustrations. (CS6240) Model Fitting 10 / 34 Fitting to Image Content Point

Fitting to Data Points Line Fitting

Line Fitting

Fitting of line in 2-D space can be stated as follows:

Given data points (xi, zi), i = 1, . . . , n, determine the line(i.e., linear function) f(x) that minimizes the error z − f(x).

This is also called linear regression.

x

i

z i

x i

z

e

(CS6240) Model Fitting 3 / 34

Page 4: Model Fitting - NUS Computing - Homecs6240/lecture/fitting.pdfHere, we look at some simple examples as illustrations. (CS6240) Model Fitting 10 / 34 Fitting to Image Content Point

Fitting to Data Points Line Fitting

Equation of a line in 2-D space is

z = a1x+ a2. (1)

The difference ei = zi − (a1xi + a2) is the fitting error.

So, the problem is to find the line, parameterized by a1 and a2, thatminimizes the sum-squared error E

E =∑

i

‖zi − (a1xi + a2)‖2. (2)

To find the least-squared fitting line, combine Eq. 1 for all i:

z1...zn

=

x1 1...

...xn 1

[

a1a2

]

(3)

(CS6240) Model Fitting 4 / 34

Page 5: Model Fitting - NUS Computing - Homecs6240/lecture/fitting.pdfHere, we look at some simple examples as illustrations. (CS6240) Model Fitting 10 / 34 Fitting to Image Content Point

Fitting to Data Points Line Fitting

which can be written asZ = MA (4)

where

Z =

z1...zn

, M =

x1 1...

...xn 1

, A =

[

a1a2

]

.

Then, the least-square solution for A is given by

A = (M⊤M)−1M⊤Z. (5)

(CS6240) Model Fitting 5 / 34

Page 6: Model Fitting - NUS Computing - Homecs6240/lecture/fitting.pdfHere, we look at some simple examples as illustrations. (CS6240) Model Fitting 10 / 34 Fitting to Image Content Point

Fitting to Data Points Plane Fitting

Plane Fitting

The same method can be applied to the fitting of planes in 3-D space.

Equation of planes in 3-D space:

z = a1x+ a2 y + a3. (6)

Then, we obtain

z1...zn

=

x1 y1 1...

......

xn yn 1

a1a2a3

(7)

which can also be written as

Z = MA. (8)

(CS6240) Model Fitting 6 / 34

Page 7: Model Fitting - NUS Computing - Homecs6240/lecture/fitting.pdfHere, we look at some simple examples as illustrations. (CS6240) Model Fitting 10 / 34 Fitting to Image Content Point

Fitting to Data Points Curved Surface Fitting

Curved Surface Fitting

Consider data points (xi, yi, zi) that define a curved surface in 3-Dspace.

A simple way to represent the curved surface is by means of anorder-m polynomial function:

z =∑

k

l

akl xkyl, 0 ≤ k + l ≤ m. (9)

(CS6240) Model Fitting 7 / 34

Page 8: Model Fitting - NUS Computing - Homecs6240/lecture/fitting.pdfHere, we look at some simple examples as illustrations. (CS6240) Model Fitting 10 / 34 Fitting to Image Content Point

Fitting to Data Points Curved Surface Fitting

For example, a 2nd-order polynomial function is

z = a20x2 + a02y

2 + a11xy + a10x+ a01y + a00. (10)

Combining the equation for all i gives

z1...zn

=

x21 y21 x1y1 x1 y1 1...

......

......

...x2n y2n xnyn xn yn 1

a20...

a00

(11)

which can also be written as

Z = MA. (12)

So, fitting of polynomial curved surface in 3-D space is easy!

Exercise: Fit a polynomial curve in 2-D space.

(CS6240) Model Fitting 8 / 34

Page 9: Model Fitting - NUS Computing - Homecs6240/lecture/fitting.pdfHere, we look at some simple examples as illustrations. (CS6240) Model Fitting 10 / 34 Fitting to Image Content Point

Fitting to Data Points Curved Surface Fitting

Another general way to represent a curved surface is by means ofbasis functions hk(x, y), k = 1, . . . ,K:

z =K∑

k=1

akhk(x, y). (13)

Usually use radially symmetric functions calledradial basis functions.

Usually want basis functions to have finite support,i.e., have non-zero value over small domain.

Examples: Gaussian, splines, wavelets, etc.

(CS6240) Model Fitting 9 / 34

Page 10: Model Fitting - NUS Computing - Homecs6240/lecture/fitting.pdfHere, we look at some simple examples as illustrations. (CS6240) Model Fitting 10 / 34 Fitting to Image Content Point

Fitting to Image Content

Fitting to Image Content

Previous sections illustrate fitting of models to data points.

What about fitting models to image content?

This is more tedious because

digital image is a discrete object, and

image content can be very complex.

Here, we look at some simple examples as illustrations.

(CS6240) Model Fitting 10 / 34

Page 11: Model Fitting - NUS Computing - Homecs6240/lecture/fitting.pdfHere, we look at some simple examples as illustrations. (CS6240) Model Fitting 10 / 34 Fitting to Image Content Point

Fitting to Image Content Point Fitting

Point Fitting

The simplest example is to fit a model to a point in the image.

How does a point look like in an image?

(a) A point. (b) The enlarged image of a point.

A point usually occupies more than one pixel.

A point does not have sharp edges.The edges are smooth or blurred.

(CS6240) Model Fitting 11 / 34

Page 12: Model Fitting - NUS Computing - Homecs6240/lecture/fitting.pdfHere, we look at some simple examples as illustrations. (CS6240) Model Fitting 10 / 34 Fitting to Image Content Point

Fitting to Image Content Point Fitting

An appropriate model of a point is 2D Gaussian.

2D (unnormalized) Gaussian

g(x, y) = exp

(

−x2 + y2

2σ2

)

(14)

(CS6240) Model Fitting 12 / 34

Page 13: Model Fitting - NUS Computing - Homecs6240/lecture/fitting.pdfHere, we look at some simple examples as illustrations. (CS6240) Model Fitting 10 / 34 Fitting to Image Content Point

Fitting to Image Content Point Fitting

So, a point can be modeled by the 2D Gaussian G as follows:

G(x;θ) = G(x;A,B, σ,u) = A+B exp

(

−‖x− u‖2

2σ2

)

(15)

x = (x, y): any location in the image.

A: intensity of background (dark region).

B: peak intensity of point (brightest region).

u = (u, v): peak location, i.e., center of point.

σ: amount of spread of the Gaussian.

θ = (A,B, σ, u, v)⊤: parameters of the point model.

(CS6240) Model Fitting 13 / 34

Page 14: Model Fitting - NUS Computing - Homecs6240/lecture/fitting.pdfHere, we look at some simple examples as illustrations. (CS6240) Model Fitting 10 / 34 Fitting to Image Content Point

Fitting to Image Content Point Fitting

If the model G matches a point in image I perfectly, then

G(x;θ) = I(x) (16)

for all locations x within the model G.

So, the point fitting problem is to find the parameters θ that minimizethe error E:

E(θ) =1

2

x∈W

(G(x;θ)− I(x))2 (17)

where W is the extent of G (like a small window or template).

(CS6240) Model Fitting 14 / 34

Page 15: Model Fitting - NUS Computing - Homecs6240/lecture/fitting.pdfHere, we look at some simple examples as illustrations. (CS6240) Model Fitting 10 / 34 Fitting to Image Content Point

Fitting to Image Content Point Fitting

Since E is differentiable, we can compute the derivatives of E withrespect to the parameters, ∂E/∂θ = (∂E/∂A, . . . , ∂E/∂v)⊤.

∂E

∂A=

x∈W

(G(x;θ)− I(x))

∂E

∂B=

x∈W

(G(x;θ)− I(x)) exp

(

−‖x− u‖2

2σ2

)

∂E

∂u=

x∈W

(G(x;θ)− I(x)) exp

(

−‖x− u‖2

2σ2

)

x− u

σ2

∂E

∂σ=

x∈W

(G(x;θ)− I(x)) exp

(

−‖x− u‖2

2σ2

)

‖x− u‖2

σ3

(18)

(CS6240) Model Fitting 15 / 34

Page 16: Model Fitting - NUS Computing - Homecs6240/lecture/fitting.pdfHere, we look at some simple examples as illustrations. (CS6240) Model Fitting 10 / 34 Fitting to Image Content Point

Fitting to Image Content Point Fitting

It is difficult to solve ∂E/∂θ = 0 analytically for the minimum.In this case, we can apply gradient descent method as follows:

Initialize θ.Repeat until convergence

update θ in the direction of negative gradient of E:

∆θ = −η∂E

∂θ(19)

i.e.,

θ(t+ 1) = θ(t)− η∂E

∂θ. (20)

η is a constant update rate.

η should be small to ensure stability and convergence.

There are many possible convergence criteria, e.g.,E(t+ 1)− E(t) is small enough.

(CS6240) Model Fitting 16 / 34

Page 17: Model Fitting - NUS Computing - Homecs6240/lecture/fitting.pdfHere, we look at some simple examples as illustrations. (CS6240) Model Fitting 10 / 34 Fitting to Image Content Point

Fitting to Image Content Point Fitting

Intuitive Idea of Gradient Descent

Consider ∂E/∂A. Assume B = 0 for now.

When A > I, ∆A < 0, A is decreased towards I.

When A < I, ∆A > 0, A is increased towards I.

Notes:

Gradient descent changes the parameters towards the desiredvalues.

Can use this to check whether the signs of the equations arecorrect.

In theory, when the desired values are attained, the error E isminimized.

In practice, gradient descent can get trapped in local minimum.

(CS6240) Model Fitting 17 / 34

Page 18: Model Fitting - NUS Computing - Homecs6240/lecture/fitting.pdfHere, we look at some simple examples as illustrations. (CS6240) Model Fitting 10 / 34 Fitting to Image Content Point

Fitting to Image Content Point Fitting

Example: Fit a Gaussian model to a point in the image.

Actual parameter values: (A,B, u, v, σ) = (10, 170, 4.4, 3.7, 1.8)Initial estimate θ(0): (A,B, u, v, σ) = (0, 100, 5, 3, 1)

Optimization error E(θ) over iteration t:

0 10 20 30 40 50 60 1E-008 1E-007 1E-006 1E-005 0.0001

0.001 0.01

0.1 1

10 100

1000 10000

iteration

optimization error

(CS6240) Model Fitting 18 / 34

Page 19: Model Fitting - NUS Computing - Homecs6240/lecture/fitting.pdfHere, we look at some simple examples as illustrations. (CS6240) Model Fitting 10 / 34 Fitting to Image Content Point

Fitting to Image Content Point Fitting

Error of position (u, v) over iteration t:

0 10 20 30 40 50 60 0

0.2

0.4

0.6

0.8

1

1.2

iteration

position error

Need a good initial estimate. Otherwise, algorithm can gettrapped in a local minimum.

Since (u, v) can be real-valued, model fitting produces asub-pixel localization of the point in the image.

(CS6240) Model Fitting 19 / 34

Page 20: Model Fitting - NUS Computing - Homecs6240/lecture/fitting.pdfHere, we look at some simple examples as illustrations. (CS6240) Model Fitting 10 / 34 Fitting to Image Content Point

Fitting to Image Content Edge Fitting

Edge Fitting

Edge fitting can be performed in a similar manner.

An edge is defined by a change of intensity, which is a blurred stepfunction.

(CS6240) Model Fitting 20 / 34

Page 21: Model Fitting - NUS Computing - Homecs6240/lecture/fitting.pdfHere, we look at some simple examples as illustrations. (CS6240) Model Fitting 10 / 34 Fitting to Image Content Point

Fitting to Image Content Edge Fitting

Edge Model

u

θv

O’

O

y’

x’

AB

x

y

(O, x, y) is the global coordinate system of the image.

(O′, x′, y′) is the local coordinate system in which the edge isdefined.

(CS6240) Model Fitting 21 / 34

Page 22: Model Fitting - NUS Computing - Homecs6240/lecture/fitting.pdfHere, we look at some simple examples as illustrations. (CS6240) Model Fitting 10 / 34 Fitting to Image Content Point

Fitting to Image Content Edge Fitting

A unit step function is defined as

U(x) =

{

1 if x > 0

0 otherwise.

U x( )

x0

1

An ideal 2-D step edge S located at O′ in the coordinate system(O′, x′, y′) along the y′-axis is given by

S(x′, y′) = U(x′) . (21)

(CS6240) Model Fitting 22 / 34

Page 23: Model Fitting - NUS Computing - Homecs6240/lecture/fitting.pdfHere, we look at some simple examples as illustrations. (CS6240) Model Fitting 10 / 34 Fitting to Image Content Point

Fitting to Image Content Edge Fitting

A 2-D blurred edge F can be modeled by convolving the 2-D step edgeS with a 1-D Gaussian G across the edge:

F (x′, y′;σ) =

G(w;σ)S(x′ − w, y′) dw (22)

where

G(w;σ) = exp

(

−w2

2σ2

)

(23)

(a) sharp edge (b) 1-D Gaussian across edge (c) blurred edge

(CS6240) Model Fitting 23 / 34

Page 24: Model Fitting - NUS Computing - Homecs6240/lecture/fitting.pdfHere, we look at some simple examples as illustrations. (CS6240) Model Fitting 10 / 34 Fitting to Image Content Point

Fitting to Image Content Edge Fitting

O′ is located at (u, v) of the global coordinate system.

So, transform edge from local system to global system:

[

x′

y′

]

=

[

(x− u) cos θ + (y − v) sin θ

−(x− u) sin θ + (y − v) cos θ

]

(24)

Let the gray level on the darker side be A and the gray level on thebrighter side be B. Then, the final edge model M is:

M(x, y;θ) = A+BF (x′, y′;σ) (25)

where θ = (u, v, θ, σ,A,B)T is the parameter vector.

(CS6240) Model Fitting 24 / 34

Page 25: Model Fitting - NUS Computing - Homecs6240/lecture/fitting.pdfHere, we look at some simple examples as illustrations. (CS6240) Model Fitting 10 / 34 Fitting to Image Content Point

Fitting to Image Content Edge Fitting

Can compute the error of match E(θ) as

E(θ) =∑

x∈W

(M(x;θ)− I(x))2 (26)

where W is the extent of M .

Now, apply algorithm to find the θ that minimizes E(θ).In this case, it is not so easy to differentiate E wrt θ.

One method is to apply Powell’s direction set algorithm [PTVF92].

Powell’s algorithm does not require the user to provide the partialderivatives of E.

It estimates the partial derivative numerically.

(CS6240) Model Fitting 25 / 34

Page 26: Model Fitting - NUS Computing - Homecs6240/lecture/fitting.pdfHere, we look at some simple examples as illustrations. (CS6240) Model Fitting 10 / 34 Fitting to Image Content Point

Fitting to Image Content Fitting Other Models

Fitting Other Models

Other parametric models such as corners can be fitted to theimage in a similar way. (Exercise, [DB93, DG90])

Fitting general models is more complex.

Two kinds of models:

Parametric: represented by parametric but non-analytic equations.Non-parametric: represented by a set of discrete data points.

(CS6240) Model Fitting 26 / 34

Page 27: Model Fitting - NUS Computing - Homecs6240/lecture/fitting.pdfHere, we look at some simple examples as illustrations. (CS6240) Model Fitting 10 / 34 Fitting to Image Content Point

Revisit Line and Plane Fitting

Revisit Line and Plane Fitting

The previous method minimizes error of one of the coordinates (z).

What if you want to minimize the perpendicular distances of the pointsto the line (or plane)?

i1

e i

x1

x2

x i2

x

Need to compute the distance of a point to a line (or plane).

(CS6240) Model Fitting 27 / 34

Page 28: Model Fitting - NUS Computing - Homecs6240/lecture/fitting.pdfHere, we look at some simple examples as illustrations. (CS6240) Model Fitting 10 / 34 Fitting to Image Content Point

Revisit Line and Plane Fitting

Equation of hyperplane π (line in 2-D, plane in 3-D, etc.):

a1x1 + a2x2 + · · ·+ anxn + an+1 = 0. (27)

Denote a = (a1 · · · an)⊤, x = (x1 · · ·xn)

⊤.

Then, equation of π can be written as

a⊤x+ an+1 = 0 (28)

where x is a point on π.

Then, the unit vector u of π is a/‖a‖. (Exercise)

(CS6240) Model Fitting 28 / 34

Page 29: Model Fitting - NUS Computing - Homecs6240/lecture/fitting.pdfHere, we look at some simple examples as illustrations. (CS6240) Model Fitting 10 / 34 Fitting to Image Content Point

Revisit Line and Plane Fitting

Consider a point p not on π.

π

up

x

O

d

Let x denote the perpendicular projection of p on π.

Perpendicular distance of p to π, denoted as d, is give by

d = p⊤u− x⊤u

=p⊤a− x⊤a

‖a‖=

a⊤p+ an+1

‖a‖.

(29)

(CS6240) Model Fitting 29 / 34

Page 30: Model Fitting - NUS Computing - Homecs6240/lecture/fitting.pdfHere, we look at some simple examples as illustrations. (CS6240) Model Fitting 10 / 34 Fitting to Image Content Point

Revisit Line and Plane Fitting

So, finding the best-fit hyperplane π is to find the a that minimizes thedistance D

D =∑

i

[

a⊤p+ an+1

‖a‖

]2

. (30)

This equation is difficult to differentiate.

Can apply Powell’s direction set algorithm [PTVF92].

Notes:

So, fitting n-D linear or non-linear models to data points inn+ 1-D space is not too difficult.

But, fitting other models are not so easy.

Example: How to fit a curve (1-D object) to data points in 3-Dspace?

No convenient analytic form. How to even represent them?

(CS6240) Model Fitting 30 / 34

Page 31: Model Fitting - NUS Computing - Homecs6240/lecture/fitting.pdfHere, we look at some simple examples as illustrations. (CS6240) Model Fitting 10 / 34 Fitting to Image Content Point

Summary

Summary

Problems and algorithms discussed:

problem corresp. model algorithm

line, plane known linear linear least square

curve, curved surface known polynomial,RBF

linear least square

line, plane unknown linear Powell

point in image unknown Gaussian gradient descent

edge in image unknown Step Powell

Fitting a model to input data is equivalent to registering themodel to the input data.

Model fitting is also equivalent to model learning.After fitting, you get a model that fits the data.

Usually need good initialization to get good optimization results.

(CS6240) Model Fitting 31 / 34

Page 32: Model Fitting - NUS Computing - Homecs6240/lecture/fitting.pdfHere, we look at some simple examples as illustrations. (CS6240) Model Fitting 10 / 34 Fitting to Image Content Point

Exercise

Exercise

(1) Develop a method for fitting a polynomial curve in 2-D space.

(2) Develop a method for fitting a corner model to a corner in animage.

(3) The equation of a hyperplane π is given by

a1x1 + a2x2 + · · ·+ anxn + an+1 = 0. (31)

Denote a = (a1 · · · an)⊤, x = (x1 · · ·xn)

⊤. Show that the unitvector u of π is a/‖a‖.

(CS6240) Model Fitting 32 / 34

Page 33: Model Fitting - NUS Computing - Homecs6240/lecture/fitting.pdfHere, we look at some simple examples as illustrations. (CS6240) Model Fitting 10 / 34 Fitting to Image Content Point

Reference

Reference I

R. Deriche and T. Blaszka.Recovering and characterizing image features using an efficientmodel based approach.In Proceedings of IEEE Conference on Computer Vision andPattern Recognition, pages 530–535, 1993.http://citeseer.nj.nec.com/deriche94recovering.html.

R. Deriche and G. Giraudon.Accurate corner detection: An analytical study.In Proceedings of 3rd International Conference on ComputerVision, pages 66–70, 1990.

(CS6240) Model Fitting 33 / 34

Page 34: Model Fitting - NUS Computing - Homecs6240/lecture/fitting.pdfHere, we look at some simple examples as illustrations. (CS6240) Model Fitting 10 / 34 Fitting to Image Content Point

Reference

Reference II

W. H. Press, S. A. Teukolsky, W. T. Vetterling, and B. P.Flannery.Numerical Recipes in C.Cambridge University Press, 2nd edition, 1992.

(CS6240) Model Fitting 34 / 34