-
Notifications
You must be signed in to change notification settings - Fork 2
/
GMM.cpp
executable file
·339 lines (278 loc) · 9.92 KB
/
GMM.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
333
334
335
336
337
338
339
#include "GMM.h"
#include <cxcore.h>
namespace GrabCutNS {
GMM::GMM(unsigned int K) : m_K(K)
{
m_gaussians = new Gaussian[m_K];
}
GMM::~GMM()
{
if (m_gaussians)
delete [] m_gaussians;
}
Real GMM::p(Color c)
{
Real result = 0;
if (m_gaussians)
{
for (unsigned int i=0; i < m_K; i++)
{
result += m_gaussians[i].pi * p(i, c);
}
}
return result;
}
Real GMM::p(unsigned int i, Color c)
{
Real result = 0;
if( m_gaussians[i].pi > 0 )
{
if (m_gaussians[i].determinant > 0)
{
Real r = c.r - m_gaussians[i].mu.r;
Real g = c.g - m_gaussians[i].mu.g;
Real b = c.b - m_gaussians[i].mu.b;
Real d = r * (r*m_gaussians[i].inverse[0][0] + g*m_gaussians[i].inverse[1][0] + b*m_gaussians[i].inverse[2][0]) +
g * (r*m_gaussians[i].inverse[0][1] + g*m_gaussians[i].inverse[1][1] + b*m_gaussians[i].inverse[2][1]) +
b * (r*m_gaussians[i].inverse[0][2] + g*m_gaussians[i].inverse[1][2] + b*m_gaussians[i].inverse[2][2]);
result = (Real)(1.0/(sqrt(m_gaussians[i].determinant)) * exp(-0.5*d));
}
}
return result;
}
void buildGMMs(GMM& backgroundGMM, GMM& foregroundGMM, Image<unsigned int>& components, const Image<Color>& image, const Image<SegmentationValue>& hardSegmentation)
{
// Step 3: Build GMMs using Orchard-Bouman clustering algorithm
// Set up Gaussian Fitters
GaussianFitter* backFitters = new GaussianFitter[backgroundGMM.K()];
GaussianFitter* foreFitters = new GaussianFitter[foregroundGMM.K()];
unsigned int foreCount = 0, backCount = 0;
// Initialize the first foreground and background clusters
for (unsigned int y = 0; y < image.height(); ++y)
{
for (unsigned int x = 0; x < image.width(); ++x)
{
components(x,y) = 0;
if (hardSegmentation(x,y) == SegmentationForeground)
{
foreFitters[0].add(image(x,y));
foreCount++;
}
else
{
backFitters[0].add(image(x,y));
backCount++;
}
}
}
backFitters[0].finalize(backgroundGMM.m_gaussians[0], backCount, true);
foreFitters[0].finalize(foregroundGMM.m_gaussians[0], foreCount, true);
unsigned int nBack = 0, nFore = 0; // Which cluster will be split
unsigned int maxK = backgroundGMM.K() > foregroundGMM.K() ? backgroundGMM.K() : foregroundGMM.K();
// Compute clusters
for (unsigned int i = 1; i < maxK; i++)
{
// Reset the fitters for the splitting clusters
backFitters[nBack] = GaussianFitter();
foreFitters[nFore] = GaussianFitter();
// For brevity, get references to the splitting Gaussians
Gaussian& bg = backgroundGMM.m_gaussians[nBack];
Gaussian& fg = foregroundGMM.m_gaussians[nFore];
// Compute splitting points
Real splitBack = bg.eigenvectors[0][0] * bg.mu.r + bg.eigenvectors[1][0] * bg.mu.g + bg.eigenvectors[2][0] * bg.mu.b;
Real splitFore = fg.eigenvectors[0][0] * fg.mu.r + fg.eigenvectors[1][0] * fg.mu.g + fg.eigenvectors[2][0] * fg.mu.b;
// Split clusters nBack and nFore, place split portion into cluster i
for (unsigned int y = 0; y < image.height(); ++y)
{
for(unsigned int x = 0; x < image.width(); ++x)
{
Color c = image(x,y);
// For each pixel
if (i < foregroundGMM.K() && hardSegmentation(x,y) == SegmentationForeground && components(x,y) == nFore)
{
if (fg.eigenvectors[0][0] * c.r + fg.eigenvectors[1][0] * c.g + fg.eigenvectors[2][0] * c.b > splitFore)
{
components(x,y) = i;
foreFitters[i].add(c);
}
else
{
foreFitters[nFore].add(c);
}
}
else if (i < backgroundGMM.K() && hardSegmentation(x,y) == SegmentationBackground && components(x,y) == nBack)
{
if (bg.eigenvectors[0][0] * c.r + bg.eigenvectors[1][0] * c.g + bg.eigenvectors[2][0] * c.b > splitBack)
{
components(x,y) = i;
backFitters[i].add(c);
}
else
{
backFitters[nBack].add(c);
}
}
}
}
// Compute new split Gaussians
backFitters[nBack].finalize(backgroundGMM.m_gaussians[nBack], backCount, true);
foreFitters[nFore].finalize(foregroundGMM.m_gaussians[nFore], foreCount, true);
if (i < backgroundGMM.K())
backFitters[i].finalize(backgroundGMM.m_gaussians[i], backCount, true);
if (i < foregroundGMM.K())
foreFitters[i].finalize(foregroundGMM.m_gaussians[i], foreCount, true);
// Find clusters with highest eigenvalue
nBack = 0;
nFore = 0;
for (unsigned int j = 0; j <= i; j++ )
{
if (j < backgroundGMM.K() && backgroundGMM.m_gaussians[j].eigenvalues[0] > backgroundGMM.m_gaussians[nBack].eigenvalues[0])
nBack = j;
if (j < foregroundGMM.K() && foregroundGMM.m_gaussians[j].eigenvalues[0] > foregroundGMM.m_gaussians[nFore].eigenvalues[0])
nFore = j;
}
}
delete [] backFitters;
delete [] foreFitters;
}
void learnGMMs(GMM& backgroundGMM, GMM& foregroundGMM, Image<unsigned int>& components, const Image<Color>& image, const Image<SegmentationValue>& hardSegmentation)
{
// Step 4: Assign each pixel to the component which maximizes its probability
for (unsigned int y = 0; y < image.height(); ++y)
{
for (unsigned int x = 0; x < image.width(); ++x)
{
Color c = image(x,y);
if (hardSegmentation(x,y) == SegmentationForeground)
{
int k = 0;
Real max = 0;
for (unsigned int i = 0; i < foregroundGMM.K(); i++)
{
Real p = foregroundGMM.p(i, c);
if (p > max)
{
k = i;
max = p;
}
}
components(x, y) = k;
}
else
{
int k = 0;
Real max = 0;
for (unsigned int i = 0; i < backgroundGMM.K(); i++)
{
Real p = backgroundGMM.p(i, c);
if (p > max)
{
k = i;
max = p;
}
}
components(x, y) = k;
}
}
}
// Step 5: Relearn GMMs from new component assignments
// Set up Gaussian Fitters
GaussianFitter* backFitters = new GaussianFitter[backgroundGMM.K()];
GaussianFitter* foreFitters = new GaussianFitter[foregroundGMM.K()];
unsigned int foreCount = 0, backCount = 0;
for (unsigned int y = 0; y < image.height(); ++y)
{
for(unsigned int x = 0; x < image.width(); ++x)
{
Color c = image(x,y);
if(hardSegmentation(x,y) == SegmentationForeground)
{
foreFitters[components(x,y)].add(c);
foreCount++;
}
else
{
backFitters[components(x,y)].add(c);
backCount++;
}
}
}
for (unsigned int i = 0; i < backgroundGMM.K(); i++)
backFitters[i].finalize(backgroundGMM.m_gaussians[i], backCount, false);
for (unsigned int i = 0; i < foregroundGMM.K(); i++)
foreFitters[i].finalize(foregroundGMM.m_gaussians[i], foreCount, false);
delete [] backFitters;
delete [] foreFitters;
}
// GaussianFitter functions
GaussianFitter::GaussianFitter()
{
s = Color();
p[0][0] = 0; p[0][1] = 0; p[0][2] = 0;
p[1][0] = 0; p[1][1] = 0; p[1][2] = 0;
p[2][0] = 0; p[2][1] = 0; p[2][2] = 0;
count = 0;
}
// Add a color sample
void GaussianFitter::add(Color c)
{
s.r += c.r; s.g += c.g; s.b += c.b;
p[0][0] += c.r*c.r; p[0][1] += c.r*c.g; p[0][2] += c.r*c.b;
p[1][0] += c.g*c.r; p[1][1] += c.g*c.g; p[1][2] += c.g*c.b;
p[2][0] += c.b*c.r; p[2][1] += c.b*c.g; p[2][2] += c.b*c.b;
count++;
}
// Build the gaussian out of all the added colors
void GaussianFitter::finalize(Gaussian& g, unsigned int totalCount, bool computeEigens) const
{
// Running into a singular covariance matrix is problematic. So we'll add a small epsilon
// value to the diagonal elements to ensure a positive definite covariance matrix.
const Real Epsilon = (Real)0.0001;
if (count==0)
{
g.pi = 0;
}
else
{
// Compute mean of gaussian
g.mu.r = s.r/count;
g.mu.g = s.g/count;
g.mu.b = s.b/count;
// Compute covariance matrix
g.covariance[0][0] = p[0][0]/count-g.mu.r*g.mu.r + Epsilon; g.covariance[0][1] = p[0][1]/count-g.mu.r*g.mu.g; g.covariance[0][2] = p[0][2]/count-g.mu.r*g.mu.b;
g.covariance[1][0] = p[1][0]/count-g.mu.g*g.mu.r; g.covariance[1][1] = p[1][1]/count-g.mu.g*g.mu.g + Epsilon; g.covariance[1][2] = p[1][2]/count-g.mu.g*g.mu.b;
g.covariance[2][0] = p[2][0]/count-g.mu.b*g.mu.r; g.covariance[2][1] = p[2][1]/count-g.mu.b*g.mu.g; g.covariance[2][2] = p[2][2]/count-g.mu.b*g.mu.b + Epsilon;
// Compute determinant of covariance matrix
g.determinant = g.covariance[0][0]*(g.covariance[1][1]*g.covariance[2][2]-g.covariance[1][2]*g.covariance[2][1])
- g.covariance[0][1]*(g.covariance[1][0]*g.covariance[2][2]-g.covariance[1][2]*g.covariance[2][0])
+ g.covariance[0][2]*(g.covariance[1][0]*g.covariance[2][1]-g.covariance[1][1]*g.covariance[2][0]);
// Compute inverse (cofactor matrix divided by determinant)
g.inverse[0][0] = (g.covariance[1][1]*g.covariance[2][2] - g.covariance[1][2]*g.covariance[2][1]) / g.determinant;
g.inverse[1][0] = -(g.covariance[1][0]*g.covariance[2][2] - g.covariance[1][2]*g.covariance[2][0]) / g.determinant;
g.inverse[2][0] = (g.covariance[1][0]*g.covariance[2][1] - g.covariance[1][1]*g.covariance[2][0]) / g.determinant;
g.inverse[0][1] = -(g.covariance[0][1]*g.covariance[2][2] - g.covariance[0][2]*g.covariance[2][1]) / g.determinant;
g.inverse[1][1] = (g.covariance[0][0]*g.covariance[2][2] - g.covariance[0][2]*g.covariance[2][0]) / g.determinant;
g.inverse[2][1] = -(g.covariance[0][0]*g.covariance[2][1] - g.covariance[0][1]*g.covariance[2][0]) / g.determinant;
g.inverse[0][2] = (g.covariance[0][1]*g.covariance[1][2] - g.covariance[0][2]*g.covariance[1][1]) / g.determinant;
g.inverse[1][2] = -(g.covariance[0][0]*g.covariance[1][2] - g.covariance[0][2]*g.covariance[1][0]) / g.determinant;
g.inverse[2][2] = (g.covariance[0][0]*g.covariance[1][1] - g.covariance[0][1]*g.covariance[1][0]) / g.determinant;
// The weight of the gaussian is the fraction of the number of pixels in this Gaussian to the number of
// pixels in all the gaussians of this GMM.
g.pi = (Real)count/totalCount;
if (computeEigens)
{
#ifdef USE_DOUBLE
#define CV_TYPE CV_64FC1
#else
#define CV_TYPE CV_32FC1
#endif
// Build OpenCV wrappers around our data.
CvMat mat = cvMat(3, 3, CV_TYPE, g.covariance);
CvMat eval = cvMat(3, 1, CV_TYPE, g.eigenvalues);
CvMat evec = cvMat(3, 3, CV_TYPE, g.eigenvectors);
// Compute eigenvalues and vectors using SVD
cvSVD( &mat, &eval, &evec );
}
}
}
}