forked from Smorodov/Multitarget-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseDetector.h
More file actions
153 lines (132 loc) · 3.88 KB
/
BaseDetector.h
File metadata and controls
153 lines (132 loc) · 3.88 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#pragma once
#include <memory>
#include "defines.h"
///
/// \brief The BaseDetector class
///
class BaseDetector
{
public:
///
/// \brief BaseDetector
/// \param collectPoints
/// \param frame
///
BaseDetector(bool collectPoints, cv::UMat& frame)
: m_collectPoints(collectPoints)
{
m_minObjectSize.width = std::max(5, frame.cols / 100);
m_minObjectSize.height = m_minObjectSize.width;
}
///
/// \brief ~BaseDetector
///
virtual ~BaseDetector(void)
{
}
///
/// \brief Init
/// \param config
///
virtual bool Init(const config_t& config) = 0;
///
/// \brief Detect
/// \param frame
///
virtual void Detect(cv::UMat& frame) = 0;
///
/// \brief SetMinObjectSize
/// \param minObjectSize
///
void SetMinObjectSize(cv::Size minObjectSize)
{
m_minObjectSize = minObjectSize;
}
///
/// \brief GetDetects
/// \return
///
const regions_t& GetDetects() const
{
return m_regions;
}
///
/// \brief CollectPoints
/// \param region
///
virtual void CollectPoints(CRegion& region)
{
const int yStep = 5;
const int xStep = 5;
for (int y = region.m_rect.y, yStop = region.m_rect.y + region.m_rect.height; y < yStop; y += yStep)
{
for (int x = region.m_rect.x, xStop = region.m_rect.x + region.m_rect.width; x < xStop; x += xStep)
{
if (region.m_rect.contains(cv::Point(x, y)))
{
region.m_points.push_back(cv::Point2f(static_cast<float>(x), static_cast<float>(y)));
}
}
}
if (region.m_points.empty())
{
region.m_points.push_back(cv::Point2f(region.m_rect.x + 0.5f * region.m_rect.width, region.m_rect.y + 0.5f * region.m_rect.height));
}
}
///
/// \brief CalcMotionMap
/// \param frame
///
virtual void CalcMotionMap(cv::Mat frame)
{
if (m_motionMap.size() != frame.size())
{
m_motionMap = cv::Mat(frame.size(), CV_32FC1, cv::Scalar(0, 0, 0));
}
cv::Mat foreground(m_motionMap.size(), CV_8UC1, cv::Scalar(0, 0, 0));
for (const auto& region : m_regions)
{
#if (CV_VERSION_MAJOR < 4)
cv::ellipse(foreground,
cv::RotatedRect((region.m_rect.tl() + region.m_rect.br()) / 2, region.m_rect.size(), 0),
cv::Scalar(255, 255, 255), CV_FILLED);
#else
cv::ellipse(foreground,
cv::RotatedRect((region.m_rect.tl() + region.m_rect.br()) / 2, region.m_rect.size(), 0),
cv::Scalar(255, 255, 255), cv::FILLED);
#endif
}
cv::Mat normFor;
cv::normalize(foreground, normFor, 255, 0, cv::NORM_MINMAX, m_motionMap.type());
double alpha = 0.95;
cv::addWeighted(m_motionMap, alpha, normFor, 1 - alpha, 0, m_motionMap);
const int chans = frame.channels();
for (int y = 0; y < frame.rows; ++y)
{
uchar* imgPtr = frame.ptr(y);
float* moPtr = reinterpret_cast<float*>(m_motionMap.ptr(y));
for (int x = 0; x < frame.cols; ++x)
{
for (int ci = chans - 1; ci < chans; ++ci)
{
imgPtr[ci] = cv::saturate_cast<uchar>(imgPtr[ci] + moPtr[0]);
}
imgPtr += chans;
++moPtr;
}
}
}
protected:
regions_t m_regions;
cv::Size m_minObjectSize;
bool m_collectPoints;
cv::Mat m_motionMap;
};
///
/// \brief CreateDetector
/// \param detectorType
/// \param collectPoints
/// \param gray
/// \return
///
BaseDetector* CreateDetector(tracking::Detectors detectorType, const config_t& config, bool collectPoints, cv::UMat& gray);