Camshift Multiple Trackers Application and Source Code

Posted on Posted in OpenCV Tutorials, Projects, Tutorials, Windows Applications

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:

10 thoughts on “Camshift Multiple Trackers Application and Source Code

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

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

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

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

Leave a Reply

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