Please see the following blog post for more details about this code Object Tracking using OpenCV (C++/Python)