-
Notifications
You must be signed in to change notification settings - Fork 14
/
MultiObjectTracking.cpp
68 lines (49 loc) · 1.77 KB
/
MultiObjectTracking.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
#include "opencv2\opencv.hpp"
#include "opencv2\highgui.hpp"
#include "opencv2\tracking.hpp"
int main() {
// Create video capturing object
// 0 opens default camera, otherwise filename as argument
cv::VideoCapture video("video.mp4");
// Check that video is opened
if (!video.isOpened()) return -1;
// For saving the frame
cv::Mat frame;
// Get video resolution
int frameWidth = video.get(cv::CAP_PROP_FRAME_WIDTH);
int frameHeigth = video.get(cv::CAP_PROP_FRAME_HEIGHT);
// Create video writer object
cv::VideoWriter output( "output.avi",
cv::VideoWriter::fourcc('M', 'J', 'P', 'G'),
30,
cv::Size(frameWidth, frameHeigth));
// Create multi tracker, select region-of-interests (ROIs) and initialize the trackers
video.read(frame);
cv::Ptr<cv::MultiTracker> multiTracker = cv::MultiTracker::create();
std::vector<cv::Rect> boundingBoxes;
cv::selectROIs("Video feed", frame, boundingBoxes, false);
if (boundingBoxes.size() < 1) return 0;
for (const auto& boundingBox : boundingBoxes) {
multiTracker->add(cv::TrackerKCF::create(), frame, boundingBox);
}
// Loop through available frames
while (video.read(frame)) {
// Update the multi tracker and draw rectangles around objects
multiTracker->update(frame);
for (const auto& object : multiTracker->getObjects()) {
cv::rectangle(frame, object, cv::Scalar(255, 0, 0), 2, 8);
}
// Display the frame
cv::imshow("Video feed", frame);
// Write video frame to output
output.write(frame);
// For breaking the loop
if (cv::waitKey(25) >= 0) break;
} // end while (video.read(frame))
// Release video capture and writer
output.release();
video.release();
// Destroy all windows
cv::destroyAllWindows();
return 0;
}