-
-
Notifications
You must be signed in to change notification settings - Fork 308
/
tracking.cpp
76 lines (67 loc) · 1.95 KB
/
tracking.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <opencv2/core/utility.hpp>
#include <opencv2/tracking.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main( int argc, char** argv ){
//help
if(argc<2){
cout <<"Your command must contain the arguments mentioned below:"
<< endl
<< "./filename path/video_name"
<< endl
<< "Example: ./track video/abc.avi"
<< endl;
return 0;
}
//variable declaration
//ROI: Region Of Interest
Rect2d roi;
Mat frame;
// Creating object of KCF tracker
Ptr<Tracker> tracker = Tracker::create( "KCF" );
// input video
string video = argv[1];
VideoCapture cap(video);
//getting a frame out of video
cap >> frame;
// selecting a bounding box which is a rectangle
bool showCrosshair = false;
bool fromCenter = false;
roi = selectROI("tracker", frame, fromCenter, showCrosshair);
cout << roi << endl;
cout << roi.x << " " << roi.y << endl;
//quit if ROI was not selected
if(roi.width==0 || roi.height==0)
return 0;
// initialize the tracker
tracker->init(frame,roi);
// perform the tracking process
printf("Start the tracking process, press ESC to quit.\n");
for ( ;; ){
// get frame from the video
cap >> frame;
// stop if no more frames
if(frame.rows==0 || frame.cols==0)
break;
//continue tracking only if ROI is in the scope of video
//stop if ROI goes out of range of image
if (roi.x>=1 && roi.y>=1 && (roi.x+roi.width) < frame.cols && (roi.y+roi.height) <frame.rows)
{
// update the tracking result
tracker->update(frame,roi);
// draw rectangle around the tracked object
rectangle( frame, roi, Scalar( 255, 0, 0 ), 2, 1 );
// show image with the tracked object
imshow("tracker",frame);
}
else{
imshow("tracker",frame);
}
//quit on ESC button
if(waitKey(1)==27)break;
}
return 0;
}