-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMotionEstimator.cxx
176 lines (146 loc) · 4.11 KB
/
MotionEstimator.cxx
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
#include <cmath>
#include <limits>
#include "MotionEstimator.hxx"
#include <opencv2/imgproc.hpp>
using namespace cv;
using namespace std;
/**
* Enable linear correction of brightness to handle "flicker" effect
* due auto-exposure correction on some cameras
*/
#define LINEAR_CORRECTION
ISentry::MotionEstimator::MotionEstimator(const libconfig::Setting& cfg)
{
detection_window_size = (int)cfg["detection_window_size"];
internal_frame_width = cfg["internal_frame_width"];
smoothing_window_size = (int)cfg["smoothing_window_size"];
}
void ISentry::MotionEstimator::reset()
{
buf.clear();
}
void ISentry::MotionEstimator::addFrame(std::pair<cv::Mat,time_t> &tframe)
{
if(!running)
return;
Mat &frame=tframe.first;
Mat bw;
cv::cvtColor(frame, bw, COLOR_BGR2GRAY);
float scaler = bw.cols/internal_frame_width;
Size newSize(bw.cols/scaler, bw.rows/scaler);
Mat smallf;
cv::resize(bw, small, newSize, 0, 0, INTER_LINEAR);
small.convertTo(smallf,DataType<float>::type);
if(buf.empty())
buf.push_back(smallf);
#ifdef LINEAR_CORRECTION
int smalllen = newSize.width*newSize.height;
Mat smallv = smallf.reshape(1, smalllen);
int n=0;
Mat small1 = Mat::ones(smalllen,1, DataType<float>::type);
#endif
Mat avg(newSize, DataType<float>::type, (float)0);
for(std::deque<Mat>::const_iterator i=buf.begin(); i != buf.end(); i++)
{
#ifdef LINEAR_CORRECTION
const Mat &fm = (*i);
Mat fv = fm.reshape(1, smalllen);
Mat fm1;
hconcat(fv, small1, fm1);
Mat fit;
if(!solve(fm1, smallv, fit, DECOMP_SVD))
{
//std::cerr << "SVD error\n";
// Add to average w/o linear transform
add(avg, fm/buf.size(), (Mat&)avg);
} else
{
float k=fit.at<float>(0);
float b=fit.at<float>(1);
//if(n==0)
// std::cerr << "SVD OK " << k << ", " << b <<"\n";
Mat ft;
// linear transofrm
fm.convertTo(ft,-1,k,b);
// Add to average
add(avg, ft/buf.size(), (Mat&)avg);
}
n++;
#else
add(avg, (*i)/buf.size(), (Mat&)avg);
#endif
}
saveFrame(smallf);
diff = avg-smallf;
Scalar m = mean(smallf);
if(!isnan(m[0]))
{
double a = smallf.rows*smallf.cols;
Scalar mse = sum(diff.mul(diff))/a;
float rm = sqrt(mse[0])/m[0];
saveRawMotion(rm);
} else
{
saveRawMotion(std::numeric_limits<float>::quiet_NaN());
}
}
void ISentry::MotionEstimator::saveFrame(cv::Mat &f)
{
while(buf.size()>=detection_window_size)
buf.pop_front();
buf.push_back(f);
}
void ISentry::MotionEstimator::saveRawMotion(float v)
{
while(raw_motion.size()>=smoothing_window_size)
raw_motion.pop_back();
raw_motion.insert(raw_motion.begin(),v);
}
float ISentry::MotionEstimator::getLastRawMotion() const
{
return raw_motion.front();
}
float ISentry::MotionEstimator::getLastSmoothedMotion() const
{
int n = 0;
float x,sumxx,sumyy,sumxy,sumx,sumy;
x=sumxx=sumyy=sumxy=sumx=sumy=0;
for(vector<float>::const_iterator i = raw_motion.begin();i!=raw_motion.end();i++)
{
float y=*i;
x = x + 1;
if(isnan(y))
continue;
sumx = sumx + x;
sumy = sumy + y;
sumxx = sumxx + x * x;
sumyy = sumyy + y * y;
sumxy = sumxy + x * y;
n = n + 1;
}
if(n < 2)
{
//non enough data to smooth. return 0
return 0;
}
float sxx = sumxx - sumx * sumx / n;
float sxy = sumxy - sumx * sumy / n;
float b = sxy / sxx;
float a = (sumy - b * sumx) / n;
float v = a + b * x;
return v>0?v:0;
}
std::vector<float> ISentry::MotionEstimator::getSignals()
{
vector<float> res;
res.push_back(getLastRawMotion());
res.push_back(getLastSmoothedMotion());
return res;
}
std::vector<cv::Mat> ISentry::MotionEstimator::getFrames()
{
vector<cv::Mat> res;
res.push_back(getLastScaledFrame());
res.push_back(getLastDiff());
return res;
}