 |
OpenCV
4.5.1
Open Source Computer Vision
|
Goal
In this tutorial you will learn how to
- Create a MultiTracker object.
- Track several objects at once using the MultiTracker object.
Source Code
17 #include "samples_utility.hpp"
22 int main(
int argc,
char** argv ){
26 " Usage: example_tracking_multitracker <video_name> [algorithm]\n"
28 " example_tracking_multitracker Bolt/img/%04d.jpg\n"
29 " example_tracking_multitracker faceocc2.webm MEDIANFLOW\n"
35 std::string trackingAlg =
"KCF";
39 trackingAlg = argv[2];
48 vector<Rect2d> objects;
52 std::string video = argv[1];
70 std::vector<Ptr<legacy::Tracker> > algorithms;
71 for (
size_t i = 0; i < ROIs.size(); i++)
73 algorithms.push_back(createTrackerByName_legacy(trackingAlg));
74 objects.push_back(ROIs[i]);
77 trackers.
add(algorithms,frame,objects);
81 printf(
"Start the tracking process, press ESC to quit.\n");
87 if(frame.rows==0 || frame.cols==0)
Explanation
Create the MultiTracker object
legacy::MultiTracker trackers;
You can create the MultiTracker object and use the same tracking algorithm for all tracked object as shown in the snippet. If you want to use different type of tracking algorithm for each tracked object, you should define the tracking algorithm whenever a new object is added to the MultiTracker object.
Selection of multiple objects
You can use selectROI to select multiple objects with the result stored in a vector of cv::Rect2d as shown in the code.
Adding the tracked object to MultiTracker
std::vector<Ptr<legacy::Tracker> > algorithms;
for (size_t i = 0; i < ROIs.size(); i++)
{
algorithms.push_back(createTrackerByName_legacy(trackingAlg));
objects.push_back(ROIs[i]);
}
trackers.add(algorithms,frame,objects);
You can add all tracked objects at once to the MultiTracker as shown in the code. In this case, all objects will be tracked using same tracking algorithm as specified in decaration of MultiTracker object. If you want to use different tracker algorithms for each tracked object, You should add the tracked objects one by one and specify their tracking algorithm using the variant of cv::legacy::MultiTracker::add.
- See also
- cv::legacy::MultiTracker::add( const String& trackerType, const Mat& image, const Rect2d& boundingBox )
Obtaining the result
for(
unsigned i=0;i<trackers.getObjects().
size();i++)
You can access the result from the public variable cv::legacy::MultiTracker::objects provided by the MultiTracker class as shown in the code.
void selectROIs(const String &windowName, InputArray img, std::vector< Rect > &boundingBoxes, bool showCrosshair=true, bool fromCenter=false)
Allows users to select multiple ROIs on the given image.
Class for video capturing from video files, image sequences or cameras.
Definition: videoio.hpp:628
int waitKey(int delay=0)
Waits for a pressed key.
bool add(Ptr< cv::legacy::Tracker > newTracker, InputArray image, const Rect2d &boundingBox)
Add a new object to be tracked.
void rectangle(InputOutputArray img, Point pt1, Point pt2, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)
Draws a simple, thick, or filled up-right rectangle.
GOpaque< Size > size(const GMat &src)
Gets dimensions from Mat.
const std::vector< Rect2d > & getObjects() const
Returns a reference to a storage for the tracked objects, each object corresponds to one tracker algo...
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
Scalar_< double > Scalar
Definition: types.hpp:669
n-dimensional dense array class
Definition: mat.hpp:798
"black box" representation of the file storage associated with a file on disk.
Definition: affine.hpp:52
This class is used to track multiple objects using the specified tracker algorithm.
Definition: tracking_legacy.hpp:352
bool update(InputArray image)
Update the current tracking status. The result will be saved in the internal storage.