/
Trail.hpp
180 lines (148 loc) · 4.36 KB
/
Trail.hpp
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#ifndef TRAIL_HPP
#define TRAIL_HPP
#include <opencv2/opencv.hpp>
#include "Frame.hpp"
typedef std::list<Frame *> trail_t;
class Trail {
private:
virtual void iterate(cv::Mat& frame, void (*action)(cv::Mat&, Frame *, int, int)) = 0;
cv::Mat *background = NULL;
bool redraw_current_frame;
bool persistent;
void (*drawStrategy)(cv::Mat&, Frame *, int, int);
static void frameCopy(cv::Mat& destination, Frame *frame, int frame_index, int trail_size)
{
(void) frame_index;
(void) trail_size;
frame->copyTo(destination);
}
static void frameAccumulate(cv::Mat& destination, Frame *frame, int frame_index, int trail_size)
{
(void) frame_index;
(void) trail_size;
cv::accumulate(frame->getData(), destination, frame->getMask());
}
static void frameFadeCopy(cv::Mat& destination, Frame *frame, int frame_index, int trail_size)
{
double weight = ((double) frame_index) / trail_size;
cv::Mat weighted_frame;
frame->getData().convertTo(weighted_frame, destination.type(), weight);
weighted_frame.copyTo(destination, frame->getMask());
}
static void frameFadeAccumulate(cv::Mat& destination, Frame *frame, int frame_index, int trail_size)
{
double weight = ((double) frame_index) / trail_size;
cv::accumulateWeighted(frame->getData(), destination, weight, frame->getMask());
}
static void frameAverage(cv::Mat& destination, Frame *frame, int frame_index, int trail_size)
{
(void) frame_index;
double weight = 1. / trail_size;
cv::accumulateWeighted(frame->getData(), destination, weight, frame->getMask());
}
protected:
trail_t trail;
public:
Trail(int trail_length, cv::Size frame_size)
{
redraw_current_frame = false;
drawStrategy = frameCopy;
persistent = false;
if (trail_length < 0) {
persistent = true;
background = new cv::Mat(frame_size, CV_8UC3);
}
if (trail_length < 1)
trail_length = 1;
for (int i = 0; i < trail_length; i++)
trail.push_back(new Frame(frame_size));
}
virtual ~Trail()
{
trail_t::iterator trail_it;
for (trail_it = trail.begin(); trail_it != trail.end(); trail_it++) {
delete(*trail_it);
}
trail.clear();
delete background;
}
void setBackground(cv::Mat trail_background)
{
delete background;
background = new cv::Mat(trail_background);
}
int setDrawingMethod(std::string drawing_method)
{
if (drawing_method == "copy") {
drawStrategy = frameCopy;
} else if (drawing_method == "accumulate") {
drawStrategy = frameAccumulate;
} else if (drawing_method == "fadecopy") {
drawStrategy = frameFadeCopy;
} else if (drawing_method == "fadeaccumulate") {
drawStrategy = frameFadeAccumulate;
} else if (drawing_method == "average") {
drawStrategy = frameAverage;
} else {
return -1;
}
return 0;
}
void setRedrawCurrentFrame(bool do_redraw_current_frame)
{
redraw_current_frame = do_redraw_current_frame;
}
void update(Frame *frame)
{
Frame *front = trail.front();
trail.pop_front();
delete front;
trail.push_back(frame);
}
void draw(cv::Mat& destination)
{
cv::Mat float_destination;
if (background)
background->convertTo(float_destination, CV_32FC3);
else
destination.convertTo(float_destination, CV_32FC3);
iterate(float_destination, drawStrategy);
float_destination.convertTo(destination, destination.type());
if (redraw_current_frame) {
Frame *current_frame = trail.back();
current_frame->getData().copyTo(destination, current_frame->getMask());
}
/* remember the current trail in the background image */
if (persistent)
float_destination.convertTo(*background, background->type());
}
};
class ForwardTrail: public Trail {
void iterate(cv::Mat& destination, void (*action)(cv::Mat&, Frame *, int, int))
{
int index = 0;
trail_t::iterator trail_it;
for (trail_it = trail.begin(); trail_it != trail.end(); trail_it++) {
action(destination, *trail_it, ++index, trail.size());
}
}
public:
ForwardTrail(int trail_lenght, cv::Size frame_size) : Trail(trail_lenght, frame_size)
{
}
};
class ReverseTrail: public Trail {
void iterate(cv::Mat& destination, void (*action)(cv::Mat&, Frame *, int, int))
{
int index = 0;
trail_t::reverse_iterator trail_it;
for (trail_it = trail.rbegin(); trail_it != trail.rend(); trail_it++) {
action(destination, *trail_it, ++index, trail.size());
}
}
public:
ReverseTrail(int trail_lenght, cv::Size frame_size) : Trail(trail_lenght, frame_size)
{
}
};
#endif // TRAIL_HPP