1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- /*
- Copyright 2018 Satya Mallick (LearnOpenCV.com)
- */
- #include <opencv2/opencv.hpp>
- #include <opencv2/tracking.hpp>
- using namespace cv;
- using namespace std;
- #define SSTR( x ) static_cast< std::ostringstream & >( \
- ( std::ostringstream() << std::dec << x ) ).str()
-
- int main(int argc, char **argv)
- {
- // Create tracker
- Ptr<Tracker> tracker = TrackerGOTURN::create();
- // Read video
- VideoCapture video("chaplin.mp4");
-
- // Exit if video is not opened
- if(!video.isOpened())
- {
- cout << "Could not read video file" << endl;
- return EXIT_FAILURE;
- }
-
- // Read first frame
- Mat frame;
- if (!video.read(frame))
- {
- cout << "Cannot read video file" << endl;
- return EXIT_FAILURE;
- }
-
- // Define initial boundibg box
- Rect2d bbox(287, 23, 86, 320);
-
- // Uncomment the line below to select a different bounding box
- //bbox = selectROI(frame, false);
-
- // Initialize tracker with first frame and bounding box
- tracker->init(frame, bbox);
-
- while(video.read(frame))
- {
- // Start timer
- double timer = (double)getTickCount();
-
- // Update the tracking result
- bool ok = tracker->update(frame, bbox);
-
- // Calculate Frames per second (FPS)
- float fps = getTickFrequency() / ((double)getTickCount() - timer);
-
- if (ok)
- {
- // Tracking success : Draw the tracked object
- rectangle(frame, bbox, Scalar( 255, 0, 0 ), 2, 1 );
- }
- else
- {
- // Tracking failure detected.
- putText(frame, "Tracking failure detected", Point(100,80), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(0,0,255),2);
- }
-
- // Display tracker type on frame
- putText(frame, "GOTURN Tracker", Point(100,20), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(50,170,50),2);
-
- // Display FPS on frame
- putText(frame, "FPS : " + SSTR(int(fps)), Point(100,50), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(50,170,50), 2);
-
- // Display frame.
- imshow("Tracking", frame);
-
- // Exit if ESC pressed.
- if(waitKey(1) == 27) break;
- }
- return EXIT_SUCCESS;
- }
|