4,242
社区成员
发帖
与我相关
我的任务
分享
#include <opencv2/core/utility.hpp>
#include <opencv2/tracking.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <iostream>
#include <cstring>
using namespace std;
using namespace cv;
int main(int argc, char** argv) {
// declares all required variables
Rect2d roi;
Mat frame;
// create a tracker object
//Ptr<Tracker> tracker = Tracker::create("KCF"); // OPENCV 3.2.0及其以下的语法
Ptr<Tracker> tracker = Tracker::create("MIL"); // LOSE TRACKING EASILY
//Ptr<Tracker> tracker = Tracker::create("Boosting"); //CANNOT RUN NORMALLY
//Ptr<Tracker> tracker = Tracker::create("MedianFlow"); //cannot run normally
//Ptr<Tracker> tracker = Tracker::create("GOTURN");
//Ptr<Tracker> tracker = Tracker::create("TLD");
// set input video
VideoCapture cap("G:\\BasketBall.mp4");
//VideoCapture cap(0);
if(!cap.isOpened()){
cout << "Could not read video file" << endl;
return 1;
}
else cout << "Video file reading..." << endl;
// get bounding box
cap >> frame; // to read the next frame
roi = selectROI("tracker", frame, true, false);
// quit if ROI was not selected
if (roi.width == 0 || roi.height == 0)
return 0;
// Initialize the tracker with a know bounding box that surrounding the target.
tracker->init(frame, roi);
// perform the tracking process
cout << "Start the tracking process, press ESC to quit." << endl;
for (;; ) {
// get frame from the video
cap >> frame;
// stop the program if no more images
if (frame.rows == 0 || frame.cols == 0)
break;
// Update the tracker, find the new most likely bounding box for the target.
tracker->update(frame, roi);
// draw the tracked object
rectangle(frame, roi, Scalar(255, 0, 0), 2, 1);
//Scalar color(R,G,B) thickness=2,lineType=1,(default)shift=0
// show image with the tracked object
imshow("tracker", frame);
//quit on ESC button
if (waitKey(1) == 27) break;
}
return 0;
}