Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hackassin
GitHub Repository: hackassin/learnopencv
Path: blob/master/GOTURN/goturnTracker.cpp
3118 views
1
/*
2
Copyright 2018 Satya Mallick (LearnOpenCV.com)
3
*/
4
5
#include <opencv2/opencv.hpp>
6
#include <opencv2/tracking.hpp>
7
8
using namespace cv;
9
using namespace std;
10
11
#define SSTR( x ) static_cast< std::ostringstream & >( \
12
( std::ostringstream() << std::dec << x ) ).str()
13
14
int main(int argc, char **argv)
15
{
16
// Create tracker
17
Ptr<Tracker> tracker = TrackerGOTURN::create();
18
19
// Read video
20
VideoCapture video("chaplin.mp4");
21
22
// Exit if video is not opened
23
if(!video.isOpened())
24
{
25
cout << "Could not read video file" << endl;
26
return EXIT_FAILURE;
27
}
28
29
// Read first frame
30
Mat frame;
31
if (!video.read(frame))
32
{
33
cout << "Cannot read video file" << endl;
34
return EXIT_FAILURE;
35
}
36
37
// Define initial boundibg box
38
Rect2d bbox(287, 23, 86, 320);
39
40
// Uncomment the line below to select a different bounding box
41
//bbox = selectROI(frame, false);
42
43
// Initialize tracker with first frame and bounding box
44
tracker->init(frame, bbox);
45
46
while(video.read(frame))
47
{
48
// Start timer
49
double timer = (double)getTickCount();
50
51
// Update the tracking result
52
bool ok = tracker->update(frame, bbox);
53
54
// Calculate Frames per second (FPS)
55
float fps = getTickFrequency() / ((double)getTickCount() - timer);
56
57
if (ok)
58
{
59
// Tracking success : Draw the tracked object
60
rectangle(frame, bbox, Scalar( 255, 0, 0 ), 2, 1 );
61
}
62
else
63
{
64
// Tracking failure detected.
65
putText(frame, "Tracking failure detected", Point(100,80), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(0,0,255),2);
66
}
67
68
// Display tracker type on frame
69
putText(frame, "GOTURN Tracker", Point(100,20), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(50,170,50),2);
70
71
// Display FPS on frame
72
putText(frame, "FPS : " + SSTR(int(fps)), Point(100,50), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(50,170,50), 2);
73
74
// Display frame.
75
imshow("Tracking", frame);
76
77
// Exit if ESC pressed.
78
if(waitKey(1) == 27) break;
79
}
80
81
return EXIT_SUCCESS;
82
}
83
84