4.9 | 10 ratings Rate this file 202 Downloads (last 30 days) File Size: 1.94 MB File ID: #33666
image thumbnail

Simple Particle Filter Demo



07 Nov 2011 (Updated )

Tracking red object in a movie using particle filter.

| Watch this File

File Information


This demo try to identify the location of red object in a movie.



Required Products Image Acquisition Toolbox
MATLAB release MATLAB 7.13 (R2011b)
Other requirements If you want to get images from webcam, you need Image Acquitision Toolbox and webcam.
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (52)
21 Nov 2014 Eiji Ota

A and B are used for the calculation of log likelihood. This log likelihood is calculated under the assumption that RGB color of the object is observed with gaussian noise. But this assumption might not be true. Please think this is a toy to understand particle filter.

12 Nov 2014 john

thank you for sharing code.My question is why do you use the formula:
A = -log(sqrt(2 * pi) * Xstd_rgb);
B = - 0.5 / (Xstd_rgb.^2);
to make the likelihood,which reasons was it based on?

11 Nov 2014 john  
02 Nov 2014 lee  
05 Oct 2014 Azim Heidaryan

what is a reference implementation (eg, paper).

28 Jul 2014 Eiji Ota

Compared with particle filter, Kalman filter is relatively light algorithm.

If the object moves really really fast, fining object frame by frame might be an idea...

27 Jul 2014 Sampada

How to modify this code to detect and track objects which are very fast moving in a video?

06 May 2014 deniz kumlu

First of all, thanks for sharing the code, it helps me a lot. I will ask question about parameters :
Xstd_rgb = 50;
Xstd_pos = 25;
Xstd_vec = 5;
How did you find the numbers like 50,25 and 5?
I want to generalize this for every color not spesific to red one. So, how can I start to modify this program ?

Thanks again.

05 Nov 2013 Eiji Ota

Would let me know the version of MATLAB you use? I've heard 64bit version of old MATLAB cannot handle video properly.

04 Nov 2013 Maqsooda

Hi ,
How can I open a video in this program. GUI is running ok but no video being shown.

Eiji Ota Can you help please.

01 Apr 2013 Atef

Thanks Mr.Eiji Ota you are kindly many thanks.

01 Apr 2013 Eiji Ota

Sorry for poorly commented code...

"Xstd_rgb" means standard deviation of observation noise, which means noise you get when you observe the state of something.

"Xstd_pos" and "Xstd_vec" mean standard deviation of system noise, which describes how far actual movement of target object differs from the ideal model (in this case, linear uniform motion).

State space become of 2 componets, one is "position of particle" and another is "speed of particle".

You can define these 3 types of noise by these parameters.

31 Mar 2013 Atef

Plz Mr.Eiji Ota can you explain these parameters use for what ?
Xstd_rgb = 50;
Xstd_pos = 25;
Xstd_vec = 5;

11 Mar 2013 Eiji Ota

I made this program based on several books written in Japanese. There's no paper related to this demo. Sorry...

You can find several papers or tutorial documents if you search internet by these keywords, "particle filter", "color tracking", "condensation".

07 Mar 2013 mejri

Please, can I know from which paper does this project described, thanks !

06 Mar 2013 Atef

Thanks Mr.Eiji Ota

06 Mar 2013 Eiji Ota

Computer Vision System Toolbox now supports Kalman filter.



If you want to track object using Kalman filter, Computer Vision System Toolbox could be an answer.

But, if you want to know how Kalman filter works, I do not have any good example for Kalman filter. Sorry...

05 Mar 2013 Atef

Mr. Ota do you have code for tracking object color using kalman filter?

04 Mar 2013 Eiji Ota

Initial location of each particle is decided by random numbers. And initial speed is set to zero. Initial conditions are decided by function "create_particles".

03 Mar 2013 Ashwani

sir can u explain what is the initial state here you are taking.......and how the code is working

31 Jan 2013 Eiji Ota

Recently I heard this demo program doesn't work with 64bit version of MATLAB in some cases.

Sorry for inconvinience... Please use 32bit version when you run this demo.


04 Dec 2012 Eiji Ota

Hello Kim,

This type of problem might happen if you are using Mac OS (The movie file I attached was windows media video).

Or video decoding function of windows OS might have some problem.

Asking support center might be the fastest way to solve this type of problem because they have a lot of information about OS related problems.

04 Dec 2012 Kim

hello, thanks for your contribution.
I got a problem with ''particle_filter_by_saved_movie.m file'' the error shows like [Dot name reference on non-scalar structure. Error in VideoReader/read (line 91) if( ~isempty(obj.NumberOfFrames) )] I don't know why? can you help me ? thank you

02 Nov 2012 Eiji Ota

"calc_log_likelihood.m" is the function which calculates log likelihood according to normal distribution.

In this function, you can find an if statement like this...

if I && J

C = double(Y(:, m, n));

D = C - Xrgb_trgt;

D2 = D' * D;

L(k) = A + B * D2;


L(k) = -Inf;


This is the part where this program calcuate the loglikelihood. I set "-Inf" for the particles which go outside of picture.

Enjoy tweaking, Atef!

01 Nov 2012 Atef

Mr.Ota please can I change the code to apply another distribution function.
thanks Mr.Ota for this code.

29 Oct 2012 Eiji Ota

Thank you, pooja. Enjoy tweaking!


25 Oct 2012 pooja

Oh this is interesting. I wonder if I can tweak the code to apply information filter instead of particle filter.

Thanks for this Mr.Ota

15 Sep 2012 Atef

Thanks Mr.Eiji Ota

13 Sep 2012 Eiji Ota

I made this program based on several books written in Japanese. There's no paper I can recommend. Sorry...

You can check the basic concept of particle filter by wikipedia.

12 Sep 2012 Atef

can you send me the paper for this work

12 Sep 2012 Atef

I'd like to applay this code using beta filter " beta distribution maximum likelihood "

31 Aug 2012 Eiji Ota

I think rgb2hsv can be used for the conversion of color space. In this demo,


I think this function is used for the conversion of color space from RGB to HSV.

If you change the function "calc_log_likelihood.m", you can change how you calculate likelihood.

30 Aug 2012 ProgramMatlab

Please, I will use this code to track the face of person how do I change the resampling function given that I used the intesection of HSV histogram as a function likelhood. Please help me. it is critical to know this. thanks a lot.

28 Aug 2012 Eiji Ota

I've never tried to track other color, but you can try quite easily.

Open the main file "particle_filter_by_saved_movie.m", and edit following part.

Xrgb_trgt = [255; 0; 0];

This parameter means like "target color". So, if you want to track blue people,
you can change upper code like this.

Xrgb_trgt = [0; 0; 255];

But if you set "green" as "target color", you might face a small problem.


In this webinar, our engineer is trying to track green ball, and showing how he solved his problem.

In some cases, using other color space like HSV might be another solution.

27 Aug 2012 harry yao

Interesting example.

could you tracking the other color such as green color or yellow or blue.

on that case, which parts of the code should be change.

20 Aug 2012 Eiji Ota

Hi rafrouf,

Thank you very much for your feedback. Even though research of image processing is not my main job, but if you create some interesting application, please let me know.

I hope your project may go well.

18 Aug 2012 ProgramMatlab

I think that you used the exp() in the resampling function to convert the -Inf values of L() to zero. If you didn't convert these values, when you make Q, you will obtain a NaN values. Tahnks a lot for your cooperation.
If you are interested by the image processing and video processing we can work in cooperation. happy weekend.

16 Aug 2012 Eiji Ota

I think I need "exp" to convert log likelihood to real value.

You might not need "exp" when you input real value to resampling function.

Anyway, I'm glad your problem seems to be solved.

16 Aug 2012 ProgramMatlab

thanks a lot,
really my problem is in the resampling task, when w e change the likelhood distance. I used the intersection between histogramme to determine the likelhood for each particle but in the resampling task. I think that you must change the first line of your programm exp(L-max(L)) by L-max(L). Thanks

16 Aug 2012 Eiji Ota

Because I'm not an expert of image processing, I might not be able to give you the best advice of this area.
But I have a few general advice which I can think of. I hope these comments may become your help.

1. Start with the simplest case

Maybe you can simply start with changing the target color from red to skin color,
and check if my program works with your skin color or not.

And after that, you can evolve the algorithm to detect the orientation of face.

If you know the ceter location of face, it's going to be easier to detect the orientation of face.

Maybe you can create some face images rotated 0, 45, 90, 135,or 180 degree, then you might can find a good way (function) to detect the orientaion of the face.

2. Basic attitude of algorithm development

I think the following web seminar might be useful for learning the basic attitude of alogrithm development. This is the another example of color tracking.


3. Face detection

What you want to do is face detection, you can find a good example in computer vision system toolbox.


I don't know the detail of this alogrithm, but I think this might to be the easiest way to detect a face :-)

14 Aug 2012 ProgramMatlab

I want to fit a square a round a face of a person. I consider each particle as a square and I added the square orienatation caracteristic the system of mouvemnt. Thus, I have, (position,speed,orientation). My problem is when I compare the histogram of the reference model and each particle using the distance of Bhattacharya and I do the resampling task, the result is very bad. Thus, I think that it is due to the result comparaison. Therefore, I used your method by modelate the color by gaussian but the result is also very bad. Please, have you any idea? thanks a lot.

14 Aug 2012 Eiji Ota

Thank you very much for the compliments :-)

And your indication is right. "sigma" in my PDF document should be "sigma^2".

Thank you again for letting me know this mistake.

13 Aug 2012 Vladimir

Hi, this is a great example (one of the best I have seen). There is just one correction, In PDF, Calculation of “likelihood” in likelihood, "sigma" should be out of square root or it should be sigma^2. One more time, great work!

13 Aug 2012 Eiji Ota

Usually histogram's bins have equal width.

But when you use histc function, you can set the width of bins arbitrarily. For example, you can call this function like this.

Edge = [0 0.2 0.8 1]
Data = [ 0.1 0.3 0.5]

[~, I] = histc(Data, Edge)

And you get I = [1 2 2].

This means Data(1) belongs to 1st bin, Data(2) belongs to 2nd bin, and Data(3) belongs to 2nd bin(Please watch the help file ).

And I thought when I set

Edge -> (cumulative distribution)
Data -> (uniform random number from 0 to 1)

I thought I can do resampling of particles using histc.

I hope this explanation may help you.

12 Aug 2012 ProgramMatlab

I read some document and I found that there are several resampling method such as the systematic resampling. please which methods did you used? and normally, resampling stage aims to select the appropriate particles belongs to their weights but in your code I don't see this choice. thanks to expalin me more thgis fucntion.

09 Aug 2012 Eiji Ota

Hi rafrouf,

The first two lines of resampling function are like this.

L = exp(L_log - max(L_log));
Q = L / sum(L, 2);

And this is same as this.

L = exp(L_log);
Q = L / sum(L, 2);

I subtract max(L_log) to maintain the precision of this type of calculation, because what's important is ratio Q.

08 Aug 2012 ProgramMatlab

please, can you explain me more the part of resampling and specially the first line.

08 Aug 2012 ProgramMatlab

Hi can I know from which paper does this project comes from?

09 Jul 2012 Eiji Ota

I've translated the pdf slides which I used in my webinar. And I added them to my zip file.

08 May 2012 Eiji Ota

Hi Edison,

This demo comes from the mathworks' webinar recorded in Japan.

Unfortunitely there's no English document right now. Sorry for the inconvinience...

You can watch webinar in Japanese.


07 May 2012 Edison Lee

Hi can I know from which paper does this project comes from?

15 Nov 2011 mejri  
18 Mar 2012

I added the URL of web seminar about particle filter.

03 Jul 2012

I added some PDF-slides which explain object tracking using particle filter.

09 Jul 2012

I added English pdf-slides.

Contact us