Skip to content

Commit

Permalink
tracking: improve public API
Browse files Browse the repository at this point in the history
Use ROI selector in python samples
  • Loading branch information
sovrasov committed Apr 24, 2017
1 parent 4317e27 commit 3ac9e24
Show file tree
Hide file tree
Showing 27 changed files with 268 additions and 447 deletions.
127 changes: 63 additions & 64 deletions modules/tracking/include/opencv2/tracking/tracker.hpp
Expand Up @@ -47,12 +47,6 @@
#include "feature.hpp"
#include "onlineMIL.hpp"
#include "onlineBoosting.hpp"
#include <iostream>


#define BOILERPLATE_CODE(name,classname) \

This comment has been minimized.

Copy link
@kurnianggoro

kurnianggoro May 29, 2017

Contributor

@sovrasov hi, i am currently make an implementation of facelandmark API based on this tracker API,
could you please tell me the reason of removing this part (also the other one in tracker.cpp)?
Because I think it makes the user more convenience to declare a new tracker (i.e. using string with tracker name).
Is there any performance issue or something like that if we implement in this way?

This comment has been minimized.

Copy link
@sovrasov

sovrasov May 29, 2017

Author Contributor

It's not related to performance. There are plans to move part of the tracking algorithms to the main opencv, so we need to have more flexible code.
User can write such a function by himself (like I did in samples_utility.hpp).

static Ptr<classname> createTracker(const classname::Params &parameters=classname::Params());\
virtual ~classname(){};

/*
* Partially based on:
Expand Down Expand Up @@ -539,7 +533,7 @@ class CV_EXPORTS_W Tracker : public virtual Algorithm
@return True if initialization went succesfully, false otherwise
*/
CV_WRAP bool init( const Mat& image, const Rect2d& boundingBox );
CV_WRAP bool init( InputArray image, const Rect2d& boundingBox );

/** @brief Update the tracker, find the new most likely bounding box for the target
@param image The current frame
Expand All @@ -550,17 +544,7 @@ class CV_EXPORTS_W Tracker : public virtual Algorithm
current frame. Note, that latter *does not* imply that tracker has failed, maybe target is indeed
missing from the frame (say, out of sight)
*/
CV_WRAP bool update( const Mat& image, CV_OUT Rect2d& boundingBox );

/** @brief Creates a tracker by its name.
@param trackerType Tracker type
The following detector types are supported:
- "MIL" -- TrackerMIL
- "BOOSTING" -- TrackerBoosting
*/
CV_WRAP static Ptr<Tracker> create( const String& trackerType );
CV_WRAP bool update( InputArray image, CV_OUT Rect2d& boundingBox );

virtual void read( const FileNode& fn )=0;
virtual void write( FileStorage& fs ) const=0;
Expand Down Expand Up @@ -1078,7 +1062,7 @@ based on @cite MIL .
Original code can be found here <http://vision.ucsd.edu/~bbabenko/project_miltrack.shtml>
*/
class CV_EXPORTS TrackerMIL : public Tracker
class CV_EXPORTS_W TrackerMIL : public Tracker
{
public:
struct CV_EXPORTS Params
Expand All @@ -1100,15 +1084,19 @@ class CV_EXPORTS TrackerMIL : public Tracker
/** @brief Constructor
@param parameters MIL parameters TrackerMIL::Params
*/
BOILERPLATE_CODE("MIL",TrackerMIL);
static Ptr<TrackerMIL> create(const TrackerMIL::Params &parameters);

CV_WRAP static Ptr<TrackerMIL> create();

virtual ~TrackerMIL() {}
};

/** @brief This is a real-time object tracking based on a novel on-line version of the AdaBoost algorithm.
The classifier uses the surrounding background as negative examples in update step to avoid the
drifting problem. The implementation is based on @cite OLB .
*/
class CV_EXPORTS TrackerBoosting : public Tracker
class CV_EXPORTS_W TrackerBoosting : public Tracker
{
public:
struct CV_EXPORTS Params
Expand All @@ -1133,7 +1121,11 @@ class CV_EXPORTS TrackerBoosting : public Tracker
/** @brief Constructor
@param parameters BOOSTING parameters TrackerBoosting::Params
*/
BOILERPLATE_CODE("BOOSTING",TrackerBoosting);
static Ptr<TrackerBoosting> create(const TrackerBoosting::Params &parameters);

CV_WRAP static Ptr<TrackerBoosting> create();

virtual ~TrackerBoosting() {}
};

/** @brief Median Flow tracker implementation.
Expand All @@ -1146,7 +1138,7 @@ by authors to outperform MIL). During the implementation period the code at
<http://www.aonsquared.co.uk/node/5>, the courtesy of the author Arthur Amarra, was used for the
reference purpose.
*/
class CV_EXPORTS TrackerMedianFlow : public Tracker
class CV_EXPORTS_W TrackerMedianFlow : public Tracker
{
public:
struct CV_EXPORTS Params
Expand All @@ -1168,7 +1160,11 @@ class CV_EXPORTS TrackerMedianFlow : public Tracker
/** @brief Constructor
@param parameters Median Flow parameters TrackerMedianFlow::Params
*/
BOILERPLATE_CODE("MEDIANFLOW",TrackerMedianFlow);
static Ptr<TrackerMedianFlow> create(const TrackerMedianFlow::Params &parameters);

CV_WRAP static Ptr<TrackerMedianFlow> create();

virtual ~TrackerMedianFlow() {}
};

/** @brief TLD is a novel tracking framework that explicitly decomposes the long-term tracking task into
Expand All @@ -1182,7 +1178,7 @@ The Median Flow algorithm (see cv::TrackerMedianFlow) was chosen as a tracking c
implementation, following authors. Tracker is supposed to be able to handle rapid motions, partial
occlusions, object absence etc.
*/
class CV_EXPORTS TrackerTLD : public Tracker
class CV_EXPORTS_W TrackerTLD : public Tracker
{
public:
struct CV_EXPORTS Params
Expand All @@ -1195,7 +1191,11 @@ class CV_EXPORTS TrackerTLD : public Tracker
/** @brief Constructor
@param parameters TLD parameters TrackerTLD::Params
*/
BOILERPLATE_CODE("TLD",TrackerTLD);
static Ptr<TrackerTLD> create(const TrackerTLD::Params &parameters);

CV_WRAP static Ptr<TrackerTLD> create();

virtual ~TrackerTLD() {}
};

/** @brief KCF is a novel tracking framework that utilizes properties of circulant matrix to enhance the processing speed.
Expand All @@ -1204,7 +1204,7 @@ class CV_EXPORTS TrackerTLD : public Tracker
* as well as the matlab implementation. For more information about KCF with color-names features, please refer to
* <http://www.cvl.isy.liu.se/research/objrec/visualtracking/colvistrack/index.html>.
*/
class CV_EXPORTS TrackerKCF : public Tracker
class CV_EXPORTS_W TrackerKCF : public Tracker
{
public:
/**
Expand Down Expand Up @@ -1251,12 +1251,16 @@ class CV_EXPORTS TrackerKCF : public Tracker
int desc_npca; //!< non-compressed descriptors of TrackerKCF::MODE
};

virtual void setFeatureExtractor(void(*)(const Mat, const Rect, Mat&), bool pca_func = false);
virtual void setFeatureExtractor(void(*)(const Mat, const Rect, Mat&), bool pca_func = false) = 0;

/** @brief Constructor
@param parameters KCF parameters TrackerKCF::Params
*/
BOILERPLATE_CODE("KCF", TrackerKCF);
static Ptr<TrackerKCF> create(const TrackerKCF::Params &parameters);

CV_WRAP static Ptr<TrackerKCF> create();

virtual ~TrackerKCF() {}
};

/** @brief GOTURN (@cite GOTURN) is kind of trackers based on Convolutional Neural Networks (CNN). While taking all advantages of CNN trackers,
Expand All @@ -1272,7 +1276,7 @@ class CV_EXPORTS TrackerKCF : public Tracker
* <https://github.com/Auron-X/GOTURN_Training_Toolkit>
* GOTURN architecture goturn.prototxt and trained model goturn.caffemodel are accessible on opencv_extra GitHub repository.
*/
class CV_EXPORTS TrackerGOTURN : public Tracker
class CV_EXPORTS_W TrackerGOTURN : public Tracker
{
public:
struct CV_EXPORTS Params
Expand All @@ -1285,24 +1289,26 @@ class CV_EXPORTS TrackerGOTURN : public Tracker
/** @brief Constructor
@param parameters GOTURN parameters TrackerGOTURN::Params
*/
BOILERPLATE_CODE("GOTURN", TrackerGOTURN);
static Ptr<TrackerGOTURN> create(const TrackerGOTURN::Params &parameters);

CV_WRAP static Ptr<TrackerGOTURN> create();

virtual ~TrackerGOTURN() {}
};

/************************************ MultiTracker Class ---By Laksono Kurnianggoro---) ************************************/
/** @brief This class is used to track multiple objects using the specified tracker algorithm.
* The MultiTracker is naive implementation of multiple object tracking.
* It process the tracked objects independently without any optimization accross the tracked objects.
*/
class CV_EXPORTS_W MultiTracker
class CV_EXPORTS_W MultiTracker : public Algorithm
{
public:

/**
* \brief Constructor.
* In the case of trackerType is given, it will be set as the default algorithm for all trackers.
* @param trackerType the name of the tracker algorithm to be used
*/
CV_WRAP MultiTracker(const String& trackerType = "");
CV_WRAP MultiTracker();

/**
* \brief Destructor
Expand All @@ -1311,58 +1317,51 @@ class CV_EXPORTS_W MultiTracker

/**
* \brief Add a new object to be tracked.
* The defaultAlgorithm will be used the newly added tracker.
* @param image input image
* @param boundingBox a rectangle represents ROI of the tracked object
*/
CV_WRAP bool add(const Mat& image, const Rect2d& boundingBox);

/**
* \brief Add a new object to be tracked.
* @param trackerType the name of the tracker algorithm to be used
*
* @param newTracker tracking algorithm to be used
* @param image input image
* @param boundingBox a rectangle represents ROI of the tracked object
*/
CV_WRAP bool add(const String& trackerType, const Mat& image, const Rect2d& boundingBox);
CV_WRAP bool add(Ptr<Tracker> newTracker, InputArray image, const Rect2d& boundingBox);

/**
* \brief Add a set of objects to be tracked.
* @param trackerType the name of the tracker algorithm to be used
* @param newTrackers list of tracking algorithms to be used
* @param image input image
* @param boundingBox list of the tracked objects
*/
CV_WRAP bool add(const String& trackerType, const Mat& image, std::vector<Rect2d> boundingBox);
bool add(std::vector<Ptr<Tracker> > newTrackers, InputArray image, std::vector<Rect2d> boundingBox);

/**
* \brief Add a set of objects to be tracked using the defaultAlgorithm tracker.
* \brief Update the current tracking status.
* The result will be saved in the internal storage.
* @param image input image
* @param boundingBox list of the tracked objects
*/
CV_WRAP bool add(const Mat& image, std::vector<Rect2d> boundingBox);
bool update(InputArray image);

/**
* \brief Update the current tracking status.
* The result will be saved in the internal storage.
* @param image input image
* @param boundingBox the tracking result, represent a list of ROIs of the tracked objects.
*/
bool update(const Mat& image);
CV_WRAP bool update(InputArray image, CV_OUT std::vector<Rect2d> & boundingBox);

//!< storage for the tracked objects, each object corresponds to one tracker algorithm.
std::vector<Rect2d> objects;
/**
* \brief Returns a reference to a storage for the tracked objects, each object corresponds to one tracker algorithm
*/
CV_WRAP const std::vector<Rect2d>& getObjects() const;

/**
* \brief Update the current tracking status.
* @param image input image
* @param boundingBox the tracking result, represent a list of ROIs of the tracked objects.
* \brief Returns a pointer to a new instance of MultiTracker
*/
CV_WRAP bool update(const Mat& image, CV_OUT std::vector<Rect2d> & boundingBox);
CV_WRAP static Ptr<MultiTracker> create();

protected:
//!< storage for the tracker algorithms.
std::vector< Ptr<Tracker> > trackerList;

//!< default algorithm for the tracking method.
String defaultAlgorithm;
//!< storage for the tracked objects, each object corresponds to one tracker algorithm.
std::vector<Rect2d> objects;
};

/************************************ Multi-Tracker Classes ---By Tyan Vladimir---************************************/
Expand All @@ -1384,11 +1383,11 @@ class CV_EXPORTS MultiTracker_Alt
/** @brief Add a new target to a tracking-list and initialize the tracker with a know bounding box that surrounding the target
@param image The initial frame
@param boundingBox The initial boundig box of target
@param tracker_algorithm_name Multi-tracker algorithm name
@param tracker_algorithm Multi-tracker algorithm
@return True if new target initialization went succesfully, false otherwise
*/
bool addTarget(const Mat& image, const Rect2d& boundingBox, String tracker_algorithm_name);
bool addTarget(InputArray image, const Rect2d& boundingBox, Ptr<Tracker> tracker_algorithm);

/** @brief Update all trackers from the tracking-list, find a new most likely bounding boxes for the targets
@param image The current frame
Expand All @@ -1397,7 +1396,7 @@ class CV_EXPORTS MultiTracker_Alt
current frame. Note, that latter *does not* imply that tracker has failed, maybe target is indeed
missing from the frame (say, out of sight)
*/
bool update(const Mat& image);
bool update(InputArray image);

/** @brief Current number of targets in tracking-list
*/
Expand Down Expand Up @@ -1441,7 +1440,7 @@ class CV_EXPORTS MultiTrackerTLD : public MultiTracker_Alt
current frame. Note, that latter *does not* imply that tracker has failed, maybe target is indeed
missing from the frame (say, out of sight)
*/
bool update_opt(const Mat& image);
bool update_opt(InputArray image);
};

//! @}
Expand Down
8 changes: 4 additions & 4 deletions modules/tracking/perf/perf_Tracker.cpp
Expand Up @@ -155,7 +155,7 @@ PERF_TEST_P(tracking, mil, testing::Combine(TESTSET_NAMES, SEGMENTS))
bool initialized = false;
vector<Rect> bbs;

Ptr<Tracker> tracker = Tracker::create( "MIL" );
Ptr<Tracker> tracker = TrackerMIL::create();
string folder = TRACKING_DIR + "/" + video + "/" + FOLDER_IMG;
int numSegments = ( sizeof ( SEGMENTS)/sizeof(int) );
int endFrame = 0;
Expand Down Expand Up @@ -226,7 +226,7 @@ PERF_TEST_P(tracking, boosting, testing::Combine(TESTSET_NAMES, SEGMENTS))
bool initialized = false;
vector<Rect> bbs;

Ptr<Tracker> tracker = Tracker::create( "BOOSTING" );
Ptr<Tracker> tracker = TrackerBoosting::create();
string folder = TRACKING_DIR + "/" + video + "/" + FOLDER_IMG;
int numSegments = ( sizeof ( SEGMENTS)/sizeof(int) );
int endFrame = 0;
Expand Down Expand Up @@ -296,7 +296,7 @@ PERF_TEST_P(tracking, tld, testing::Combine(TESTSET_NAMES, SEGMENTS))
bool initialized = false;
vector<Rect> bbs;

Ptr<Tracker> tracker = Tracker::create( "TLD" );
Ptr<Tracker> tracker = TrackerTLD::create();
string folder = TRACKING_DIR + "/" + video + "/" + FOLDER_IMG;
int numSegments = ( sizeof ( SEGMENTS)/sizeof(int) );
int endFrame = 0;
Expand Down Expand Up @@ -366,7 +366,7 @@ PERF_TEST_P(tracking, GOTURN, testing::Combine(TESTSET_NAMES, SEGMENTS))
bool initialized = false;
vector<Rect> bbs;

Ptr<Tracker> tracker = Tracker::create("GOTURN");
Ptr<Tracker> tracker = TrackerGOTURN::create();
string folder = TRACKING_DIR + "/" + video + "/" + FOLDER_IMG;
int numSegments = (sizeof(SEGMENTS) / sizeof(int));
int endFrame = 0;
Expand Down
4 changes: 3 additions & 1 deletion modules/tracking/samples/benchmark.cpp
Expand Up @@ -3,6 +3,7 @@
#include "opencv2/tracking.hpp"
#include "opencv2/videoio.hpp"
#include "opencv2/plot.hpp"
#include "samples_utility.hpp"
#include <fstream>
#include <iomanip>
#include <iostream>
Expand Down Expand Up @@ -75,10 +76,11 @@ const int LTRC_COUNT = 100;
struct AlgoWrap
{
AlgoWrap(const string &name_)
: tracker(Tracker::create(name_)), lastState(NotFound), name(name_), color(getNextColor()),
: lastState(NotFound), name(name_), color(getNextColor()),
numTotal(0), numResponse(0), numPresent(0), numCorrect_0(0), numCorrect_0_5(0),
timeTotal(0), auc(LTRC_COUNT + 1, 0)
{
tracker = createTrackerByName(name);
}

enum State
Expand Down
2 changes: 1 addition & 1 deletion modules/tracking/samples/goturnTracker.cpp
Expand Up @@ -140,7 +140,7 @@ int main(int argc, char *argv[])
setMouseCallback("GOTURN Tracking", onMouse, 0);

//Create GOTURN tracker
Ptr<Tracker> tracker = Tracker::create("GOTURN");
Ptr<Tracker> tracker = TrackerGOTURN::create();

//Load and init full ALOV300++ dataset with a given datasetID, as alternative you can use loadAnnotatedOnly(..)
//to load only frames with labled ground truth ~ every 5-th frame
Expand Down
2 changes: 1 addition & 1 deletion modules/tracking/samples/kcf.cpp
Expand Up @@ -52,7 +52,7 @@ int main( int argc, char** argv ){
BoxExtractor box;

// create the tracker
Ptr<Tracker> tracker = Tracker::create( "KCF" );
Ptr<Tracker> tracker = TrackerKCF::create();

// set input video
std::string video = argv[1];
Expand Down
3 changes: 2 additions & 1 deletion modules/tracking/samples/multiTracker_dataset.cpp
Expand Up @@ -48,6 +48,7 @@
#include <opencv2/tracking.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include "samples_utility.hpp"
#include <iostream>

using namespace std;
Expand Down Expand Up @@ -184,7 +185,7 @@ int main(int argc, char *argv[])
//Initialize the tracker and add targets
for (int i = 0; i < (int)boundingBoxes.size(); i++)
{
if (!mt.addTarget(frame, boundingBoxes[i], tracker_algorithm))
if (!mt.addTarget(frame, boundingBoxes[i], createTrackerByName(tracker_algorithm)))
{
cout << "Trackers Init Error!!!";
return 0;
Expand Down

0 comments on commit 3ac9e24

Please sign in to comment.