OpnCV MultiTracker used to track multiple object using existing single tracking algorithm like CSRT, MIL, KCF, TLD, GOTURN, MEDIANFLOW, MOSSE and BOOSTING
For learn about Single tracking algorithm see the blog post OpenCV Object Tracking.
In this blog post I am using TrackerCSRT object and supply to MultiTracker class which will track multiple object on video frame. The object need to track will select manually by user using mouse. This is done by the function selectROIs where user can select multiple object by dragging the mouse and then press Enter key to select next object, finally press Esc key for start tracking.
Below code will show how it can achieve using OpenCV C++ interface.
For learn about Single tracking algorithm see the blog post OpenCV Object Tracking.
In this blog post I am using TrackerCSRT object and supply to MultiTracker class which will track multiple object on video frame. The object need to track will select manually by user using mouse. This is done by the function selectROIs where user can select multiple object by dragging the mouse and then press Enter key to select next object, finally press Esc key for start tracking.
Below code will show how it can achieve using OpenCV C++ interface.
C++ Code
// create the MultiTracker
MultiTracker trackers;
// container for tracking objects
vector objects;
std::string video = "track.mp4";
VideoCapture cap(video);
Mat frame;
cap >> frame;
vector ROIs;
//Select object to totrack
selectROIs("tracker",frame,ROIs);
//quit when the tracked object(s) is not selected
if(ROIs.size()<1)
return ;
// initialise the tracker
std::vector < Ptr > algorithms;
for (size_t i = 0; i < ROIs.size(); i++)
{
Ptr tracker = TrackerCSRT::create();
algorithms.push_back(tracker);
objects.push_back(ROIs[i]);
}
trackers.add(algorithms,frame,objects);
// the tracking action
printf("Start the tracking process, press ESC to quit.\n");
for ( ;; ){
cap >> frame;
// quit the program if no more frame
if(frame.rows==0 || frame.cols==0)
break;
//update the tracking result
trackers.update(frame);
// draw the tracked object
for(unsigned i=0;i < trackers.getObjects().size();i++)
rectangle( frame, trackers.getObjects()[i], Scalar( 0, 255, 0 ), 2, 1 );
// show image with the tracked object
imshow("tracker",frame);
//quit on ESC key
if(waitKey(1)==27)break;
}
No comments:
Post a Comment