MOTION TRACKING USING OPENCV

Hello Friends,

While researching about various trackers in my hexapod project I came across a very simple python code that was tracking on the basis of movements. But it was based on old Matlab API. So I wanted to implement it in OpenCV. Tracking any object in a video is a very important part in the field of Robotics. For eg. suppose you want to track moving vehicles at traffic signals(Project Transpose,IIM Ahmedabad), track moving obstacles for an autonomous robot( Project Hexapod,Bits Pilani KK Birla Goa Campus), finding life existence in unmanned areas, etc.

You can download the code from here: https://github.com/abhi-kumar/OPENCV_MISC/blob/master/track_motion.cpp

Lets go through the major snippets of the code.

#include <stdio.h>
#include <cv.h>
#include <highgui.h>

These are the libraries for the old C based OpenCV modules

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/video/tracking.hpp"

These are the libraries for the new C++ based OpenCV modules

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

The standard C++ libraries

float MHI_DURATION = 0.05;                
int DEFAULT_THRESHOLD = 32;
float MAX_TIME_DELTA = 12500.0;
float MIN_TIME_DELTA = 5;
int visual_trackbar = 2;

These are the parameters to be used in the tracking function. Please note that they may change according to the type of camera being used.
1.Timestamp– Current time in milliseconds or other units.
2.MHI_DURATION-Maximal duration of the motion track in the same units as timestamp
3.DELTA_TIME-Minimal (or maximal) allowed difference between mhi values within a pixel neighborhood.

updateMotionHistory(motion_mask,motion_history,timestamp,MHI_DURATION);			
calcMotionGradient(motion_history, mg_mask, mg_orient, 5, 12500.0, 3);
segmentMotion(motion_history, seg_mask, seg_bounds, timestamp, 32);

To understand these three major lines you must go through these links
1. http://docs.opencv.org/modules/video/doc/motion_analysis_and_object_tracking.html#updatemotionhistory

2. http://docs.opencv.org/modules/video/doc/motion_analysis_and_object_tracking.html#calcmotiongradient

3. http://docs.opencv.org/modules/video/doc/motion_analysis_and_object_tracking.html#segmentmotion

Now the compilation and running the application in ubuntu
1.Download the code
2.Open a terminal an traverse to the folder containing the code
(Assuming you named the code file as “track_motion.cpp”)
3.Type
a)chmod +x track_motion.cpp
b)g++ -ggdb `pkg-config –cflags opencv` -o `basename track_motion.cpp .cpp` track_motion.cpp `pkg-config –libs opencv`
c)./track_motion

The default trackbar will be set to binary view, any motion detected will be tracked in white color. Changing the trackbar position to number “1” will provide a grayscale view and in the same way number “0” is RGB and number “3” is in HSV.

Here is a demo video link to get an overview of the different views of the application:

I hope you benefit from this code.

Thank you 🙂

Advertisements

9 thoughts on “MOTION TRACKING USING OPENCV”

  1. No such file or directory error i got fo r#include
    #include .. I have implmented opencv correctly. I also run video capturing program but this one gives me as error.

      1. 1>Source.cpp(16): warning C4305: ‘initializing’ : truncation from ‘double’ to ‘float’
        1>Source.cpp(92): error C3861: ‘updateMotionHistory’: identifier not found
        1>Source.cpp(93): error C3861: ‘calcMotionGradient’: identifier not found
        1>Source.cpp(94): error C3861: ‘segmentMotion’: identifier not found
        1>Source.cpp(114): warning C4244: ‘=’ : conversion from ‘double’ to ‘uchar’, possible loss of data
        1>Source.cpp(128): warning C4244: ‘=’ : conversion from ‘float’ to ‘uchar’, possible loss of data
        1>Source.cpp(129): warning C4244: ‘initializing’ : conversion from ‘int’ to ‘float’, possible loss of data
        1>Source.cpp(130): warning C4244: ‘=’ : conversion from ‘float’ to ‘uchar’, possible loss of data
        1>Source.cpp(148): error C3861: ‘calcGlobalOrientation’: identifier not found
        1>Source.cpp(182): error C2065: ‘M_PI’ : undeclared identifier

      2. Hello Rushikesh,

        Which version of OpenCV are you using? This code’s functions are related to OpenCV 2.4.6 library. And since you get an error with M_PI, your way of linking the library files while compiling may be incorrect. M_PI is associated with math.h basic c++ header file. So also let me know as to how are you compiling the code. Or there might be an installation problem, you can check that by running(compiling) a basic image input and display code. Post it here or mail the details at abhishek4273@gmail.com.

        Regards,
        Abhishek

  2. I would suggest you switch back to the newest stable one 2.4.9 or 2.4.8. Can I have the compilation command you are using, because definitely M_PI should not give an error, the others are version up-gradation issues. Check the documentation with the revised syntax.

    Regards,
    Abhishek

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s