File Exchange

image thumbnail

Particle Filter Color Tracker

version (12 MB) by Sebastien PARIS
Tracking an object in a video with a Color Particle Filter


Updated 24 Jan 2020

View Version History

View License

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

Cite As

Sebastien PARIS (2021). Particle Filter Color Tracker (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (88)


Sebastien PARIS

@Consult_Ahmad: There are two versions. the first uses pdfcolor_ellipserand which effectively sample "uniformely". The number of bins is a prior I tweaked on this particular video file.

Consult Ahmad

I have a really cool time with running and understanding PF filters using your code. I have a couple of queries though, First: does the pdf generator uses randomly selected pixels for constructing color histograms and Second: how do I get the "bin numbers" of those pixels. Thank you

Sebastien PARIS

@Nikhita in pdfcolor_ellipserand.c
@EKC should works now with modern PC & Matlab

Nikhita More

Sir, this code is really helpful.. thank you,
but i wanted to know where are the weights calculation in the code


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?

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.

Huang Bo-Yang


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


fu yun

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?



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


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


Sebastien PARIS


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


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 .

Sebastien PARIS


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


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

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.

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|


You know how it doing ?

Sebastien PARIS


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


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


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


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;

Dasar Sisten Kontrol

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

Dasar Sisten Kontrol


thanku sir..

Sebastien PARIS

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

for example


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

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?


waiting for u r valuable reply



sahere rahimi

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

Sebastien PARIS

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


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.

Sebastien PARIS

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


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.

Sebastien PARIS

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


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.

... ...

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.


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


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.

Sebastien PARIS


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



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

Sathiyaraj Manoharan

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

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.

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

Sebastien PARIS


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

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?


Thanks for sharing dude..

Sebastien PARIS

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

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


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


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

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.

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.

Kevin Murphy

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.

Duy Nguyen

Duy Nguyen

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

Hong 4

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

Hong 4

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

Yong Zhao

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


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

Fang-I Chu

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

yasong guo

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

Sebastien PARIS

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


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! :(

Tedo Biresaw

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

Sebastien PARIS

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

Thx all for comments/ratings

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

David Norville

Got it to work. Thanks

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.


thanks for you sharing

min hu

thanks for is very good.

nauha jam

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

prabhakar raj

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

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

ophelka flowerfairy

to recompile simply use
mex filename.c

Sébastien Paris

Please recompile with the following command :

mex -output rgb2hsv_mex.dll rgb2hsv_mex.c

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!

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!

loi yii tien

How to recompile each mex files?

Tom Pinkiewicz

It works now and it looks excellent. Well done!

Sebastien PARIS

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

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?

MATLAB Release Compatibility
Created with R2016b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!