-
Notifications
You must be signed in to change notification settings - Fork 1
/
BayesianFilter.cxx
326 lines (278 loc) · 11.4 KB
/
BayesianFilter.cxx
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
#include "BayesianFilter.h"
#include "itkImage.h"
#include "itkBayesianClassifierInitializationImageFilter.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkBayesianClassifierImageFilter.h"
#include "itkGradientAnisotropicDiffusionImageFilter.h"
#include "itkRescaleIntensityImageFilter.h"
#include "itkDiscreteGaussianImageFilter.h"
#include "itkBinaryThresholdImageFilter.h"
#include "itkSubtractImageFilter.h"
#include "itkLabelStatisticsImageFilter.h"
#include "itkStatisticsImageFilter.h"
BayesianFilter::BayesianFilter()
{
// parameter initialization
m_numOfInitClasses = 2;
m_variance = 0.3;
m_weight = 0.5;
m_verbose = true;
m_smooth = false;
m_smoothItr = 1;
m_gadItr = 5;
m_timeStep = 0.125;
m_conductance = 3;
m_inputImage = ImageType::New();
m_labelImage = LabelImageType::New();
m_output = LabelImageType::New();
}
BayesianFilter::~BayesianFilter()
{
}
void BayesianFilter::SetNumberOfBayesianInitialClasses(unsigned int numOfInitClasses)
{
m_numOfInitClasses = numOfInitClasses;
}
void BayesianFilter::SetGaussianBlurVariance(float variance)
{
m_variance = variance;
}
void BayesianFilter::SetLabelWeight(float weight)
{
m_weight = weight;
}
void BayesianFilter::SetVerbose(bool verbose)
{
m_verbose = verbose;
}
void BayesianFilter::SetSmooth(bool smooth)
{
m_smooth = smooth;
}
void BayesianFilter::SetNumberOfSmoothingIterations(unsigned int smoothItr)
{
m_smoothItr = smoothItr;
}
void BayesianFilter::SetNumberOfGradientAnistropicDiffusionIterations(unsigned int gadItr)
{
m_gadItr = gadItr;
}
void BayesianFilter::SetSmoothingTimeStep(float timeStep)
{
m_timeStep = timeStep;
}
void BayesianFilter::SetConductanceParameter(float conductance)
{
m_conductance = conductance;
}
void BayesianFilter::SetImage(ImageType::Pointer inputImage)
{
m_inputImage->Graft(inputImage);
}
void BayesianFilter::SetLabel(LabelImageType::Pointer labelImage)
{
m_labelImage->Graft(labelImage);
}
void BayesianFilter::Run()
{
// check input is valid
if (m_inputImage == nullptr || m_labelImage == nullptr)
{
std::cerr << "Invalid input data" << std::endl;
return;
}
// check input image and label have same size
if (m_inputImage->GetBufferedRegion().GetSize()[0] != m_labelImage->GetBufferedRegion().GetSize()[0] ||
m_inputImage->GetBufferedRegion().GetSize()[1] != m_labelImage->GetBufferedRegion().GetSize()[1] ||
m_inputImage->GetBufferedRegion().GetSize()[2] != m_labelImage->GetBufferedRegion().GetSize()[2])
{
std::cerr << "Input image and label should have same size" << std::endl;
return;
}
// Bayesian initialization
if (m_verbose)
{
std::cout << "Bayesian refinement in progress..." << std::endl;
std::cout << "Start Bayesian initialization..." << std::endl;
}
BayesianInitializerType::Pointer bayesianInitializer = BayesianInitializerType::New();
bayesianInitializer->SetInput(m_inputImage);
bayesianInitializer->SetNumberOfClasses(m_numOfInitClasses);
bayesianInitializer->Update();
// Extract membership image
if (m_verbose)
{
std::cout << "Extracting membership images from Bayesian intialization..." << std::endl;
}
MembershipImageRegionConstIteratorType bayesianInitializerIterator(bayesianInitializer->GetOutput(),
bayesianInitializer->GetOutput()->GetBufferedRegion());
std::vector<ImageType::Pointer> bayesianInitializerMembershipImageList;
for (int i = 0;i < m_numOfInitClasses; i++)
{
// allocate the membership image of each initialized class
ImageType::Pointer membershipImage = ImageType::New();
bayesianInitializerMembershipImageList.push_back(membershipImage);
membershipImage->CopyInformation(bayesianInitializer->GetOutput());
membershipImage->SetBufferedRegion(bayesianInitializer->GetOutput()->GetBufferedRegion());
membershipImage->SetRequestedRegion(bayesianInitializer->GetOutput()->GetRequestedRegion());
membershipImage->Allocate();
// copy memberhsip image from Bayesian initializer output
ImageRegionIteratorType membershipIterator(membershipImage,
membershipImage->GetBufferedRegion());
bayesianInitializerIterator.GoToBegin();
membershipIterator.GoToBegin();
while (!bayesianInitializerIterator.IsAtEnd())
{
membershipIterator.Set(bayesianInitializerIterator.Get()[i]);
++membershipIterator;
++bayesianInitializerIterator;
}
}
// Bayesian classification using Kmean
if (m_verbose)
{
std::cout << "Performing Bayesian classification with Kmean membership..." << std::endl;
}
// bayesian classification
BayesianClassifierFilterType::Pointer bayesianClassifierFiler = BayesianClassifierFilterType::New();
bayesianClassifierFiler->SetInput(bayesianInitializer->GetOutput());
// apply smoothing on the Bayesian classification
if (m_smooth)
{
bayesianClassifierFiler->SetNumberOfSmoothingIterations(1);
GADSmoothingFilterType::Pointer smoother = GADSmoothingFilterType::New();
smoother->SetNumberOfIterations(5);
smoother->SetTimeStep(0.125);
smoother->SetConductanceParameter(3);
bayesianClassifierFiler->SetSmoothingFilter(smoother);
}
bayesianClassifierFiler->Update();
// calculate mean in each class with bayesian segmentation output
if (m_verbose)
{
std::cout << "Calculating statistics on segmentation labels..." << std::endl;
}
LabelStatisticsImageFilterType::Pointer bayesianLabelStatisticFilter = LabelStatisticsImageFilterType::New();
bayesianLabelStatisticFilter->SetInput(m_inputImage);
bayesianLabelStatisticFilter->SetLabelInput(bayesianClassifierFiler->GetOutput());
bayesianLabelStatisticFilter->Update();
// calculate the mean in each class with user input
LabelStatisticsImageFilterType::Pointer userLabelStatisticFilter = LabelStatisticsImageFilterType::New();
userLabelStatisticFilter->SetInput(m_inputImage);
userLabelStatisticFilter->SetLabelInput(m_labelImage);
userLabelStatisticFilter->Update();
// gaussian weighted bayesian initialization membership
if (m_verbose)
{
std::cout << "Calculating user label weighted membership..." << std::endl;
}
// create gaussian mask image
StatisticsLabelImageFilterType::Pointer labelStatFilter = StatisticsLabelImageFilterType::New();
labelStatFilter->SetInput(m_labelImage);
labelStatFilter->Update();
MembershipImageType::Pointer gaussianMemebership = MembershipImageType::New();
MembershipImageType::IndexType start;
MembershipImageType::SizeType size;
for (int i = 0; i < 3; i++)
{
start.SetElement(i, m_labelImage->GetBufferedRegion().GetIndex()[i]);
size.SetElement(i, m_labelImage->GetBufferedRegion().GetSize()[i]);
}
MembershipImageType::RegionType region(start, size);
gaussianMemebership->SetRegions(region);
gaussianMemebership->SetNumberOfComponentsPerPixel(labelStatFilter->GetMaximum()+1); // note that there is 0 class
gaussianMemebership->Allocate();
// calculate the membership for each class in user label input
for (auto vIt = userLabelStatisticFilter->GetValidLabelValues().begin();
vIt != userLabelStatisticFilter->GetValidLabelValues().end();
++vIt)
{
if (userLabelStatisticFilter->HasLabel(*vIt))
{
unsigned short labelValue = *vIt;
//std::cout << "label: " << labelValue << std::endl;
//std::cout << "mean: " << userLabelStatisticFilter->GetMean(labelValue) << std::endl;
// find the bayesian segmented class with nearest mean
float diff = std::numeric_limits<float>::max();
unsigned short correspondBayesianClass = 0;
for (auto bayesianLabelStatIt = bayesianLabelStatisticFilter->GetValidLabelValues().begin();
bayesianLabelStatIt != bayesianLabelStatisticFilter->GetValidLabelValues().end();
++bayesianLabelStatIt)
{
if (bayesianLabelStatisticFilter->HasLabel(*bayesianLabelStatIt))
{
if (std::abs(bayesianLabelStatisticFilter->GetMean(*bayesianLabelStatIt) - userLabelStatisticFilter->GetMean(labelValue)) < diff)
{
diff = std::abs(bayesianLabelStatisticFilter->GetMean(*bayesianLabelStatIt) - userLabelStatisticFilter->GetMean(labelValue));
correspondBayesianClass = *bayesianLabelStatIt;
}
}
}
//std::cout << "corresponding bayesian class: " << correspondBayesianClass << std::endl;
//std::cout << "mean: " << bayesianLabelStatisticFilter->GetMean(correspondBayesianClass) << std::endl;
// calculate statistic of each membership class
StatisticsImageFilterType::Pointer bayesianMembershipStatFilter = StatisticsImageFilterType::New();
bayesianMembershipStatFilter->SetInput(bayesianInitializerMembershipImageList.at(correspondBayesianClass));
bayesianMembershipStatFilter->Update();
//std::cout << "Bayesian membership class: " << correspondBayesianClass << std::endl;
//std::cout << "Mean: " << bayesianMembershipStatFilter->GetMean() << std::endl;
//std::cout << "***************************************" << std::endl;
// extract the label
using BinaryThresholdImageFilterType = itk::BinaryThresholdImageFilter<LabelImageType, LabelImageType>;
BinaryThresholdImageFilterType::Pointer thresholdFilter = BinaryThresholdImageFilterType::New();
thresholdFilter->SetInput(m_labelImage);
thresholdFilter->SetUpperThreshold(labelValue);
thresholdFilter->SetLowerThreshold(labelValue);
thresholdFilter->SetInsideValue(1);
thresholdFilter->SetOutsideValue(0);
thresholdFilter->Update();
// apply gaussian blur on the label
GaussianFilterType::Pointer gaussianFilter = GaussianFilterType::New();
gaussianFilter->SetInput(thresholdFilter->GetOutput());
gaussianFilter->SetVariance(m_variance);
gaussianFilter->Update();
ImageRegionConstIteratorType labelIterator(gaussianFilter->GetOutput(),
gaussianFilter->GetOutput()->GetLargestPossibleRegion());
//MembershipImageRegionConstIteratorType bayesianInitializerIterator(bayesianInitializer->GetOutput(),
// bayesianInitializer->GetOutput()->GetLargestPossibleRegion());
MembershipImageRegionIteratorType gaussianMembershipIterator(gaussianMemebership,
gaussianMemebership->GetLargestPossibleRegion());
labelIterator.GoToBegin();
bayesianInitializerIterator.GoToBegin();
gaussianMembershipIterator.GoToBegin();
while (!labelIterator.IsAtEnd())
{
gaussianMembershipIterator.Get().SetElement(labelValue, ((1.0 - m_weight)*bayesianInitializerIterator.Get()[correspondBayesianClass] * 0.5 + m_weight*labelIterator.Get() * bayesianMembershipStatFilter->GetMean()) / ((0.5 + bayesianMembershipStatFilter->GetMean()) / 2.0));
//if (gaussianMembershipIterator.Get().GetElement(count) < 0)
//{
// std::cout << "correspondBayesianClass: " << correspondBayesianClass << std::endl;
// std::cout << "bayesianInitializeIterator.Get()[correspondBayesianClass]: " << bayesianInitializerIterator.Get()[correspondBayesianClass] << std::endl;
// std::cout << "labelIterator.Get(): " << labelIterator.Get() << std::endl;
// std::cout << "bayesianMembershipStatFilter->GetMean(): " << bayesianMembershipStatFilter->GetMean() << std::endl;
//}
++gaussianMembershipIterator;
++bayesianInitializerIterator;
++labelIterator;
}
}
}
// final Bayesian segmentation
if (m_verbose)
{
std::cout << "Performing label refinement..." << std::endl;
}
// final bayesian classification
BayesianClassifierFilterType::Pointer bayesianClassifierFilter2 = BayesianClassifierFilterType::New();
bayesianClassifierFilter2->SetInput(gaussianMemebership);
bayesianClassifierFilter2->Update();
m_output->Graft(bayesianClassifierFilter2->GetOutput());
}
LabelImageType::Pointer BayesianFilter::GetOutput()
{
return m_output;
}
void BayesianFilter::SetOutput(LabelImageType::Pointer output)
{
m_output = output;
}