-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmovingaverage.cpp
More file actions
56 lines (49 loc) · 1.32 KB
/
Copy pathmovingaverage.cpp
File metadata and controls
56 lines (49 loc) · 1.32 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
#include "movingaverage.h"
void MovingAverage::add(std::vector<cv::Mat> &x, std::vector<cv::Mat> y){
if(x.empty()){
for(int i=0;i<y.size();i++){
cv::Mat tmp;
y[i].convertTo(tmp,CV_32F);
x.push_back(tmp);
}
}
else{
for(int i=0;i<y.size();i++) cv::add(x[i],y[i],x[i],cv::Mat(),CV_32F);
}
}
void MovingAverage::subtract(std::vector<cv::Mat> &x, std::vector<cv::Mat> y){
for(int i=0;i<y.size();i++) cv::subtract(x[i],y[i],x[i],cv::Mat(),CV_32F);
}
MovingAverage::MovingAverage(int n) : max_size(n), tmp_count(0){
}
bool MovingAverage::add(std::vector<cv::Mat> x, int tag) {
// todo: check shape
while (buf.size() >= max_size) {
subtract(sum, buf.front());
buf.pop_front();
}
add(sum,x);
buf.push_back(x);
last_tag_ = tag;
//todo tmp_sum
return true;
}
std::vector<cv::Mat> MovingAverage::mean() {
std::vector<cv::Mat> res;
for(int i=0;i<sum.size();i++){
res.push_back(sum[i]/buf.size());
}
return res;
}
int MovingAverage::last_tag(){ return last_tag_;}
void MovingAverage::resize(int n) {
max_size = n;
while (buf.size() > max_size) {
subtract(sum, buf.front());
buf.pop_front();
}
if(tmp_count >= max_size){
tmp_count=0;
tmp_sum.clear();
}
}