1/25/2015

MIL, Boosting tracker test in opencv 3.0

I have tested MIL, Boosting tracking algorithm in opencv 3.0

Please refer to official reference here -> http://docs.opencv.org/trunk/modules/tracking/doc/tracking.html

Firstly, to use tracker algorithm, you have to set the value which is tracking module path of OPENCV_EXTRA_MODUALES_PATH option, when you build the opencv 3.0.



We can download tracking module in Github. -> https://github.com/Itseez/opencv_contrib
"opencv_contrib" is developing separately with opencv full version.
In setting cmake, I checked only tracking option. because other module may be unstable version yet.
And I need just tracking module.


The example source code is referenced in here -> https://github.com/lenlen/opencv/blob/tracking_api/samples/cpp/tracker.cpp

In source code, you can select Mil and Boosting algorithm option in the function of creation.

At the result, MIL is faster than boosting.
But both algorithm are very slow and performance is also not good yet.
So they can not use in industrial application.


MIL result video

Boosting result video


The example source code is here





#include < stdio.h>  
#include < opencv2\video\tracker.hpp>
#include < opencv2\opencv.hpp>


 
#ifdef _DEBUG          
#pragma comment(lib, "opencv_core300d.lib")  
#pragma comment(lib, "opencv_highgui300d.lib")  
#pragma comment(lib, "opencv_imgcodecs300d.lib")  
#pragma comment(lib, "opencv_videoio300d.lib") 
#pragma comment(lib, "opencv_imgproc300d.lib") 
#pragma comment(lib, "opencv_cuda300d.lib")   
#pragma comment(lib, "opencv_cudawarping300d.lib")
#pragma comment(lib, "opencv_tracking300d.lib")
#define _DEBUG_RRRR  
#else  
#pragma comment(lib, "opencv_core300.lib")  
#pragma comment(lib, "opencv_highgui300.lib")  
#pragma comment(lib, "opencv_imgcodecs300.lib")  
#pragma comment(lib, "opencv_videoio300.lib") 
#pragma comment(lib, "opencv_imgproc300.lib") 
#pragma comment(lib, "opencv_cuda300.lib")   
#pragma comment(lib, "opencv_cudawarping300.lib")
#pragma comment(lib, "opencv_tracking300d.lib")
#endif  


using namespace cv;
using namespace std;

static Mat image;
static bool selectObject = false;
static bool startSelection = false;
static Rect2d boundingBox;
static bool paused;

static void onMouse( int event, int x, int y, int, void* ) 
{ 
 if( !selectObject ) 
 { 
  switch ( event ) 
  { 
  case EVENT_LBUTTONDOWN: 
   //set origin of the bounding box 
   startSelection = true; 
   boundingBox.x = x; 
   boundingBox.y = y; 
   break; 
  case EVENT_LBUTTONUP: 
   //sei with and height of the bounding box 
   boundingBox.width = std::abs( x - boundingBox.x ); 
   boundingBox.height = std::abs( y - boundingBox.y ); 
   paused = false; 
   selectObject = true; 

   printf("Object Rect Size(left, right, width, height) %.1f %.1f %.1f %.1f\n", boundingBox.x, boundingBox.y, boundingBox.width, boundingBox.height);
   break; 
  case EVENT_MOUSEMOVE: 
   if( startSelection && !selectObject ) 
   { 
    //draw the bounding box 
    Mat currentFrame; 
    image.copyTo( currentFrame ); 
    rectangle( currentFrame, Point( boundingBox.x, boundingBox.y ), Point( x, y ), Scalar( 255, 0, 0 ), 2, 1 ); 
    imshow( "Tracking API", currentFrame ); 
   } 
   break; 
  } 
 } 
} 




int main()
{
 unsigned long AAtime=0, BBtime=0;

 VideoCapture cap;
 cap.open("C:\\videoSample\\tracking\\sample2.avi");

 if( !cap.isOpened() ) 
 { 
  printf("Video File Open Fail! \n");
  return -1; 
 } 


 Mat frame; 
 paused = false; 
 namedWindow( "Tracking", 0 ); 
 setMouseCallback( "Tracking", onMouse, 0 );


 //instantiates the specific Tracker 
 //MIL : TrackerMIL
 //BOOSTING : TrackerBoosting
 Ptr< Tracker> tracker = Tracker::create("BOOSTING" ); //"MIL");//
 if( tracker == NULL ) 
 { 
  printf("Error in the instantiation of the tracker..\n");
  return -1; 
 } 

 //get the first frame 
 cap >> frame; 
 frame.copyTo( image ); 
 imshow( "Tracking", image ); 

 bool initialized = false;
 while(1)
 { 
  if( !paused ) 
  { 
   cap >> frame; 
   frame.copyTo( image ); 


   if( !initialized && selectObject ) 
   { 
    //initializes the tracker 
    AAtime = getTickCount();
    if( !tracker->init( frame, boundingBox ) ) 
    { 
     printf("Could not initialize tracker\n"); 
     return -1; 
    } 
    BBtime = getTickCount();
    double fps = (BBtime - AAtime)/getTickFrequency();
    double sec = 1/fps;
    printf("Tracking Initial time = %.2lffsp, %.2lfsec \n",  fps, sec );

    initialized = true; 
   } 
   else if( initialized ) 
   { 
    AAtime = getTickCount();
    
    //updates the tracker 
    if( tracker->update( frame, boundingBox ) ) 
    { 
     rectangle( image, boundingBox, Scalar( 255, 0, 0 ), 2, 1 ); 
    } 

    BBtime = getTickCount();
    double fps = (BBtime - AAtime)/getTickFrequency();
    double sec = 1/fps;
    printf("Tracking update time = %.2lffsp, %.2lfsec \n",  fps, sec );

   } 
   imshow( "Tracking", image ); 
  } 


  char c = (char) waitKey( 2 ); 
  if( c == 'q' ) 
   break; 
  if( c == 'p' ) 
   paused = !paused; 


 } 
}