-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfoneaccumulator.cpp
More file actions
127 lines (109 loc) · 3.25 KB
/
foneaccumulator.cpp
File metadata and controls
127 lines (109 loc) · 3.25 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
#include "foneaccumulator.h"
#include <iostream>
int FoneAccumulator::dispThreshold;
int FoneAccumulator::maxN;
int FoneAccumulator::forceLearnDuration = 60;
FoneAccumulator::FoneAccumulator(size_t width, size_t height)
{
dispThreshold = 20;
maxN = 100;
trackedPixelsThreshold = 0.5F;
meanAccumulator = new cv::Mat(height, width, CV_32F);
dispAccumulator = new cv::Mat(height, width, CV_32F);
n = new cv::Mat(height, width, CV_8UC1);
tracked = new cv::Mat(height, width, CV_8UC1);
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++)
{
meanAccumulator->at<uchar>(y, x) = 0;
dispAccumulator->at<uchar>(y, x) = 0;
n->at<uchar>(y, x) = 0;
tracked->at<uchar>(y, x) = 0;
}
this->width = width;
this->height = height;
forceFoneAccumulating = false;
forceLearnFrameCounter = 0;
}
void FoneAccumulator::accumulate(cv::Mat *nextFrame)
{
int foregroundPixelsCount = 0;
// each pixel
for (int y = 0; y < height; ++y)
{
for (int x = 0; x < width; ++x)
{
uint accumulatedMean = meanAccumulator->at<uint>(y, x);
uint accumulatedDisp = dispAccumulator->at<uint>(y, x);
uchar N = n->at<uchar>(y, x);
uint mean = 0;
uint disp = 0;
if (N!=0)
{
mean = accumulatedMean / N;
disp = accumulatedDisp / N;
}
uchar addition = nextFrame->at<uchar>(y, x);
uchar diff = abs(mean - addition);
// first condition is for cases when we haven't collected much data yet and disp is too small
// second defines, if variance `diff` can be explained with fone picture noise
// third is for the case of the first frame, when we don't have any data and have to collect it
// fourth is for ones who would like to accumulate fone with no conditions
if (diff < dispThreshold || diff*diff < disp || N == 0 || forceFoneAccumulating)
{
tracked->at<uchar>(y, x) = 0;
uint newMeanValue = accumulatedMean + addition;
uint newDispValue = accumulatedDisp + diff*diff;
if (N == maxN)
{
// normalizing accumulators
newMeanValue = (uint) ((float)(newMeanValue) / (N + 1) * N);
newDispValue = (uint) ((float)(newDispValue) / (N + 1) * N); // TODO: is this a correct normalization?
}
else
{
n->at<uchar>(y, x) = N + 1;
}
meanAccumulator->at<uint>(y, x) = newMeanValue;
dispAccumulator->at<uint>(y, x) = newDispValue;
}
else // pixel tends not to belong the background
{
++foregroundPixelsCount;
tracked->at<uchar>(y, x) = 255;
}
}
}
if (foregroundPixelsCount > nextFrame->size().area() * trackedPixelsThreshold)
{
enableForceAccumulating();
}
if (forceFoneAccumulating)
{
++forceLearnFrameCounter;
if (forceLearnFrameCounter >= forceLearnDuration)
{
disableForceAccumulating();
}
}
}
void FoneAccumulator::getForegroundMask(cv::Mat& thresholded)
{
threshold(*dispAccumulator, thresholded, dispThreshold, (uchar) 255, 0);
}
void FoneAccumulator::enableForceAccumulating()
{
forceFoneAccumulating = true;
forceLearnFrameCounter = 0;
}
void FoneAccumulator::disableForceAccumulating()
{
forceFoneAccumulating = false;
}
FoneAccumulator::~FoneAccumulator()
{
delete meanAccumulator;
delete dispAccumulator;
delete n;
delete tracked;
}