Camshift Multiple Trackers Application and Source Code

This application demonstrates the implementation of multiple Camshift trackers to track multiple objects and regions in a video at the same time.
It is written in C++, using Qt and OpenCV. You can find the Executable and Source code at the bottom of this post.

When you run this program:
1. Open a video file or select Webcam as the video source
2. Select the objects you want to track in the video (select the region using your mouse)
3. Press “ESC” to stop the video, “p” to pause or un-pause and “k” to clear all of the trackers.

Camshift (Continuously Adaptive Mean Shift) algorithm is used for tracking regions of a video starting from any given frame throughout the next frames. The basis of this algorithm (as it can be inferred from its name) is the Mean Shift algorithm. For more information about the Mean Shift and Camshift algorithms you can visit the links below:

Mean Shift Wikipedia Article

Motion Analysis and Object Tracking

OpenCV: Mean Shift and Camshift

You can download the executable and source code from the links below:

Download Executable
Download Source Code

Please let me know if you find any problems in the program, or if you need help with compiling the source codes.

Below are some screenshots and a video from this program:

19 Replies to “Camshift Multiple Trackers Application and Source Code”

    1. Hi Sanjaya,
      Unfortunately you won’t find much about Arduino on my website, but to point you in some direction, each detected object is a Rect object or in other words four integer values (x, y, width and height) which you need to send. You can use this tutorial for the detection or tracking and search online about how to send integer values using Arduino, or topics like that.

      Sorry if this doesn’t help you much.

  1. Hello Amin,
    Thank you for preparing such a nice instructive site, it’s very helpful for beginners like we are.
    I am looking for a more generic arbitrary object problem. Object either can be a car, a boat, a plane or a human in the scene.
    I checked out the Camshift but it has some limitations especially for the brightness and the color similarities around the object under track.
    If you know, Can you advise more sophisticated tracking schemes which combines cascaded classifiers and the Camshift to run together?
    Any help is welcome
    Thank you

  2. hello ,i am a student and i have some question, how can I run with source code not exe file?
    Which software you compiler with?

      1. Yes it’s Qt. You can compile with C++ but based on your questions I recommend doing a little bit of research on the difference of “IDE” and “Compiler”.

  3. hello sir, i’m beginner to opencv. I want to track object from moving camera. I have did it for static camera using GMM. sir please help me for moving camera if u have did it or give me dome link…I’m not getting how to track using moving camera…thanks

  4. hello! now i’m begginer with opencv and studying about cam shift. It was greatlly helpuful for me. But i can’t find where is the code that allows sellect multiple object. Can you explain for me?

  5. I was trying Camshift for people counting along with Mog2.
    If i need to combine Camshift with any other method, which would be the best?
    Is there any algorithm/Tracker which indicates if any object is still in frame?
    Thanks for your reply.

    1. OK, for counting people there is another method (almost proven to work very good) which you can use instead of Mog2+Camshift. It’s already included in OpenCV so you can try it right away. It’s called HOG (Histogram of Oriented Gradients) and you can use it. It’s very efficient. Here is a link to HOG documentation in OpenCV:
      You need HOGDescriptor::getDefaultPeopleDetector. Let me know if this solves your problem.

  6. I tried camshift, if move the object out of the frame, the object is still tracked(marked) within the frame(at some region). How to get the status of the tracker in Camshift ?

    1. You’ll probably need to implement it yourself. The first thing that comes to mind is that you can check for the camshift track window size and if it is too small you can call it lost. Does this answer your question? If not, please let me know.

      1. Thanks for your reply that wouldn’t work in this senario because i need the tracker to be scale invarient(that was the reason i tried Camshift).
        Is there any other algorithm to check if the object is within the frame?

        1. Please note that Camshift is not an object detection method (not in most cases) and it only uses the color information (histograms with a custom bib to be precise) so there is no actual way to make sure what is being tracked is actually the same object that was set at first. So if you must use Camshift then my suggestion is that you go ahead with some assumptions. For example you can add assumptions of the minimum and maximum possible size of the object being tracked. Or you can set an ROI (region of interest) where you always expect your object to be. Can you tell me what is the exact object and environment that it’s in? Maybe I can come up with a better suggestion?!

          Other than all this, if you still insist on using Camshift and not make any assumptions, you should combine Camshift with some other method such as an object detection method or something.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.