-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathGrimsonGMM.cpp
333 lines (283 loc) · 9.31 KB
/
GrimsonGMM.cpp
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/****************************************************************************
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
******************************************************************************/
/****************************************************************************
*
* GrimsonGMM.cpp
*
* Purpose: Implementation of the Gaussian mixture model (GMM) background
* subtraction described in:
* "Adaptive background mixture models for real-time tracking"
* by Chris Stauffer and W.E.L Grimson
*
* Author: Donovan Parks, September 2007
*
* This code is based on code by Z. Zivkovic's written for his enhanced GMM
* background subtraction algorithm:
*
* "Improved adaptive Gausian mixture model for background subtraction"
* Z.Zivkovic
* International Conference Pattern Recognition, UK, August, 2004
*
*
* "Efficient Adaptive Density Estimapion per Image Pixel for the
* Task of Background Subtraction"
* Z.Zivkovic, F. van der Heijden
* Pattern Recognition Letters, vol. 27, no. 7, pages 773-780, 2006.
*
* Zivkovic's code can be obtained at: www.zoranz.net
******************************************************************************/
#include "GrimsonGMM.hpp"
using namespace Algorithms::BackgroundSubtraction;
int compareGMM(const void* _gmm1, const void* _gmm2)
{
GMM gmm1 = *(GMM*)_gmm1;
GMM gmm2 = *(GMM*)_gmm2;
if(gmm1.significants < gmm2.significants)
return 1;
else if(gmm1.significants == gmm2.significants)
return 0;
else
return -1;
}
GrimsonGMM::GrimsonGMM()
{
m_modes = NULL;
}
GrimsonGMM::~GrimsonGMM()
{
if(m_modes != NULL)
delete[] m_modes;
}
void GrimsonGMM::Initalize(const BgsParams& param)
{
m_params = (GrimsonParams&)param;
// Tbf - the threshold
m_bg_threshold = 0.75f; // 1-cf from the paper
// Tgenerate - the threshold
m_variance = 36.0f; // sigma for the new mode
// GMM for each pixel
m_modes = new GMM[m_params.Size()*m_params.MaxModes()];
// used modes per pixel
m_modes_per_pixel = cvCreateImage(cvSize(m_params.Width(), m_params.Height()), IPL_DEPTH_8U, 1);
m_background = cvCreateImage(cvSize(m_params.Width(), m_params.Height()), IPL_DEPTH_8U, 3);
}
RgbImage* GrimsonGMM::Background()
{
return &m_background;
}
void GrimsonGMM::InitModel(const RgbImage& data)
{
m_modes_per_pixel.Clear();
for(unsigned int i = 0; i < m_params.Size()*m_params.MaxModes(); ++i)
{
m_modes[i].weight = 0;
m_modes[i].variance = 0;
m_modes[i].muR = 0;
m_modes[i].muG = 0;
m_modes[i].muB = 0;
m_modes[i].significants = 0;
}
}
void GrimsonGMM::Update(int frame_num, const RgbImage& data, const BwImage& update_mask)
{
// it doesn't make sense to have conditional updates in the GMM framework
}
void GrimsonGMM::SubtractPixel(long posPixel, const RgbPixel& pixel, unsigned char& numModes,
unsigned char& low_threshold, unsigned char& high_threshold)
{
// calculate distances to the modes (+ sort???)
// here we need to go in descending order!!!
long pos;
bool bFitsPDF=false;
bool bBackgroundLow=false;
bool bBackgroundHigh=false;
float fOneMinAlpha = 1-m_params.Alpha();
float totalWeight = 0.0f;
// calculate number of Gaussians to include in the background model
int backgroundGaussians = 0;
double sum = 0.0;
for(int i = 0; i < numModes; ++i)
{
if(sum < m_bg_threshold)
{
backgroundGaussians++;
sum += m_modes[posPixel+i].weight;
}
else
{
break;
}
}
// update all distributions and check for match with current pixel
for (int iModes=0; iModes < numModes; iModes++)
{
pos=posPixel+iModes;
float weight = m_modes[pos].weight;
// fit not found yet
if (!bFitsPDF)
{
//check if it belongs to some of the modes
//calculate distance
float var = m_modes[pos].variance;
float muR = m_modes[pos].muR;
float muG = m_modes[pos].muG;
float muB = m_modes[pos].muB;
float dR=muR - pixel(0);
float dG=muG - pixel(1);
float dB=muB - pixel(2);
// calculate the squared distance
float dist = (dR*dR + dG*dG + dB*dB);
if(dist < m_params.HighThreshold()*var && iModes < backgroundGaussians)
bBackgroundHigh = true;
// a match occurs when the pixel is within sqrt(fTg) standard deviations of the distribution
if(dist < m_params.LowThreshold()*var)
{
bFitsPDF=true;
// check if this Gaussian is part of the background model
if(iModes < backgroundGaussians)
bBackgroundLow = true;
//update distribution
float k = m_params.Alpha()/weight;
weight = fOneMinAlpha*weight + m_params.Alpha();
m_modes[pos].weight = weight;
m_modes[pos].muR = muR - k*(dR);
m_modes[pos].muG = muG - k*(dG);
m_modes[pos].muB = muB - k*(dB);
//limit the variance
float sigmanew = var + k*(dist-var);
m_modes[pos].variance = sigmanew < 4 ? 4 : sigmanew > 5*m_variance ? 5*m_variance : sigmanew;
m_modes[pos].significants = m_modes[pos].weight / sqrt(m_modes[pos].variance);
}
else
{
weight = fOneMinAlpha*weight;
if (weight < 0.0)
{
weight=0.0;
numModes--;
}
m_modes[pos].weight = weight;
m_modes[pos].significants = m_modes[pos].weight / sqrt(m_modes[pos].variance);
}
}
else
{
weight = fOneMinAlpha*weight;
if (weight < 0.0)
{
weight=0.0;
numModes--;
}
m_modes[pos].weight = weight;
m_modes[pos].significants = m_modes[pos].weight / sqrt(m_modes[pos].variance);
}
totalWeight += weight;
}
// renormalize weights so they add to one
double invTotalWeight = 1.0 / totalWeight;
for (int iLocal = 0; iLocal < numModes; iLocal++)
{
m_modes[posPixel + iLocal].weight *= (float)invTotalWeight;
m_modes[posPixel + iLocal].significants = m_modes[posPixel + iLocal].weight
/ sqrt(m_modes[posPixel + iLocal].variance);
}
// Sort significance values so they are in desending order.
qsort(&m_modes[posPixel], numModes, sizeof(GMM), compareGMM);
// make new mode if needed and exit
if (!bFitsPDF)
{
if (numModes < m_params.MaxModes())
{
numModes++;
}
else
{
// the weakest mode will be replaced
}
pos = posPixel + numModes-1;
m_modes[pos].muR = pixel.ch[0];
m_modes[pos].muG = pixel.ch[1];
m_modes[pos].muB = pixel.ch[2];
m_modes[pos].variance = m_variance;
m_modes[pos].significants = 0; // will be set below
if (numModes==1)
m_modes[pos].weight = 1;
else
m_modes[pos].weight = m_params.Alpha();
//renormalize weights
int iLocal;
float sum = 0.0;
for (iLocal = 0; iLocal < numModes; iLocal++)
{
sum += m_modes[posPixel+ iLocal].weight;
}
double invSum = 1.0/sum;
for (iLocal = 0; iLocal < numModes; iLocal++)
{
m_modes[posPixel + iLocal].weight *= (float)invSum;
m_modes[posPixel + iLocal].significants = m_modes[posPixel + iLocal].weight
/ sqrt(m_modes[posPixel + iLocal].variance);
}
}
// Sort significance values so they are in desending order.
qsort(&(m_modes[posPixel]), numModes, sizeof(GMM), compareGMM);
if(bBackgroundLow)
{
low_threshold = BACKGROUND;
}
else
{
low_threshold = FOREGROUND;
}
if(bBackgroundHigh)
{
high_threshold = BACKGROUND;
}
else
{
high_threshold = FOREGROUND;
}
}
///////////////////////////////////////////////////////////////////////////////
//Input:
// data - a pointer to the data of a RGB image of the same size
//Output:
// output - a pointer to the data of a gray value image of the same size
// (the memory should already be reserved)
// values: 255-foreground, 125-shadow, 0-background
///////////////////////////////////////////////////////////////////////////////
void GrimsonGMM::Subtract(int frame_num, const RgbImage& data,
BwImage& low_threshold_mask, BwImage& high_threshold_mask)
{
unsigned char low_threshold, high_threshold;
long posPixel;
// update each pixel of the image
for(unsigned int r = 0; r < m_params.Height(); ++r)
{
for(unsigned int c = 0; c < m_params.Width(); ++c)
{
// update model + background subtract
posPixel=(r*m_params.Width()+c)*m_params.MaxModes();
SubtractPixel(posPixel, data(r,c), m_modes_per_pixel(r,c), low_threshold, high_threshold);
low_threshold_mask(r,c) = low_threshold;
high_threshold_mask(r,c) = high_threshold;
m_background(r,c,0) = (unsigned char)m_modes[posPixel].muR;
m_background(r,c,1) = (unsigned char)m_modes[posPixel].muG;
m_background(r,c,2) = (unsigned char)m_modes[posPixel].muB;
}
}
}