-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathChanVeseSegmentation.cpp
362 lines (326 loc) · 10.6 KB
/
ChanVeseSegmentation.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// Robert Crandall
// Chan-Vese Segmentation
#include "ChanVeseSegmentation.h"
#include <iostream>
using std::cout;
using std::endl;
using std::atan;
using std::sqrt;
using std::abs;
using std::pow;
using std::min;
using std::max;
const double PI = 3.14159265358979323846264338327950288;
//---------------------------------------------------------------------------//
// function GetRegionAverages
// Compute c1 and c2 as used in the Chan-Vese segmentation algorithm.
// c1 and c2 are given by
// c1 = integral(u0*H(phi))dxdy/integral(H(phi))dxdy
// c2 = integral(u0*(1-H(phi))dxdy/integral(1-H(phi))dxdy
//
// If epsilon == 0, we define H as the usual Heaviside function. Then c1 is
// the average of the image pixels over the set where phi is >= 0, and c2 is
// the average over {phi < 0}.
// If epsilon > 0, we use a smoothed version of the Heaviside function with
// parameter epsilon.
void GetRegionAverages(Image<unsigned char>* img,
Image<double>* phi,
double epsilon,
double &c1,
double &c2)
{
// Non-smoothed calculation
if (0 == epsilon)
{
int n1 = 0;
int n2 = 0;
double Iplus = 0;
double Iminus = 0;
for (unsigned int i = 0; i < img->nRows(); ++i)
{
for (unsigned int j = 0; j < img->nCols(); ++j)
{
if (phi->data()[i][j] >= 0)
{
++n1;
Iplus += (double)img->data()[i][j];
}
else
{
++n2;
Iminus += (double)img->data()[i][j];
}
}
}
c1 = Iplus/double(n1);
c2 = Iminus/double(n2);
}
// Smoothed calculation
else
{
double num1 = 0;
double den1 = 0;
double num2 = 0;
double den2 = 0;
double H_phi;
for (unsigned int i = 0; i < phi->nRows(); ++i)
{
for (unsigned int j = 0; j < phi->nCols(); ++j)
{
// Compute value of H_eps(phi) where H_eps is a mollified Heavyside function
H_phi = .5*(1+(2/PI)*atan(phi->data()[i][j]/epsilon));
num1 += ((double)img->data()[i][j])*H_phi;
den1 += H_phi;
num2 += ((double)img->data()[i][j])*(1 - H_phi);
den2 += 1 - H_phi;
}
}
c1 = num1/den1;
c2 = num2/den2;
}
}
//---------------------------------------------------------------------------//
// function ReinitPhi
// Reinitialize phi to the signed distance function to its zero contour
void ReinitPhi(Image<double>* phiIn,
Image<double>** psiOut,
double dt,
double h,
unsigned int numIts)
{
if (0 == *psiOut)
(*psiOut) = new Image<double>(phiIn->nRows(),phiIn->nCols());
else if ((*psiOut)->nRows() != phiIn->nRows()
|| (*psiOut)->nCols() != phiIn->nCols())
(*psiOut)->Allocate(phiIn->nRows(),phiIn->nCols());
(*psiOut)->CopyFrom(phiIn);
double a;
double b;
double c;
double d;
double x;
double G;
bool fStop = false;
double Q;
unsigned int M;
Image<double>* psiOld = new Image<double>(phiIn->nRows(),phiIn->nCols());
for (unsigned int k = 0; k < numIts && fStop == false; ++k)
{
psiOld->CopyFrom(*psiOut);
for (unsigned int i = 1; i < phiIn->nRows()-1; ++i)
{
for (unsigned int j = 1; j < phiIn->nCols()-1; ++j)
{
a = (phiIn->data()[i][j] - phiIn->data()[i-1][j])/h;
b = (phiIn->data()[i+1][j] - phiIn->data()[i][j])/h;
c = (phiIn->data()[i][j] - phiIn->data()[i][j-1])/h;
d = (phiIn->data()[i][j+1] - phiIn->data()[i][j])/h;
if (phiIn->data()[i][j] > 0)
G = sqrt(max(max(a,0.0)*max(a,0.0),min(b,0.0)*min(b,0.0))
+ max(max(c,0.0)*max(c,0.0),min(d,0.0)*min(d,0.0))) - 1.0;
else if (phiIn->data()[i][j] < 0)
G = sqrt(max(min(a,0.0)*min(a,0.0),max(b,0.0)*max(b,0.0))
+ max(min(c,0.0)*min(c,0.0),max(d,0.0)*max(d,0.0))) - 1.0;
else
G = 0;
x = (phiIn->data()[i][j] >= 0)?(1.0):(-1.0);
(*psiOut)->data()[i][j] = (*psiOut)->data()[i][j] - dt*x*G;
}
}
// Check stopping condition
Q = 0.0;
M = 0.0;
for (unsigned int i = 0; i < phiIn->nRows(); ++i)
{
for (unsigned int j = 0; j < phiIn->nCols(); ++j)
{
if (abs(psiOld->data()[i][j]) <= h)
{
++M;
Q += abs(psiOld->data()[i][j] - (*psiOut)->data()[i][j]);
}
}
}
if (M != 0)
Q = Q/((double)M);
else
Q = 0.0;
if (Q < dt*h*h)
{
fStop = true;
//cout << "Stopping condition reached at " << k+1 << " iterations; Q = " << Q << endl;
}
else
{
//cout << "Iteration " << k << ", Q = " << Q << " > " << dt*h*h << endl;
}
}
}
//---------------------------------------------------------------------------//
// function GetChanVeseCoefficients
// Compute coefficients needed in Chan-Vese segmentation algorithm given current
// level set function
void GetChanVeseCoefficients(Image<double>* phi,
struct CVsetup* pCVinputs,
unsigned int i,
unsigned int j,
double L,
double& F1,
double& F2,
double& F3,
double& F4,
double& F,
double& deltaPhi)
{
// factor to avoid division by zero
double eps = 0.000001;
double h = pCVinputs->h;
double dt = pCVinputs->dt;
double mu = pCVinputs->mu;
unsigned int p = pCVinputs->p;
double C1 = 1/sqrt(eps + pow((phi->data()[i+1][j] - phi->data()[i][j]),2)
+ pow((phi->data()[i][j+1] - phi->data()[i][j-1]),2)/4.0);
double C2 = 1/sqrt(eps + pow((phi->data()[i][j] - phi->data()[i-1][j]),2)
+ pow((phi->data()[i-1][j+1] - phi->data()[i-1][j-1]),2)/4.0);
double C3 = 1/sqrt(eps + pow((phi->data()[i+1][j] - phi->data()[i-1][j]),2)/4.0
+ pow((phi->data()[i][j+1] - phi->data()[i][j]),2));
double C4 = 1/sqrt(eps + pow((phi->data()[i+1][j-1] - phi->data()[i-1][j-1]),2)/4.0
+ pow((phi->data()[i][j] - phi->data()[i][j-1]),2));
deltaPhi = h/(PI*(h*h + (phi->data()[i][j])*(phi->data()[i][j])));
double Factor = dt*deltaPhi*mu*(double(p)*pow(L,p-1));
F = h/(h+Factor*(C1+C2+C3+C4));
Factor = Factor/(h+Factor*(C1+C2+C3+C4));
F1 = Factor*C1;
F2 = Factor*C2;
F3 = Factor*C3;
F4 = Factor*C4;
}
//---------------------------------------------------------------------------//
// Main segmentation algorithm. Segment a grayscale image into foreground and
// background regions, given an initial contour defined by the level set function
// phi. Based on the algorithm described in the paper
// "Active Contours Without Edges" by Chan & Vese.
void ChanVeseSegmentation(Image<unsigned char>* img,
Image<double>* phi0,
Image<double>** phi,
struct CVsetup* pCVinputs)
{
double P_ij;
double deltaPhi;
double F1;
double F2;
double F3;
double F4;
double F;
double L;
double c1;
double c2;
// Segmentation parameters
double h = pCVinputs->h;
double dt = pCVinputs->dt;
double nu = pCVinputs->nu;
double lambda1 = pCVinputs->lambda1;
double lambda2 = pCVinputs->lambda2;
unsigned int p = pCVinputs->p;
// Variables to evaluate stopping condition
bool fStop = false;
double Q;
unsigned int M;
Image<double>* phiOld = new Image<double>(img->nRows(),img->nCols());
// Initialize phi
if (0 == phi)
*phi = new Image<double>(img->nRows(),img->nCols());
else if ((*phi)->nRows() != phi0->nRows() || (*phi)->nCols() != phi0->nCols())
(*phi)->Allocate(phi0->nRows(),phi0->nCols());
(*phi)->CopyFrom(phi0);
// Main loop
for (unsigned int k = 0; k < 5 && fStop == false; ++k)
{
phiOld->CopyFrom(*phi);
// Compute region averages for current level set function
// Main segmentation algorithm
GetRegionAverages(img, *phi, h, c1, c2);
// Inner loop...
for (unsigned int l = 0; l < 5; ++l)
{
// Compute length of contour if p > 1
if (1 == p)
{
L = 1.0;
}
else
{
L = 1.0; // fix this!!
}
// Loop through all interior image pixels
for (unsigned int i = 1; i < img->nRows()-1; ++i)
{
for (unsigned int j = 1; j < img->nCols()-1; ++j)
{
// Compute coefficients needed in update
GetChanVeseCoefficients(*phi,
pCVinputs,
i, j,
L,
F1,
F2,
F3,
F4,
F,
deltaPhi);
P_ij = (*phi)->data()[i][j]
- dt*deltaPhi*(nu + lambda1*pow(img->data()[i][j]-c1,2)
- lambda2*pow(img->data()[i][j]-c2,2));
// Update level set function
(*phi)->data()[i][j] = F1*(*phi)->data()[i+1][j]
+ F2*(*phi)->data()[i-1][j]
+ F3*(*phi)->data()[i][j+1]
+ F4*(*phi)->data()[i][j-1]
+ F*P_ij;
}
}
// Update border values of phi by reflection
for (unsigned int i = 0; i < img->nRows(); ++i)
{
(*phi)->data()[i][0] = (*phi)->data()[i][1];
(*phi)->data()[i][img->nCols()-1] = (*phi)->data()[i][img->nCols()-2];
}
for (unsigned int j = 0; j < img->nCols(); ++j)
{
(*phi)->data()[0][j] = (*phi)->data()[1][j];
(*phi)->data()[img->nRows()-1][j] = (*phi)->data()[img->nRows()-2][j];
}
// Reinitialize phi to the signed distance function to its zero contour
ReinitPhi(*phi, phi, 0.1, h, 100);
}
// Check stopping condition
/*
Q = 0.0;
M = 0.0;
for (unsigned int i = 0; i < img->nRows(); ++i)
{
for (unsigned int j = 0; j < img->nCols(); ++j)
{
if (abs(phiOld->data()[i][j]) <= h)
{
++M;
Q += abs(phiOld->data()[i][j] - (*phi)->data()[i][j]);
}
}
}
if (M != 0)
Q = Q/((double)M);
else
Q = 0.0;
if (Q < dt*h*h)
{
fStop = true;
cout << "Stopping condition reached at " << k+1 << " iterations; Q = " << Q << endl;
}
else
{
cout << "Iteration " << k << ", Q = " << Q << " > " << dt*h*h << endl;
}
*/
}
}