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:
Motion Analysis and Object Tracking
OpenCV: Mean Shift and Camshift
You can download the executable and source code from the links below:
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:
https://www.youtube.com/watch?v=-Yy_GA-7SNM
which open cv version you are using
hello sir, I am a beginner to opencv. Can I add the coordinates and size of the detected object and send data to Arduino?
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.
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
With the object types you’ve mentioned (car, boat, plane and so on), cascade classifiers should prove to be accurate and fast enough if you have enough data.
See this:
http://amin-ahmadi.com/cascade-trainer-gui/
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?
Thanks!!=)
Is the software call “QT” ? or other?
Thanks!!!
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”.
I’d like to detect and track drone, do you have any suggestions please?
You might want to try Cascade Classifiers.
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
I suggest you carefully go through the method already described in this post. Camshift can help you out with moving camera too. You can also use Kalman Filter for tracking an object. I think you should go through the classes described here, there are lots of methods already implemented in OpenCV:
http://docs.opencv.org/2.4/modules/video/doc/motion_analysis_and_object_tracking.html
thanks a lot sir…i’ll try it
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?
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.
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: http://docs.opencv.org/2.4/modules/gpu/doc/object_detection.html
You need HOGDescriptor::getDefaultPeopleDetector. Let me know if this solves your problem.
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 ?
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.
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?
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.