forked from Smorodov/Multitarget-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MouseExample.h
117 lines (94 loc) · 3.67 KB
/
MouseExample.h
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#pragma once
#include "opencv2/opencv.hpp"
#include <opencv2/highgui/highgui_c.h>
#include "Ctracker.h"
#include <iostream>
#include <vector>
//------------------------------------------------------------------------
// Mouse callbacks
//------------------------------------------------------------------------
void mv_MouseCallback(int event, int x, int y, int /*flags*/, void* param)
{
if (event == cv::EVENT_MOUSEMOVE)
{
cv::Point2f* p = (cv::Point2f*)param;
if (p)
{
p->x = static_cast<float>(x);
p->y = static_cast<float>(y);
}
}
}
// ----------------------------------------------------------------------
void MouseTracking(cv::CommandLineParser parser)
{
std::string outFile = parser.get<std::string>("out");
cv::VideoWriter writer;
int k = 0;
std::vector<cv::Scalar> colors = { cv::Scalar(255, 0, 0), cv::Scalar(0, 255, 0), cv::Scalar(0, 0, 255), cv::Scalar(255, 255, 0), cv::Scalar(0, 255, 255), cv::Scalar(255, 0, 255), cv::Scalar(255, 127, 255), cv::Scalar(127, 0, 255), cv::Scalar(127, 0, 127) };
cv::namedWindow("Video");
cv::Mat frame = cv::Mat(800, 800, CV_8UC3);
if (!writer.isOpened())
{
writer.open(outFile, cv::VideoWriter::fourcc('P', 'I', 'M', '1'), 20, frame.size(), true);
}
// Set mouse callback
cv::Point2f pointXY;
cv::setMouseCallback("Video", mv_MouseCallback, (void*)&pointXY);
bool useLocalTracking = false;
CTracker tracker(useLocalTracking,
CTracker::DistCenters,
CTracker::KalmanLinear,
CTracker::FilterCenter,
CTracker::TrackNone,
CTracker::MatchHungrian,
0.2f,
0.5f,
100.0f,
25,
25);
track_t alpha = 0;
cv::RNG rng;
while (k != 27)
{
frame = cv::Scalar::all(0);
// Noise addition (measurements/detections simulation )
float Xmeasured = pointXY.x + static_cast<float>(rng.gaussian(2.0));
float Ymeasured = pointXY.y + static_cast<float>(rng.gaussian(2.0));
// Append circulating around mouse cv::Points (frequently intersecting)
std::vector<Point_t> pts;
pts.push_back(Point_t(Xmeasured + 100.0f*sin(-alpha), Ymeasured + 100.0f*cos(-alpha)));
pts.push_back(Point_t(Xmeasured + 100.0f*sin(alpha), Ymeasured + 100.0f*cos(alpha)));
pts.push_back(Point_t(Xmeasured + 100.0f*sin(alpha / 2.0f), Ymeasured + 100.0f*cos(alpha / 2.0f)));
pts.push_back(Point_t(Xmeasured + 100.0f*sin(alpha / 3.0f), Ymeasured + 100.0f*cos(alpha / 1.0f)));
alpha += 0.05f;
regions_t regions;
for (auto p : pts)
{
regions.push_back(CRegion(cv::Rect(static_cast<int>(p.x - 1), static_cast<int>(p.y - 1), 3, 3)));
}
for (size_t i = 0; i < pts.size(); i++)
{
cv::circle(frame, pts[i], 3, cv::Scalar(0, 255, 0), 1, CV_AA);
}
tracker.Update(pts, regions, cv::Mat());
std::cout << tracker.tracks.size() << std::endl;
for (size_t i = 0; i < tracker.tracks.size(); i++)
{
const auto& track = tracker.tracks[i];
if (track->m_trace.size() > 1)
{
for (size_t j = 0; j < track->m_trace.size() - 1; j++)
{
cv::line(frame, track->m_trace[j], track->m_trace[j + 1], colors[i % colors.size()], 2, CV_AA);
}
}
}
cv::imshow("Video", frame);
if (writer.isOpened())
{
writer << frame;
}
k = cv::waitKey(10);
}
}