Ask Your Question
3

How to perform TLD tracking on ROI bounding box using openCV c++ builtin methods ?

asked 2015-01-12 19:02:00 -0500

sid gravatar image

updated 2015-09-26 09:02:34 -0500

I Need to implement TLD in visual studio C++ windows 8 64bit . need help. Which opencv to Download? thanx in advance.

edit retag flag offensive close merge delete

Comments

Hi I can not run this code I have problems with the installation of additional modules while someone postarmi its solution?

improc gravatar imageimproc ( 2016-04-18 12:37:55 -0500 )edit

2 answers

Sort by ยป oldest newest most voted
11

answered 2015-01-13 02:25:51 -0500

berak gravatar image

updated 2015-04-26 04:31:10 -0500

if you want to use the builtin tld tracker, you need opencv3.0 and also the opencv_contrib repo

here's some sample code, you'll see, it's quite simple:

#include <opencv2/core/utility.hpp>
#include <opencv2/tracking.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <cstring>

using namespace std;
using namespace cv;

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

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;
        boundingBox.width = boundingBox.height = 0;
        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;
        break;
      case EVENT_MOUSEMOVE:

        if( startSelection && !selectObject )
        {
          //draw the bounding box
          Mat currentFrame;
          image.copyTo( currentFrame );
          rectangle( currentFrame, Point((int) boundingBox.x, (int)boundingBox.y ), Point( x, y ), Scalar( 255, 0, 0 ), 2, 1 );
          imshow( "Tracking API", currentFrame );
        }
        break;
    }
  }
}

//
//  Hot keys: 
//   q - quit the program
//   p - pause video
//

int main( int argc, char** argv )
{
    //open the capture
    VideoCapture cap(0);
    if( !cap.isOpened() )
    {
      return -1;
    }

    //
    //  "MIL", "BOOSTING", "MEDIANFLOW", "TLD"
    //
    string tracker_algorithm = "MIL";
    if ( argc>1 ) tracker_algorithm = argv[1];

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

    Ptr<Tracker> tracker = Tracker::create( tracker_algorithm );
    if( tracker == NULL )
    {
        cout << "***Error in the instantiation of the tracker...***\n";
        return -1;
    }

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

    bool initialized = false;
    int frameCounter = 0;

    for ( ;; )
    {
        char c = (char) waitKey( 2 );
        if( c == 'q' || c == 27 )
            break;
        if( c == 'p' )
            paused = !paused;

        if ( !paused )
        {
            cap >> frame;
            if(frame.empty())
            {
                break;
            }
            frame.copyTo( image );

            if( selectObject )
            {
                if( !initialized )
                {
                    //initializes the tracker
                    if( !tracker->init( frame, boundingBox ) )
                    {
                    cout << "***Could not initialize tracker...***\n";
                    return -1;
                    }
                    initialized = true;
                }
                else
                {
                    //updates the tracker
                    if( tracker->update( frame, boundingBox ) )
                    {
                        rectangle( image, boundingBox, Scalar( 255, 0, 0 ), 2, 1 );
                    }
                }
            }
            imshow( "Tracking API", image );
            frameCounter++;
        }
    }

    return 0;
}
edit flag offensive delete link more

Comments

1

its quite slow even on release

sid gravatar imagesid ( 2015-01-22 01:35:01 -0500 )edit
1

You have to make it using cmake with extra modules.

sid gravatar imagesid ( 2015-03-19 13:19:29 -0500 )edit

I have successfully install the extra modules.But I don't know how to run the program and I don't install Qt.Could you tell me what should I do in details?Can the program run via the cmd command?

Sheng Liu gravatar imageSheng Liu ( 2015-04-25 04:11:44 -0500 )edit
1
  • you do not need Qt (it does not matter, which gui you're using)
  • yes, just run it on the cmdline . ./prog TLD
berak gravatar imageberak ( 2015-04-25 05:33:48 -0500 )edit

Thanks for your reply. I faced a situation when run the TLD.

E:\300cmakegongcheng\testTLD\x64\Debug>testTLD

VIDEOINPUT LIBRARY - 0.1995 - TFW07

This example shows the functionality of "Long-term optical tracking API"-- pause video [p] and draw a bounding box around the target to start the tracker Example of <video_name> is in opencv_extra/testdata/cv/tracking/ Call: ./tracker <tracker_algorithm> <video_name> <start_frame> [<bounding_frame>]

Hot keys: q - quit the program p - pause video

But I have not found the opencv_extra/testdata/cv/tracking.Could you tell me the command in details to run it on the cmdline?

Sheng Liu gravatar imageSheng Liu ( 2015-04-26 04:12:00 -0500 )edit
1

you can use any video src (you don't need the example vid from opencv_extra, this was just an example)

above code is actually using the webcam, not a video file, (i just forgot to remove the text in the help function)

if you use the tracker.cpp from tracking/samples, run it as:

`testTLD  TLD  some_video.avi`
berak gravatar imageberak ( 2015-04-26 04:24:15 -0500 )edit

Thanks in advance!

Sheng Liu gravatar imageSheng Liu ( 2015-04-26 04:27:48 -0500 )edit
1

sorry i don't see, where your problem is

berak gravatar imageberak ( 2015-04-26 04:39:49 -0500 )edit

Thanks in advance.I see!

Sheng Liu gravatar imageSheng Liu ( 2015-04-26 05:13:36 -0500 )edit

It's a great work!Thanks for sharing. And is there any methods to increase the speed of the program?

Sheng Liu gravatar imageSheng Liu ( 2015-04-26 06:47:02 -0500 )edit
0

answered 2016-04-20 10:31:50 -0500

atv gravatar image

Could i use this to track multiple objects? And could i use the images stored in image for training purposes with cascade files?

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

2 followers

Stats

Asked: 2015-01-12 19:02:00 -0500

Seen: 8,038 times

Last updated: Apr 20 '16