View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Particle Filter Color Tracker

4.7 | 23 ratings Rate this file 51 Downloads (last 30 days) File Size: 12.5 MB File ID: #17960 Version: 1.7
image thumbnail

Particle Filter Color Tracker



11 Dec 2007 (Updated )

Tracking an object in a video with a Color Particle Filter

| Watch this File

File Information

A simple example demonstrating how to track an object with particle filter. Likelihood function is based on Bhattacharya distance of color histograms and gradient distributions

Please run mexme_pf_color_tracker to recompile mex-files on your own plateform.

Run the two demo files test_pf_colortracker and test_pf_colortracker2

IMPORTANT. Be sure than you "mex -setup" had been done before compiling sources

MATLAB release MATLAB 7.5 (R2007b)
Other requirements A C Compiler such MSVC/Intel CPP/GCC
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (83)
15 Apr 2016 EKC

EKC (view profile)

Running R2015b (64bit Windows) on my new PC. Had to replace mmreader(video_file) with VideoReader(video_file) but getting:

Error using VideoReader/init (line 619)
The file requires the following codec(s) to be installed on your system: cvid.

Any ideas how to fix?

FYI mmreader() & all code worked fine on my old PC (running 32bit R2011).

Any ideas?

27 Mar 2016 Jigar Mori

This code is not working as expected. Can you have complete MATLAB implementation?

I am working on "Parameterizable FPGA framework for particle filter based object tracking in video". It uses histogram and bhattacharya co efficient. Can anybody suggest MATLAB code so that I can replicate on VHDL.

26 Aug 2015 Huang Bo-Yang

12 Jun 2015 Ahmad

Ahmad (view profile)

Hello Sir,

well, once again thanks for such a marvelous code that has indeed helped a lot. Sir, I was just wondering to test a hybrid tracker that is based on PF and meanshift (MS) optimization technique which is a gradient based search mechanism that is responsible for the evaluation of the particles at hand.

Sir, I am going into a kind of error that I think results from the corruption of the HEAP and as a result my Matlab exits with ACCESS VIOLATION ERROR.

But when I restrict the MS to just one iteration (contrary to the average in MS which is 5). So the bottom line is that MatLab crashes when I increase the number of MS iterations.

could you please give me any clue that would save me much of the time. Thanks


Comment only
26 Apr 2015 fu yun

fu yun (view profile)

I'm very grateful for your excellent work.It's really helpful!
And I have a question : how to set the color I need to track? Eg:I want to track a red object,where should I set the parameters? How to change the files?

Comment only
27 Mar 2014 Ahmad

Ahmad (view profile)

26 Mar 2014 Ahmad

Ahmad (view profile)

The desperation worked out at last ... I only Uncommented the pdfgrad_ellipse function with a threshold_grad in the input list (which was by default commented) AND I Commented the pdfgrad_ellipse function without threshold_grad in its input list .... my statement might be confusing so have a look at the 2 functions that were played with ....

likelihood_shape = pdfgrad_ellipse(Z , yk , ek , h0 , lambda , sigma_shape , l , alpha0 , ratio , bary , nb_classe , threshold_grad);

% likelihood_shape = pdfgrad_ellipse(Z , yk , ek , h0 , lambda , sigma_shape , l , alpha0 , ratio , bary , nb_classe);

Comment only
26 Mar 2014 payal

payal (view profile)

Thanks a lot for sharing this code...
Thanks Ravi Prakash, without settings you mentioned I was unable to do it...
Thanks a lot...

Comment only
26 Mar 2014 payal

payal (view profile)

25 Mar 2014 Sebastien PARIS


Did you recompile mex files for your system ? (run mexme_ ....).
What is your exact OS ?, compiler ?

Comment only
25 Mar 2014 Ahmad

Ahmad (view profile)

Sir, I mean test_pf_colortracker is tracking fantastic BUT the test_pf_colortracker2 is making the trouble ... and I have used BREAKs in my code (I mean your code :)) .. and I have identified the error making function which is the pdfgrad_ellipse ....
Thank you very much sir for the quick response .

Comment only
25 Mar 2014 Sebastien PARIS


What do you mean by "the second part of the code" ? Does the tracking is well displayed ?

Comment only
25 Mar 2014 Ahmad

Ahmad (view profile)

Thank you Sebastein for sharing this code, I am having problems with the 2nd part of the code. Matlab forces me to restart it, am I doing something wrong.
Configuration of my systems are:
core-i5, 4 GB RAM, Windows 7 Home edition, SDK 7.1 installed, MatLab (R2013b) .... Sir, can you look into this issue please. I shall be thankful Sir, Regards

Comment only
17 Jan 2014 Sebastien PARIS


Please find informations in:

X. Rong Li et V. P. Jilkov : Survey of maneuvering target tracking. Part I. Dynamic models.
Aerospace and Electronic Systems, IEEE Transactions on, 39(4):1333–1364, 2003.

Comment only
12 Jan 2014 Jae Hyuck

Firstly, thank you for this excellent program.
While analyzing this program, I got a question.

When you construct motion model covariance matrix for pos and vel, R_y, you consider them as dependent(not using diagonal matrix) as a relation with delta_k. Could you give me some evidence why you select such model?
% R_y = sigma_y |delta_k^3/3 delta_k^2/2|
|delta_k^2/2 delta_k|

11 Jan 2014 Stendo

Stendo (view profile)

You know how it doing ?

Comment only
10 Jan 2014 Sebastien PARIS


You must define a model of likelihood given your geometric model of your object... and plug into this code

Comment only
10 Jan 2014 Stendo

Stendo (view profile)

Hi please help me.I would need to modify this project. I need to recognize objects using geometric properties or edge recognition. It must be particle filter. I am a beginner in MATLAB and I do not know how to do. Thanks for helping

Comment only
01 Jan 2014 daher

daher (view profile)

Hi all
I am having an inquiry. Suppose i have to track the first blue color object found in a video. After few frames, another object with the same color appears. How to keep tracking the first object no matte what will appear in the video without referring to the geometry of each blue object and compare it with the first identified. Hope you can give me a hint... Thaaaaaanks

Comment only
27 Sep 2013 Ricardo

Excellent, thank you. Still have to fully understand the theory though :)

Users of Linux hope this helps: video_file should be set to the full path, e.g. '/opt/matlab/.../SECOND.AVI', and have gstreamer0.10 plugins installed.

If you do not have mmreader, check out mmread from fileexchange. mmread does not instantiate a class, it returns a struct and therefore all set, get, read in the original test files should be replaced to access struct members.

video = mmread(video_file);
nb_frame = video.nrFramesTotal - offset_frame - 10;
dim_x = video.width;
dim_y = video.height;

ln127 : I = video.frames(offset_frame + 1).cdata;
ln158 : I = video.frames(offset_frame + k).cdata;

16 Aug 2013 Dasar Sisten Kontrol

i get problem with file download. can you help me for send me the file at my email please?

15 Aug 2013 Dasar Sisten Kontrol

12 Apr 2013 balaji

balaji (view profile)

thanku sir..

Comment only
18 Mar 2013 Sebastien PARIS

K Nummiaro, E Koller-Meier, L Van Gool, An adaptive color-based particle filter, Image and Vision Computing, 2003 - Elsevier

for example

Comment only
14 Mar 2013 balaji

balaji (view profile)

sir i need some details about this pf colour tracker..any reference paper about this example?

Comment only
30 Jan 2013 Karthik MSwamy

Excellent code. Works perfect on my Win 8, Matlab 2011b x64. Can you please cite the paper you used for writing this tracker?

28 Jan 2013 GITAM

GITAM (view profile)

waiting for u r valuable reply

Comment only
08 Jun 2012 ECE

ECE (view profile)


16 Mar 2012 sahere rahimi

i do not have any c compiler, please help me, please give me a file that does'nt need any c compiler

Comment only
15 Mar 2012 Sebastien PARIS

Ching-Chun: 2009b win64 ?
Can you put a break point to stop at errors ?

Comment only
10 Mar 2012 Ching-Chun

I change the matlab version to 2009b, however the error still occurs. I hope I can help to repair the bug, but there is no more information.

Comment only
05 Mar 2012 Sebastien PARIS

The problem ... that I don't know from which mex-files, the crash is coming....
Do you have more verbose ?

Comment only
05 Mar 2012 Ching-Chun

sorry, I forgot to mention these. My Os is windows 7 64-bit, processor is Intel Core 2 Duo 2.80 GHz, Matlab version is (R2008a), and the used compiler is Microsoft Visual Studio 2010.

Comment only
05 Mar 2012 Sebastien PARIS

What is excetly your system ? OS, processor, version of Matlab, compiler used ?

Comment only
05 Mar 2012 Ching-Chun

Sebastien Paris, thank you. The program works well now. But when I run "test_pf_colortracker2.m", there are some errors and then Matlab asks to restart. Is it because I do somethin wrong? The followed are excerpts from the error messages:

Frames = 106/770
Frames = 107/770
Saving minidump to file C:\Users\irene\AppData\Local\Temp\matlab_crash_dump.4692.0.dmp

Segmentation violation detected at Mon Mar 05 15:20:34 2012

MATLAB Version: (R2008a)
MATLAB License: 161051
Operating System: Microsoft Windows Vista
Window System: Version 6.1 (Build 7601: Service Pack 1)
Processor ID: x86 Family 6 Model 7 Stepping 10, GenuineIntel
Virtual Machine: Java 1.6.0 with Sun Microsystems Inc. Java HotSpot(TM) 64-Bit Server VM mixed mode
Default Encoding: Big5

Fault Count: 1

Register State:
rax = 00000000ffffffff rbx = 0000000000000028
rcx = 000000000000ef53 rdx = 0000000000000011
rbp = 0000000000000029 rsi = 0000000000001d3b
rdi = 00000000354f5cd0 rsp = 000000000102bc00
r8 = 0000000000000000 r9 = 0000000000000028
r10 = 000000003547e228 r11 = 0000000000000028
r12 = 000000000000000b r13 = 000000003da57e00
r14 = 0000000000000000 r15 = 00000000000000fa
rip = 000007fef8291c93 flg = 0000000000010203

Stack Trace:

This error was detected while a MEX-file was running. If the MEX-file
is not an official MathWorks function, please examine its source code
for errors. Please consult the External Interfaces Guide for information
on debugging MEX-files.

... ...

Comment only
04 Mar 2012 Sebastien PARIS

Ching-Chun. As indicaded in requierements, you must have C compiler. Please install for example Visual C++ express accoding to your version of Matlab.

Comment only
02 Mar 2012 Ching-Chun

Please help me.
What should I do to make it works?

When I compile the file "mexme_pf_color_tracker.m"
I got the messages

echo on


Select a compiler:

[0] None


If I enter "0", I got more messages as follows.

mex: No compiler selected. No action taken.

Warning: The MATLAB C and Fortran API has changed to support MATLAB
variables with more than 2^32-1 elements. In the near future
you will be required to update your code to utilize the new
API. You can find more information about this at:
Building with the -largeArrayDims option enables the new API.

C:\PROGRA~1\MATLAB\R2008A\BIN\MEX.PL: Error: No compiler options file could be found to compile source code. Please run "mex -setup" to rectify.

??? Error using ==> mex at 207
Unable to complete successfully.

Error in ==> mexme_pf_color_tracker at 3

Comment only
15 Dec 2011 zhao

zhao (view profile)

Hi Sebastien,i modified the files,and Create more problems. If i want to run the files in matlab7.0, can you sent the right files to me. My,thanks a lots.

Comment only
15 Dec 2011 Sebastien PARIS


Instead of badly rating, please have the nominal configuration requiered.

Comment only
14 Dec 2011 zhao

zhao (view profile)

14 Dec 2011 zhao

zhao (view profile)

My matlab7.0 can not find function 'mmreader'. If aviread is used,can tell me where should be replace in the files.

21 Sep 2011 Sathiyaraj Manoharan

Can anyone explain me how to install the C compiler and compile the C files in MATLAB..

Comment only
20 Sep 2011 Sathiyaraj Manoharan

Hi Sebastien:
I am new to this can you explain how to execute.I m getting error like C file is not found.I think that because of the mex compiler. how to install that with brief.Is it necessary to install VC++ 2008 express.Can I run it in Matlab2011.

Comment only
16 Sep 2011 Chi Chin

Hi Sebastien,

Thanks for your prompt reply. I had installed the K-lite mega codec in my system but it still give me the same error :<

Comment only
16 Sep 2011 Sebastien PARIS


Maybe install a video codec pack such K-lite mega codec in your system. Regards,

Comment only
16 Sep 2011 Chi Chin

I get the following error on Windows 7(64-bit) and MATLAB R2009b. Can anyone assist?

??? Error using ==> aviread at 76
Unable to locate decompressor to decompress video stream

Error in ==> test_pf_colortracker at 196
mov = aviread(video , offset_frame + 1);

anyone can help me?

Comment only
18 Jul 2011 Selva

Selva (view profile)

Thanks for sharing dude..

Comment only
21 May 2011 Sebastien PARIS

oj: If I have time, I'll update with mmreader instead of the old (and depreciated) aviread

Comment only
21 May 2011 Sebastien PARIS

Jake: here IS used here is independant of the current measure so ... q(x_k|x_{k-1},z_k) = p(x_k|x_{k-1}) here ...

Comment only
20 May 2011 oj

oj (view profile)

Dear sebastien I am unable to get the program in running condition... Even though I have replaced mmreader for aviread... still cudnt fix problem... help anyone..

Comment only
03 May 2011 Jake

Jake (view profile)

Thank you for sharing, I just wondering did you pick a importance density in this program?

Comment only
20 Mar 2011 Sebastien PARIS

to kar: as we say in french "pour avoir le beurre et l'argent du beurre".... in other words, the easiness of matlab and the speed of C.

Comment only
18 Mar 2011 tejaswini kar

sir can i get the reference material of the codes for my work.actually i am unable to understand it possible that either the whole program can be written in matlab or written in c.why u have used both c an matlab. what is the advantage.

25 Jan 2011 Kevin Murphy

04 Jul 2010 tarun goel

Hi sir, it is indeed a nice work and thanx for sharing it. Could you please tell some refeence material where the meaning of variables can be understood as their names are of little help. Iwud be highly thankful.

13 Jun 2010 Duy Nguyen

11 Jun 2010 Duy Nguyen

Thank for your sharing, Sebastien. I want to get more detail about this issue, can you give me reference for this ones.

Comment only
03 Sep 2009 Hong 4

Hong 4 (view profile)

I found a paper relates to this subject:
K Nummiaro, E Koller-Meier, L Van Gool, An adaptive color-based particle filter, Image and Vision Computing, 2003 - Elsevier

Comment only
03 Sep 2009 Hong 4

Hong 4 (view profile)

I found a paper relates to this subject:
K Nummiaro, E Koller-Meier, L Van Gool, An adaptive color-based particle filter, Image and Vision Computing, 2003 - Elsevier

Comment only
30 Aug 2009 Yong Zhao

thanks for you sharing, I'll study and discuss with all of you.

Comment only
19 Aug 2009 Janayna

did you use any paper to implement the particle filter?
Thank you

18 Jul 2009 Fang-I Chu

can i ask if i want to track a moving object under grayscale, what is it different from color tracking?

Comment only
17 Jun 2009 yasong guo

i have solve it now!thank you for your codes .wonderful!thank you!!!

Comment only
09 Mar 2009 Sebastien PARIS

Ok, you can replace the aviread function with the new one 'mmreader' to read avi files.

Comment only
08 Mar 2009 M

M (view profile)

I get the following error on Windows Vista (64-bit) and MATLAB R2008b. Can anyone assist?

??? Error using ==> aviread at 76
Unable to locate decompressor to decompress video stream

Error in ==> test_pf_colortracker at 196
mov = aviread(video , offset_frame + 1);

PS: I tried installing CINEPAK, but it didnt work! :(

Comment only
28 Nov 2008 Tedo Biresaw

Wow! what really a nice work.
I get it work.

26 Nov 2008 Sebastien PARIS

Ok, I'll update with a mexmefirt.m file for mexfilers beginners

Thx all for comments/ratings

Comment only
25 Nov 2008 Ravi Prakash

For all those laymen like me who couldn't get it to work in the first attempt. Type inside the MATLAB command window

> mex ellipse.c
> mex rgb2hsv_mex.c
> mex pdfcolor_ellipserand.c

In both files part_moment.c and particle_resampling.c at the top add the line
#define max(a,b) ( (a) >= (b) ? (a) : (b) )

> mex part_moment.c
> mex particle_resampling.c

Finally to run
> test_pf_colortracker

Thanks Sebastien. FIGHT ON!!

Comment only
29 Oct 2008 David Norville

Got it to work. Thanks

Comment only
27 Oct 2008 David Norville

I'm having some trouble recompiling with the part_moment.c file. It says there is an undefinded reference to '_max'. Can you help me please.

Comment only
21 Oct 2008 XU FENGLIANG

thanks for you sharing

09 Sep 2008 min hu

thanks for is very good.

28 Jun 2008 nauha jam

Thanks for program.I would ask about the LAB model of color image in matlab

Comment only
05 May 2008 prabhakar raj

i want the code for tracking faces in an image sequence using particle filter,can u help me in this

15 Apr 2008 li jackey

how to recompile mex files?when the file is compiled in VC platform,the problem is always that mex.h can't be found,how to deal with it ,please tell me quickly,thank you

Comment only
03 Mar 2008 ophelka flowerfairy

to recompile simply use
mex filename.c

Comment only
02 Mar 2008 Sébastien Paris

Please recompile with the following command :

mex -output rgb2hsv_mex.dll rgb2hsv_mex.c

Comment only
02 Mar 2008 loi yii tien

Actually i am doing the object tracking in video using neural network. Can you send me an example of it if you can do it. Thanks!

Comment only
02 Mar 2008 loi yii tien

How to recompile each mex files? and i got error...

??? Invalid MEX-file 'C:\Program Files\MATLAB71\work\pf_colortracker\rgb2hsv_mex.dll': The specified procedure could not be found.


Error in ==> test_pf_colortracker at 202
im = rgb2hsv_mex(Z);

hope to hear u soon!

Comment only
02 Mar 2008 loi yii tien

How to recompile each mex files?

Comment only
12 Dec 2007 Tom Pinkiewicz

It works now and it looks excellent. Well done!

12 Dec 2007 Sebastien PARIS

You are right. I have just update with the missing file. Thx you....

Comment only
11 Dec 2007 Tom Pinkiewicz

The C files compiled okay but I get an error that function part_moment is undefined. Are you missing a file with this function?

Comment only
27 Jul 2009 1.2

-Minor changes

28 Dec 2010 1.5

-Add Linux64 with GCC support

13 Nov 2011 1.6

-Add likelihood based on gradient distribution
-Use mmreader instead of aviread

05 Jul 2012 1.7

-add #define max & min for LCC compiler

Contact us