File Exchange

## Simple Particle Filter Demo

version 1.4.0.1 (1.94 MB) by

Tracking red object in a movie using particle filter.

Updated

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

Japanese:

このデモは映像中の赤い物体の場所を追跡します。

tien nguyen

### tien nguyen (view profile)

Could you explain for me some questions following.
- How to determine likelihood function
-Xstd_rgb = 50;Xstd_pos = 25;Xstd_vec = 5;
Thanks a lot

Miko May Lee Chang

Hakan

manh 123

Jigar Mori

### Jigar Mori (view profile)

I am working on "Parameterizable FPGA framework for particle filter based object tracking in video". Are your algorithm and algorithm mentioned in this paper same? I would like to know whether bhattacharya co-efficient is used or not?

shahrazed Ghouil

### shahrazed Ghouil (view profile)

I need histogram si I(x,y)<=vallé1 alors R(x,y)=1
sinon si I(x,y)>vallé1 et I(x,y)<vallé2 alors R(x,y)=2

Eiji Ota

### Eiji Ota (view profile)

Hello Mikheil,

I used this model just because this is the only one I could think of at that moment. I didn't know the model you show below...

Your model might be good one, but I don't have any information I could share. Sorry!

Mikheil Azatov

### Mikheil Azatov (view profile)

Hello Eiji. Really good job writing the code. I think I finally understand particle filters after watching many youtube videos and reading many papers and tutorials. I think your code was the most helpful part !!! Thank you!

I have a question about update_particle.m. Specifically you have new velocity defined as

X(3:4,:) = X(3:4,:) + Xstd_vec * randn(2, N);

I understand that using this algorithm, the velocity of the spots that are red a favored and settle on the correct number. I have also seen in other papers people defining velocity as

X(3:,4) = gamma*( X(1:2,:)(t) - X(1:2),:)(t-1)) + Xstd_vec * randn(2, N) * (1-gamma) ;

Basically this way you define velocity as actual velocity of the spot( difference in coordinates at two time points) + the random factor. Gamma is just a parameter that specifies how much of velocity should be random and how much specified by the measured velocity.

I was wondering why you chose your option instead of this one and if you think one option is better than the other one in some concepts. Any thoughts and comments would be greatly appreciated!!!

I'm trying to write a code that tracks multiple people that can cross each other and they do not have a specific color attached to them.

Mikheil Azatov

Eiji Ota

### Eiji Ota (view profile)

For your purpose, modifying this code might not be a good idea... Computer Vision System Toolbox provides a good function called vision.ForegroundDetector.

http://www.mathworks.com/help/releases/R2015a/vision/ref/vision.foregrounddetector-class.html

This function detect anything that moves.

hello Eiji, thanks for this wonderful code.
How this code can be modified to track any moving object/objects where no other properties like color, shape etc. are considered?

abdullah

john

### john (view profile)

OK,it is simple but rearlly interesting!Thank you!

minoru murase

Eiji Ota

### Eiji Ota (view profile)

Hi farah,

For this type of problems, tech support has a lot of information. So, asking mathworks' tech support would be best.

If you want to check it yourself, I think checking if you can play WMV-file on you PC would be a first step I guess.

Eiji Ota

### Eiji Ota (view profile)

Hi Osama,

If you try to track two color, I think you can think of the distribution which has two peaks in RGB color space.

It's going to be the distribution two gaussian distribution combined. It is called mixture gaussian distribution.

If you have distribution, you can calculate likelihood.

Eiji Ota

### Eiji Ota (view profile)

Hi Prachi,

As for your question, please let me recommend you a function called "rgb2hsv" you might know.

But I'm thinking that if you convert color RGB color space to HSV color, the distribution of noise might change too.

So, you have to change the way to calculate log-likelihood too.

I'm supposing gaussian noise in RGB-color space. But I'm not sure this supposition is fair or not. I do not have any substantial reason for this supposition.
Please remember that this is a toy demo.

If you have camera you use, I think you can check the pattern or distribution of noise you get when you observe some color using your camera.

For example, if you prepare big red sheet and monitor it under various environments, you can guess how color changes according to lighting condition or some other factor.

Then you can guess the distribution of noise you get when you track something by color, I think.

farah

### farah (view profile)

read giving error .Prompts to stop matlab .Maybe problem with the video.I have matlb 2013a

Osama

### Osama (view profile)

Hi Mr Eiji

first of all thanks for the the code and it works really well.
Could you help me, i am trying to track two color channels (blue and red)at the same time.
I changed the
Xrgb_trgt = [ 255; 0; 0 ]; to Xrgb_trgt = [ 0; 0; 255];
and it tracks the blue channel too but could you assist with how i can track both red and blue channels ? Thanks and also when i run it the particles seemed to be only appearing in one corner please help thankyou

Prachi

### Prachi (view profile)

Thank you for the code. I want to use HSV color space for particle filtering. can you please suggest the way to calculate log-likelihood usig HSV color components?

Eiji Ota

### Eiji Ota (view profile)

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.

john

### john (view profile)

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?

john

lee

Azim Heidaryan

### Azim Heidaryan (view profile)

what is a reference implementation (eg, paper).

Eiji Ota

### Eiji Ota (view profile)

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...

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

deniz kumlu

### deniz kumlu (view profile)

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.

Eiji Ota

### Eiji Ota (view profile)

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

Maqsooda

### Maqsooda (view profile)

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.

Atef

### Atef (view profile)

Thanks Mr.Eiji Ota you are kindly many thanks.

Eiji Ota

### Eiji Ota (view profile)

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.

Atef

### Atef (view profile)

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

Eiji Ota

### Eiji Ota (view profile)

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".

mejri

### mejri (view profile)

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

Atef

### Atef (view profile)

Thanks Mr.Eiji Ota

Eiji Ota

### Eiji Ota (view profile)

Computer Vision System Toolbox now supports Kalman filter.

http://www.mathworks.com/help/vision/ref/vision.kalmanfilterclass.html

http://www.mathworks.com/help/vision/examples/using-kalman-filter-for-object-tracking.html

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...

Atef

### Atef (view profile)

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

Eiji Ota

### Eiji Ota (view profile)

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".

Ashwani

### Ashwani (view profile)

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

Eiji Ota

### Eiji Ota (view profile)

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.

EIJI

Eiji Ota

### Eiji Ota (view profile)

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.

Kim

### Kim (view profile)

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

Eiji Ota

### Eiji Ota (view profile)

"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;

else

L(k) = -Inf;

end

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

Enjoy tweaking, Atef!

Atef

### Atef (view profile)

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

Eiji Ota

### Eiji Ota (view profile)

Thank you, pooja. Enjoy tweaking!

EIJI

pooja

### pooja (view profile)

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

Atef

### Atef (view profile)

Thanks Mr.Eiji Ota

Eiji Ota

### Eiji Ota (view profile)

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.

Atef

### Atef (view profile)

can you send me the paper for this work

Atef

### Atef (view profile)

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

Eiji Ota

### Eiji Ota (view profile)

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

http://www.mathworks.co.jp/help/toolbox/vision/gs/btd0kx6-1.html

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.

ProgramMatlab

### ProgramMatlab (view profile)

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.

Eiji Ota

### Eiji Ota (view profile)

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.

http://www.mathworks.co.jp/company/events/webinars/wbnr55833.html?id=55833&p1=961664441&p2=961664459

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.

harry yao

### harry yao (view profile)

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.

Eiji Ota

### Eiji Ota (view profile)

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.

ProgramMatlab

### ProgramMatlab (view profile)

Hello!
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.

Eiji Ota

### Eiji Ota (view profile)

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.

ProgramMatlab

### ProgramMatlab (view profile)

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

Eiji Ota

### Eiji Ota (view profile)

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.

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.

http://www.mathworks.co.jp/company/events/webinars/wbnr55833.html?id=55833&p1=961664441&p2=961664459

3. Face detection

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

http://www.mathworks.com/help/toolbox/vision/gs/btd0kx6-1.html#btd13m8

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

ProgramMatlab

### ProgramMatlab (view profile)

Hi!
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.

Eiji Ota

### Eiji Ota (view profile)

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.

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!

Eiji Ota

### Eiji Ota (view profile)

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.

ProgramMatlab

### ProgramMatlab (view profile)

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.

Eiji Ota

### Eiji Ota (view profile)

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.

ProgramMatlab

### ProgramMatlab (view profile)

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

ProgramMatlab

### ProgramMatlab (view profile)

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

Eiji Ota

### Eiji Ota (view profile)

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

Eiji Ota

### Eiji Ota (view profile)

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.

http://www.mathworks.co.jp/company/events/webinars/wbnr61774.html?id=61774&p1=962062707&p2=962062716

Edison Lee

### Edison Lee (view profile)

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

mejri

### mejri (view profile)

 1 Sep 2016 1.4.0.1 Updated license 9 Jul 2012 1.4 I added English pdf-slides. 3 Jul 2012 1.3 I added some PDF-slides which explain object tracking using particle filter. 18 Mar 2012 1.2 I added the URL of web seminar about particle filter.
##### MATLAB Release
MATLAB 7.13 (R2011b)