From eb0c67065d754f170d22df30e628290ae6b354af Mon Sep 17 00:00:00 2001 From: Kurnianggoro Date: Sun, 5 Jul 2015 10:34:37 +0900 Subject: [PATCH] naive implementation of the multi tracker --- .../include/opencv2/tracking/tracker.hpp | 69 ++++++++++++++++++- modules/tracking/src/multiTracker.cpp | 61 +++++++++++++++- 2 files changed, 128 insertions(+), 2 deletions(-) diff --git a/modules/tracking/include/opencv2/tracking/tracker.hpp b/modules/tracking/include/opencv2/tracking/tracker.hpp index 3711c3ab1..3709b2799 100644 --- a/modules/tracking/include/opencv2/tracking/tracker.hpp +++ b/modules/tracking/include/opencv2/tracking/tracker.hpp @@ -1245,14 +1245,81 @@ class CV_EXPORTS_W TrackerKCF : public Tracker //! @} +/************************************ MultiTracker Class ************************************/ +/** @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 { 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 + */ + MultiTracker(const String& trackerType = "" ); + + /** + * \brief Destructor + */ + ~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 + */ + 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 image input image + * @param boundingBox a rectangle represents ROI of the tracked object + */ bool add( const String& trackerType, const Mat& 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 image input image + * @param boundingBox list of the tracked objects + */ + bool add(const String& trackerType, const Mat& image, std::vector boundingBox); + + /** + * \brief Add a set of objects to be tracked using the defaultAlgorithm tracker. + * @param image input image + * @param boundingBox list of the tracked objects + */ + bool add(const Mat& image, std::vector boundingBox); + + /** + * \brief Update the current tracking status. + * The result will be saved in the internal storage. + * @param image input image + */ + bool update( const Mat& image); + + //!< storage for the tracked objects, each object corresponds to one tracker algorithm. + std::vector objects; + + /** + * \brief Update the current tracking status. + * @param image input image + * @param boundingBox the tracking result, represent a list of ROIs of the tracked objects. + */ bool update( const Mat& image, std::vector & boundingBox ); + protected: + //!< storage for the tracker algorithms. std::vector< Ptr > trackerList; - std::vector objects; + + //!< default algorithm for the tracking method. + String defaultAlgorithm; }; } /* namespace cv */ diff --git a/modules/tracking/src/multiTracker.cpp b/modules/tracking/src/multiTracker.cpp index c032f32c6..ac4fd65a2 100644 --- a/modules/tracking/src/multiTracker.cpp +++ b/modules/tracking/src/multiTracker.cpp @@ -41,21 +41,80 @@ namespace cv { + + // constructor + MultiTracker::MultiTracker(const String& trackerType):defaultAlgorithm(trackerType){}; + + // destructor + MultiTracker::~MultiTracker(){}; + + // add an object to be tracked, defaultAlgorithm is used + bool MultiTracker::add(const Mat& image, const Rect2d& boundingBox){ + // quit if defaultAlgorithm has not been configured + if(defaultAlgorithm==""){ + printf("Default algorithm was not defined!\n"); + return false; + } + + // add a new tracked object + return add(defaultAlgorithm.c_str(), image, boundingBox); + }; + + // add a new tracked object bool MultiTracker::add( const String& trackerType, const Mat& image, const Rect2d& boundingBox ){ + // declare a new tracker Ptr newTracker = Tracker::create( trackerType ); + + // add the created tracker algorithm to the trackers list trackerList.push_back(newTracker); + + // add the ROI to the bounding box list objects.push_back(boundingBox); + // initialize the created tracker return trackerList.back()->init(image, boundingBox); }; - bool MultiTracker::update( const Mat& image, std::vector & boundingBox ){ + // add a set of objects to be tracked + bool MultiTracker::add(const String& trackerType, const Mat& image, std::vector boundingBox){ + // status of the tracker addition + bool stat; + + // add tracker for all input objects + for(unsigned i =0;i boundingBox){ + // quit if defaultAlgorithm has not been configured + if(defaultAlgorithm==""){ + printf("Default algorithm was not defined!\n"); + return false; + } + + return add(defaultAlgorithm.c_str(), image, boundingBox); + }; + + // update position of the tracked objects, the result is stored in internal storage + bool MultiTracker::update( const Mat& image){ for(unsigned i=0;i< trackerList.size(); i++){ trackerList[i]->update(image, objects[i]); } + return true; + }; + // update position of the tracked objects, the result is copied to external variable + bool MultiTracker::update( const Mat& image, std::vector & boundingBox ){ + update(image); boundingBox=objects; return true; }; + } /* namespace cv */