You are on page 1of 15

1 ESTIMATORS

Mestrado em Engenharia Electrotcnica e de Computadores e Mestrado em Tecnologias de Informao Visual ca

Viso por Computador a Assignment n 6

Visual Tracking using Particle Filters 1 Estimators

Many problems in science and engineering require the estimation of a set of variables related with some system from a sequence of noisy measurements made on some variables that are related to this system. When such estimation is to be done by a computer a state space model for the dynamic system written using a discrete-time formulation is the most adequate. This means that the evolution of the system is modelled using dierence equations, and the measurements are assumed to be available at discrete times. All the attention is focused on the state vector which should contain all the relevant information to describe the system. In a (visual) tracking problem, this information is normally related to the kinematic characteristics of the target, and the measurement vector represents noisy observations that are somehow related to the state vector. Although this is not a requirement, the measurement vector is frequently of lower dimension than the state vector. Two models are required to analyse and infer about a dynamic system. The rst one, known as the system model describes the evolution of the state with time and the second one relates the noisy measures to the state, being known as the measurement model. Dierent approaches can be used to derive estimators for these kinds of problems and frequently the results are the same. Here we are interested in the so called probabilistic approaches. These kind of approaches produce estimates under the form of probability distributions, instead of deterministic values. The probabilistic state space formulation and the requirement for updating of the state information upon the reception of each new measurement
Viso por Computador a

Paulo Menezes, 2011, DEEC

1.1 Sequencial importance sampling algorithm

1 ESTIMATORS

are well suited to a Bayesian approach. In such approach one attempts to construct the posterior probability density function (pdf) of the state given all the available information, which includes the set of received measurements. For the cases where an estimate must be obtained whenever a new measurement is received, a recursive lter is an adequate solution. This lter is normally divided in two stages: (1) prediction and (2) update (or correction). In the rst stage, the system model is used to predict the state pdf at the next measurement time, from the previous one. Due to the presence of noise (which models the unknown disturbances that aects the system), the predicted pdf is generally translated, deformed and spread. The reception of a new measurement permits the adjustment of this pdf during the update operation. This is done using the Bayes theorem as the mechanism to update the knowledge about the system state upon the reception of new information. There are dierent ltering algorithms that can be derived using a Bayesian formulation, being the Kalman Filter (or its Extended version) one of the most used. Its requirements are that the models be linear (or dierentiable) and the involved noise distributions be Gaussian. Particle lters are in turn, particularly well-suited for vision-based tracking applications, in particular due to the non-Gaussian characteristics of the measurement functions involved. These are also known as Monte Carlo-based methods, as they involve the use of sets of random samples that approximate the probability distributions we want to estimate.

1.1

Sequencial importance sampling algorithm

Suppose that there is a probability distribution p(x), from which it is very dicult to draw samples, but we know that it is proportional to another distribution (x) which can easily be evaluated. Let x(i) q(x), i = 1, ..., Ns be samples that are easily generated from a proposal distribution q(.), called importance density. Then, a weighted approximation to p(.) is
Ns

p(x)
i=1
(i)

w(i) (x x(i) )

(1)

) where w(i) (x(i) ) is the normalised weight of the i-th particle. q(x Thus, if the samples were drawn from an importance density q(x0:k |z1:k )

Viso por Computador a

Paulo Menezes, 2011, DEEC

1.1 Sequencial importance sampling algorithm

1 ESTIMATORS

then weights are wk


(i)

p(x0:k |z1:k ) q(x0:k |z1:k )


(i)

(i)

(2)

If we factorise the importance density as q(x0:k |z1:k ) = q(xk |x0:k1 , z1:k )q(x0:k1 |z0:k1 )
(i)

(3)

then we can obtain samples x0:k q(x0:k |z1:k ) by augmenting the existing (i) (i) samples x0:k q(x0:k1 |z1:k1 ) with the new state xk q(xk |x0:k1 , z1:k ). We start by expressing p(x0:k |z1:k ) in terms of p(x0:k1 |z1:k1 ), p(zk |xk ) and p(xk |xk1 ) p(zk |x0:k , z1:k1 )p(x0:k |z1:k1 ) p(zk |z1:k1 ) p(zk |x0:k , z1:k1 )p(xk |x0:k1 , z1:k1 )p(x0:k1 |z1:k1 ) = p(zk |z1:k1 ) p(zk |xk )p(xk |xk1 ) = p(x0:k1 |z1:k1 ) p(zk |z1:k1 ) p(zk |xk )p(xk |xk1 )p(x0:k1 |z1:k1 )

p(x0:k |z1:k ) =

Substituting these results in the weight update equation 2 p(zk |xk )p(xk |xk1 )p(x0:k1 |z1:k1 ) q(xk |x0:k1 , z1:k )q(x0:k1 |z1:k1 ) p(zk |xk )p(xk |xk1 ) p(x0:k1 |z1:k1 ) q(xk |x0:k1 , z1:k ) q(x0:k1 |z1:k1 ) p(zk |xk )p(xk |xk1 )
(i) (i) q(xk |x0:k1 , z1:k ) (i) (i) (i) (i) (i) (i) (i) (i) (i) (i) (i) (i) (i) (i) (i) (i) (i)

(i) wk

= =

wk1

(i)

If q(xk |x0:k , z1:k ) = q(xk |xk1 , zk ) then (we can just store xk and discard x0:k1 and z0:k1 ).
(i) wk

(i) (i) (i) (i) p(zk |xk )p(xk |xk1 ) wk1 (i) (i) q(xk |xk1 , zk )

Viso por Computador a

Paulo Menezes, 2011, DEEC

1.1 Sequencial importance sampling algorithm

1 ESTIMATORS

and the posterior density can be approximated by


Ns

p(xk |z1:k )
i=1

wk (xk xk )

(i)

(i)

Based on this, the Sequential Importance Sampling algorithm (SIS) is based on the recursive propagation of the weights of the points. This recursive update is performed each time a measurement is received. Its description can be seen on algorithm 1. Algorithm 1 Sequencial Importance Sampling Algorithm 1: for i = 1 : Ns do (i) (i) 2: xk q(xk |xk1 , zk ) {Draw sample, using proposal distribution}
3: 4: 5: 6: 7:

wk wk1

(i)

(i) p(zk |xk )p(xk |xk1 ) q(xk |xk1 ,zk )


(i) (i)

(i)

(i)

(i)

end for for i = 1 : Ns do wk end for


(i) wk
N j=1 (i)

wk

(i)

{Normalise the importance weights}

There is an important particular case of this framework that arises when the prior distribution is chosen as the importance distribution. In this case (i) i i the importance weights satisfy wk wk1 p(zk |xk ). It should be noted that although this special case is widely used, the importance sampling method is far more general than this. The deciency of this algorithm is that that most of the particles after some iterations show negligible weights. This means that most of the computational load used in updating these weights is worthless as these particles make (almost) no contribution to approximate the desired distribution. A measure for the degeneracy of this algorithm is the eective sample size Nef f dened as Ns (4) Nef f = i 1 + V ar(wk ) where
i wk =

p(xk |z1:k ) q(xk |xk1 , zk )

(5)

Viso por Computador a

Paulo Menezes, 2011, DEEC

1.2 Re-sampling

1 ESTIMATORS

is referred as the true weight . This cannot be evaluated exactly, but an estimate can be obtained Nef f =
(i)

1
(i) 2 Ns i=1 (wk )

(6)

where wk is the normalised weight. Note that Nef f Ns and small Nef f indicates severe degeneracy. Two solutions exist to reduce this eect, which are: a good choice of importance density, and the use of re-sampling. The choice of a good importance density is not easily obtained for most of the cases. On the other side, the second method is, in general, much simpler to implement being, therefore, the most common choice.

1.2

Re-sampling

The re-sampling is a technique that can be used whenever a signicant degeneracy of the samples weights is observed (Nef f < Nthreshold ). The basic idea of the method is to eliminate the particles with small weights and concentrate on particles with large weights. This involves generating a new set xi Ns by re-sampling (with replacek i=1 ment) Ns times from an appropriate discrete representation of p(xk |z1:k ) given by
Ns

p(xk |z1:k )
i=1

wk (xk xk )

(i)

(i)

(7)

j in a way that P r(xi = xi ) = wk . The obtained samples are i.i.d. samk k ples from the discrete density and the weights are reset to 1/Ns . Several algorithms for implementing the re-sampling are available that guarantee the number of operations to be O(Ns ). The algorithm 2 shows one of the preferred by most authors.

1.3

Sampling Importance Re-sampling Filter

This is a Monte Carlo method that can be applied to recursive Bayesian ltering problems, and is known by several names like: Particle lter [?, ?], SIR Filter [?], Bootstrap lter [?] or CONDENSATION [?].
Viso por Computador a

Paulo Menezes, 2011, DEEC

1.3 Sampling Importance Re-sampling Filter Algorithm 2 Re-sampling algorithm 1: c1 0 {Construct the CDF} 2: for i = 2 : Ns do (i) 3: c(i) ci1 + wk 4: end for 5: i 1 {Start at the bottom} 1 6: u1 U[0, Ns ] {Draw a starting point} 7: for j=1:Ns do 1 8: u(j) u1 + Ns (j 1) {Move along the CDF} 9: while u(j) > c(i) do 10: ii+1 11: end while (j) (i) 12: xk xk {Assign particle} (j) 1 13: wk Ns {Reset weight} 14: end for

1 ESTIMATORS

This method can be derived from the SIS by setting the importance den(Ii) (i) sity q(xk |xk1 , z1:k ) = p(xk |xk1 ) and applying the re-sampling at every step. As usual it requires the knowledge of the system dynamics f (., .) and measurement equation h(., .). It must be possible to draw samples from the noise vk1 and from the prior. The likelihood function p(zk |xk ) must be evaluated point-wise.

resampling

1 11 1 0 00 0 1 11 1 0 00 0
measure function

1 1 0 0 1 1 0 0

1 0 1 0
propagation

1 1 0 0 1 0 1 1 0 0 1 0

1 1111 11 11 0 0000 00 00 11 11 00 00 11 00 1 1111 11 11 0 0000 00 00 11 11 00 00 11 00

weighting

Figure 1: Illustration of the CONDENSATION algorithm


Viso por Computador a

Paulo Menezes, 2011, DEEC

2 IMPLEMENTATION This implies that the sample xi p(xk |xk1 ) can be generated by drawk (i) (i) (i) i ing a noise sample vk1 pv (vk1 ) and making xk = fk (xk1 |vk1 ) where pv is the probability density of vk1 . The weights are then updated by
i wk wk1 p(zk |xk ) (i) (i) (i) (i)

(8)

but as the resampling is applied in every step, we have wk1 = 1/N, i, it results in (i) wk p(zk |xk ) (9) These weights are normalised before the re-sampling stage as can be seen in algorithm 3 describes the SIR. One iteration of this algorithm is also illustrated on gure 1. Algorithm 3 Sampling Importance Re-sampling lter 1: for i = 1 : Ns do (i) (i) 2: xk p(xk |xk1 ){Draw particle} (i) (i) 3: wk p(zk |xk ) 4: end for (i) Ns 5: t i=1 wk 6: for i = 1 : Ns do (i) 7: wk t1 wk 8: end for 9: Re-sample using algorithm 2 The advantages of this method are the simplicity of both computing the weights and sampling the importance density. It has the following disadvantages: the importance density is independent of the measure zk and thus the state space is blindly explored, what can make this algorithm quite inecient under certain situations. As the re-sampling is applied in every iteration, it leads to the known sample impoverishment.

2
2.1

Implementation
Monte Carlo approximation

Considering a square of side length l and an inscribed circle (as in gure 2), which naturally has its diameter equal to the side of the square. The area of
Viso por Computador a

Paulo Menezes, 2011, DEEC

2.2 Implement a particle lter to track a moving 2 IMPLEMENTATION point on plane X-Y

Figure 2: A circle inscribed in a square the circle is

l2 Ac = , 4 As = l2 .

and that of the square is The ratio between these two areas is Ac = . As 4 If we draw randomly points over the square region, as long as their distribution is uniform, the ratio of number the points that fall on the circle from the total drawn inside the square, tends to converge to the ratio between the areas, as the number of points becomes very large. Demonstrate this using Matlab and plot the evolution of the approximate value of as more points are generated.

2.2

Implement a particle lter to track a moving point on plane X-Y

Considering a point thats moving and whose coordinates can be measured by some noisy sensor. As we dont know much about the properties of the point motion, we are going to use a constant velocity assumption.
Viso por Computador a

Paulo Menezes, 2011, DEEC

3 IMPLEMENT A PARTICLE FILTER TO TRACK A SIMPLE OBJECT ON A VIDEO SEQUENCE As in a discrete implementation we normally use dierence equations to approximate the dierential models, we can write the model as: Vk = Vk1 Xk = Xk1 + Vk1 t Xk1 Xk2 t we can write the state evolution in a autoregressive form as Vk1 = Xk = Xk1 + (Xk1 Xk2 ) = 2Xk1 Xk2 As

As the vector Xk = (xk , yk ), we can create an extended vector that contains the present and past information xk y Xk = k . xk1 yk1 Then we can model the state evolution as Xk = FXk1 where 2 0 1 0 0 2 0 1 . F= 1 0 0 0 0 1 0 0 The following gure shows the matlab code for resampling a matrix of particles based on their respective weights

Implement a particle lter to track a simple object on a video sequence

Here we intend to track a U-shapped line drawn on a sheet of paper, like the one shown in gure 8.
Viso por Computador a

Paulo Menezes, 2011, DEEC

3 IMPLEMENT A PARTICLE FILTER TO TRACK A SIMPLE OBJECT ON A VIDEO SEQUENCE

% i n i t i a l i z e t h e p a r t i c l e s and w e i g h t s p=u n i f r n d ( 0 , 5 0 0 , 2 , 5 0 0 ) ; p a r t i c l e s =[p ; p ] ; w e i g h t s=o n e s ( 1 , 5 0 0 ) / 5 0 0 ; % main c y c l e while ( 1 ) p a r t i c l e s=r e s a m p l e ( p a r t i c l e s , w e i g h t s ) ; p a r t i c l e s=p r e d i c t ( p a r t i c l e s ) ; measure=g e t m e a s u r e ( ) ; w e i g h t s=doweight ( measure , p a r t i c l e s ) ; [ s t a t e , c o v a r ]=computeMean ( ) ; end

Figure 3: Particle lter main cycle Tracking this object on the image sequence means that we intend to continuously estimate its position, orientation and scale. Thus each particle will propose an hypothesis (see gure 9) for these values and need to be validated by some measuring function (weighted). For this we need to dene a measuring model. A perfect estimation of the above parameters will make a model of the tracked object to fall perfectly onto its image. We may consider a measure of matching the distances between the estimated model and the contours of the object in the image. In a particle lter each particle represents a proposal for the estimate of the parameter vector that represents the target state. Each of these proposals must then be evaluated to measure how close it is to the real state of the target we are trying to estimate. In many problems, the state of the target cannot be measured directly, but only a manifestation of that can be perceived. This is the case of the current problem, where each captured image of the video sequence is not the measure, but contains the eects of the of the target presence, together with other possible objects and background scenarios that we will consider as distracting factors or noise. The proposed solution is based on the assumption that the target is visible and its contours are the only one that match perfectly with a line model that represents it. Therefore if we are able to place the model over it, with the same orientation, scale, then the sum of distances between the target contour points will be zero. Furthermore this sum of distances will increase as the
Viso por Computador a

10

Paulo Menezes, 2011, DEEC

3 IMPLEMENT A PARTICLE FILTER TO TRACK A SIMPLE OBJECT ON A VIDEO SEQUENCE

function [ p a r t s , w e i g t ]= r e s a m p l e ( p a r t i c l e s , w e i g h t s ) p a r t s=zeros ( 4 , 5 0 0 ) ; [ rows , c o l s ]= s i z e ( w e i g h t s ) ; c d f=zeros ( rows , c o l s ) ; c d f (1)= w e i g h t s ( 1 ) ; for i =2: c o l s , c d f ( i )= c d f ( i 1)+ w e i g h t s ( i ) ; end i =1; u=u n i f r n d ( 0 , 1 / c o l s ) ; for j =1: c o l s , u=u+1/ c o l s ; while u > w e i g h t s ( i ) i=i +1; end p a r t s ( : , j )= p a r t i c l e s ( : , i ) ; w e i g h t s ( j )=1/ c o l s ; end w e i g t=w e i g h t s ; end

Figure 4: Resampling in matlab


function p a r t s=p r e d i c t ( p a r t i c l e s , F) p a r t s=F p a r t i c l e s ; p a r t s=p a r t s+random ( Normal , 0 , 2 , s i z e ( p a r t i c l e s ) ) ; end

Figure 5: Predicting in matlab model conguration goes away from the true one. The next gure show the contour of the input image shown above. Figure 10a) represents clearly the U-shape to be tracked but also 2 other u-like shapes, that may attract the tracker. This would generate false tracks that can be avoided by image preprocessing to remove these unwanted contours or to impose restrictions on the size (scale) of the viewed target. On gure 10b) we can see the hypothesis of the model state, being the closest to the target the one that visibly will have smaller distances to the target contour edges.
Viso por Computador a

11

Paulo Menezes, 2011, DEEC

3 IMPLEMENT A PARTICLE FILTER TO TRACK A SIMPLE OBJECT ON A VIDEO SEQUENCE

function w e i g h t s=doweight ( measure , p a r t i c l e s ) % e s t a b l i s h some measuring f u n c t i o n for i =1: length ( p a r t i c l e s ) dx=measure (1) p a r t i c l e s ( 1 , i ) ; dy=measure (2) p a r t i c l e s ( 2 , i ) ; % r e c a l l that for a p a r t i c l e close to the point the distance i s small % and t h e w e i g h t a p p r o a c h e s 1 w e i t ( i )=exp(dxdxdydy ) ; end w e i g h t s=w e i t ; end

Figure 6: Weighting the particles


function pos=g e t m e a s u r e ( i ) % e s t a b l i s h some measuring f u n c t i o n pos =[100 sin ( i /20 pi ) ; 200 cos ( i /40 pi ) ] ; pos=pos+random ( Normal , 0 , 1 0 , 2 , 1 ) ; end

Figure 7: Generating noisy measures

Figure 8: A possible frame of the video sequence containing the shape to track For computing the distance to the target contours, we would select 5 points over the line-model of the target, and compute the distances di between

Viso por Computador a

12

Paulo Menezes, 2011, DEEC

3 IMPLEMENT A PARTICLE FILTER TO TRACK A SIMPLE OBJECT ON A VIDEO SEQUENCE

Figure 9: Representations of states proposed by dierent particles

a)

b)

Figure 10: a)Extracted contours of image represented in gure 8; b) superimposing 3 hypothesis for the model conguration on the same image. these points and the nearest contour. Dening D(x) as D(x) =
i

di (x),

where di (x) is the distance between point i of the model and the nearest image contour, for a given proposed conguration x, we can dene the weigh of each particle j as (j) w(j ) = eD(x ) . The choice of the negative exponential function is because for a perfect match D(x(j) ) = 0 so the exponential is 1, and decreases for increasing values of the sum of distances. This this is indeed the perfect shape for the weighting
Viso por Computador a

13

Paulo Menezes, 2011, DEEC

3 IMPLEMENT A PARTICLE FILTER TO TRACK A SIMPLE 3.1 Using the distance transform OBJECT ON A VIDEO SEQUENCE

function, as it gives more weight to particles that represent good proposals and less weight for those that represent bad proposals.

3.1

Using the distance transform

To simplify the computation of measures between contours and given image locations, we can use the concept of distance transform. The distance transform of a contour image, consists in an image where each pixel value represents the distance between that that location and the nearest contour. Figure 11 shows the distance transform of the contour image of gure 10. In matlab use the bwdist function. Now to obtain the distance of the model

Figure 11: Distance transform of the contour image points is just the matter of taking the values of the distance transform for the locations of these points.

3.2

The measurement and state models

Lets consider the U-shape dened by the line segments [(-5,5),(-5,0)], [(5,0),(5,0)] and [(5,0),(5,5)]. Thus selecting some points over these line segments we can get the set of points dened in table 1. Now for each set of x y -5 5 -5 4 -5 3 -5 2 -5 1 -5 0 -3 0 -1 0 1 0 3 0 5 0 5 1 5 2 5 3 5 4 5 5

Table 1: Coordinates of measuring points dened in target coordinates parameters proposed by each the particles we just need to apply the correViso por Computador a

14

Paulo Menezes, 2011, DEEC

3 IMPLEMENT A PARTICLE FILTER TO TRACK A SIMPLE 3.2 The measurement and state models OBJECT ON A VIDEO SEQUENCE

sponding transformation to this set of points to obtain their corresponding coordinates. The parameters of the state vector to estimate will correspond to the x and y coordinates of the model on the image frame, its scale and orientation. For this we need to dene also the matrix F that models the evolution of these parameters. For the current work you can consider a constant velocity model, dened in an autoregressive manner as in section 2.

Viso por Computador a

15

Paulo Menezes, 2011, DEEC

You might also like