forked from InsightSoftwareConsortium/ITK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
itkBayesianClassifierImageFilter.txx
375 lines (305 loc) · 12.7 KB
/
itkBayesianClassifierImageFilter.txx
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
363
364
365
366
367
368
369
370
371
372
373
374
375
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: itkBayesianClassifierImageFilter.txx
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Insight Software Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
Portions of this code are covered under the VTK copyright.
See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#ifndef __itkBayesianClassifierImageFilter_txx
#define __itkBayesianClassifierImageFilter_txx
#include "itkBayesianClassifierImageFilter.h"
#include "itkImageRegionConstIterator.h"
namespace itk
{
/**
* Constructor
*/
template < class TInputVectorImage, class TLabelsType,
class TPosteriorsPrecisionType, class TPriorsPrecisionType >
BayesianClassifierImageFilter<TInputVectorImage, TLabelsType,
TPosteriorsPrecisionType, TPriorsPrecisionType >
::BayesianClassifierImageFilter()
{
m_UserProvidedPriors = false;
m_UserProvidedSmoothingFilter = false;
this->SetNumberOfRequiredOutputs( 2 );
m_NumberOfSmoothingIterations = 0;
m_SmoothingFilter = NULL;
PosteriorsImagePointer p = PosteriorsImageType::New();
this->SetNthOutput( 1 , p.GetPointer() );
}
/**
* Print Self Method
*/
template < class TInputVectorImage, class TLabelsType,
class TPosteriorsPrecisionType, class TPriorsPrecisionType >
void
BayesianClassifierImageFilter<TInputVectorImage, TLabelsType,
TPosteriorsPrecisionType, TPriorsPrecisionType >
::PrintSelf(std::ostream& os, Indent indent) const
{
Superclass::PrintSelf(os,indent);
os << indent << "User provided priors = " << m_UserProvidedPriors << std::endl;
os << indent << "User provided smooting filter = " << m_UserProvidedSmoothingFilter << std::endl;
os << indent << "Smoothing filter pointer = " << m_SmoothingFilter.GetPointer() << std::endl;
os << indent << "Number of smoothing iterations = " << m_NumberOfSmoothingIterations << std::endl;
}
/**
* Generate Data method is where the classification (and smoothing) is performed.
*/
template < class TInputVectorImage, class TLabelsType,
class TPosteriorsPrecisionType, class TPriorsPrecisionType >
void
BayesianClassifierImageFilter<TInputVectorImage, TLabelsType,
TPosteriorsPrecisionType, TPriorsPrecisionType >
::GenerateData()
{
// Setup input image
const InputImageType * membershipImage = this->GetInput();
// Setup general parameters
const unsigned int numberOfClasses = membershipImage->GetVectorLength();
if( numberOfClasses == 0 )
{
itkExceptionMacro("The number of components in the input Membership image is Zero !");
return;
}
this->AllocateOutputs();
this->ComputeBayesRule();
if( m_UserProvidedSmoothingFilter )
{
this->NormalizeAndSmoothPosteriors();
}
this->ClassifyBasedOnPosteriors();
}
template < class TInputVectorImage, class TLabelsType,
class TPosteriorsPrecisionType, class TPriorsPrecisionType >
typename BayesianClassifierImageFilter<TInputVectorImage, TLabelsType,
TPosteriorsPrecisionType, TPriorsPrecisionType >
::PosteriorsImageType *
BayesianClassifierImageFilter<TInputVectorImage, TLabelsType,
TPosteriorsPrecisionType, TPriorsPrecisionType >
::GetPosteriorImage()
{
return dynamic_cast< PosteriorsImageType * >(
this->ProcessObject::GetOutput(1) );
}
/**
* Compute the labeled map with no priors and no smoothing
*/
template < class TInputVectorImage, class TLabelsType,
class TPosteriorsPrecisionType, class TPriorsPrecisionType >
void
BayesianClassifierImageFilter<TInputVectorImage, TLabelsType,
TPosteriorsPrecisionType, TPriorsPrecisionType >
::AllocateOutputs()
{
const InputImageType * membershipImage = this->GetInput();
this->GetOutput()->SetRegions( membershipImage->GetBufferedRegion() );
this->GetOutput()->SetOrigin( membershipImage->GetOrigin() );
this->GetOutput()->SetSpacing( membershipImage->GetSpacing() );
this->GetOutput()->SetDirection( membershipImage->GetDirection() );
this->GetOutput()->Allocate();
// The first output is the Image of Labels,
// The second output is the image of Posteriors.
// TODO Make this optional.. RequiredNumberOfOutputs should not always
// be 2.
this->GetPosteriorImage()->SetRegions( membershipImage->GetBufferedRegion() );
this->GetPosteriorImage()->SetOrigin( membershipImage->GetOrigin() );
this->GetPosteriorImage()->SetSpacing( membershipImage->GetSpacing() );
this->GetPosteriorImage()->SetDirection( membershipImage->GetDirection() );
this->GetPosteriorImage()->SetVectorLength( this->GetInput()->GetVectorLength() );
this->GetPosteriorImage()->Allocate();
}
/**
* Compute the posteriors using the Bayes rule. If no priors are available,
* then the posteriors are just a copy of the memberships. */
template < class TInputVectorImage, class TLabelsType,
class TPosteriorsPrecisionType, class TPriorsPrecisionType >
void
BayesianClassifierImageFilter<TInputVectorImage, TLabelsType,
TPosteriorsPrecisionType, TPriorsPrecisionType >
::ComputeBayesRule()
{
itkDebugMacro( << "Computing Bayes Rule" );
const InputImageType * membershipImage = this->GetInput();
ImageRegionType imageRegion = membershipImage->GetBufferedRegion();
if( m_UserProvidedPriors )
{
const PriorsImageType * priorsImage =
dynamic_cast< const PriorsImageType * >( this->GetInput(1) );
if( priorsImage == NULL )
{
itkExceptionMacro("Second input type does not correspond to expected Priors Image Type");
}
PosteriorsImageType * posteriorsImage =
dynamic_cast< PosteriorsImageType * >( this->GetPosteriorImage() );
if( posteriorsImage == NULL )
{
itkExceptionMacro("Second output type does not correspond to expected Posteriors Image Type");
}
InputImageIteratorType itrMembershipImage( membershipImage, imageRegion );
PriorsImageIteratorType itrPriorsImage( priorsImage, imageRegion );
PosteriorsImageIteratorType itrPosteriorsImage( posteriorsImage, imageRegion );
itrMembershipImage.GoToBegin();
itrPriorsImage.GoToBegin();
const unsigned int numberOfClasses = membershipImage->GetVectorLength();
itkDebugMacro( << "Computing Bayes Rule nclasses in membershipImage: " << numberOfClasses );
while( !itrMembershipImage.IsAtEnd() )
{
PosteriorsPixelType posteriors;
const PriorsPixelType priors = itrPriorsImage.Get();
const MembershipPixelType memberships = itrMembershipImage.Get();
for( unsigned int i=0; i<numberOfClasses; i++)
{
posteriors[i] =
static_cast< TPosteriorsPrecisionType >( memberships[i] * priors[i] );
}
itrPosteriorsImage.Set( posteriors );
++itrMembershipImage;
++itrPriorsImage;
++itrPosteriorsImage;
}
}
else
{
PosteriorsImageType * posteriorsImage =
dynamic_cast< PosteriorsImageType * >( this->GetPosteriorImage() );
if( posteriorsImage == NULL )
{
itkExceptionMacro("Second output type does not correspond to expected Posteriors Image Type");
}
InputImageIteratorType itrMembershipImage( membershipImage, imageRegion );
PosteriorsImageIteratorType itrPosteriorsImage( posteriorsImage, imageRegion );
itrMembershipImage.GoToBegin();
itrPosteriorsImage.GoToBegin();
while( !itrMembershipImage.IsAtEnd() )
{
itrPosteriorsImage.Set( itrMembershipImage.Get() );
++itrMembershipImage;
++itrPosteriorsImage;
}
}
}
template < class TInputVectorImage, class TLabelsType,
class TPosteriorsPrecisionType, class TPriorsPrecisionType >
void
BayesianClassifierImageFilter<TInputVectorImage, TLabelsType,
TPosteriorsPrecisionType, TPriorsPrecisionType >
::SetSmoothingFilter( SmoothingFilterType * smoothingFilter )
{
this->m_SmoothingFilter = smoothingFilter;
this->m_UserProvidedSmoothingFilter = true;
this->Modified();
}
/**
* Normalize the posteriors and smooth them using an user-provided.
*/
template < class TInputVectorImage, class TLabelsType,
class TPosteriorsPrecisionType, class TPriorsPrecisionType >
void
BayesianClassifierImageFilter<TInputVectorImage, TLabelsType,
TPosteriorsPrecisionType, TPriorsPrecisionType >
::NormalizeAndSmoothPosteriors()
{
PosteriorsImageIteratorType itrPosteriorImage(
this->GetPosteriorImage(), this->GetPosteriorImage()->GetBufferedRegion() );
PosteriorsPixelType p;
const unsigned int numberOfClasses = this->GetPosteriorImage()->GetVectorLength();
for( unsigned int iter=0; iter< m_NumberOfSmoothingIterations; iter++)
{
itrPosteriorImage.GoToBegin();
while( !itrPosteriorImage.IsAtEnd() )
{
p = itrPosteriorImage.Get();
// Normalize P so the probablity across components sums to 1
TPosteriorsPrecisionType probability=0;
for( unsigned int i=0; i< numberOfClasses; i++ )
{
probability += p[i];
}
p /= probability;
itrPosteriorImage.Set( p );
++itrPosteriorImage;
}
for( unsigned int componentToExtract=0; componentToExtract < numberOfClasses; componentToExtract++)
{
// Create an auxillary image to store one component of the vector image.
// Smoothing filters typically can't handle multi-component images, so we
// will extract each component and smooth it.
typename ExtractedComponentImageType::Pointer extractedComponentImage =
ExtractedComponentImageType::New();
extractedComponentImage->CopyInformation( this->GetPosteriorImage());
extractedComponentImage->SetBufferedRegion(
this->GetPosteriorImage()->GetBufferedRegion() );
extractedComponentImage->SetRequestedRegion(
this->GetPosteriorImage()->GetRequestedRegion() );
extractedComponentImage->Allocate();
typedef itk::ImageRegionIterator< ExtractedComponentImageType > IteratorType;
itrPosteriorImage.GoToBegin();
IteratorType it( extractedComponentImage,
extractedComponentImage->GetBufferedRegion() );
it.GoToBegin();
while( !itrPosteriorImage.IsAtEnd() )
{
it.Set(itrPosteriorImage.Get()[componentToExtract]);
++it;
++itrPosteriorImage;
}
m_SmoothingFilter->SetInput( extractedComponentImage );
m_SmoothingFilter->Modified(); // Force an update
m_SmoothingFilter->Update();
itrPosteriorImage.GoToBegin();
IteratorType sit( m_SmoothingFilter->GetOutput(),
m_SmoothingFilter->GetOutput()->GetBufferedRegion() );
sit.GoToBegin();
while( !itrPosteriorImage.IsAtEnd() )
{
PosteriorsPixelType posteriorPixel = itrPosteriorImage.Get();
posteriorPixel[componentToExtract] = sit.Get();
itrPosteriorImage.Set(posteriorPixel);
++sit;
++itrPosteriorImage;
}
}
}
}
/**
* Compute the labeled map based on the Maximum rule applied to the posteriors.
*/
template < class TInputVectorImage, class TLabelsType,
class TPosteriorsPrecisionType, class TPriorsPrecisionType >
void
BayesianClassifierImageFilter<TInputVectorImage, TLabelsType,
TPosteriorsPrecisionType, TPriorsPrecisionType >
::ClassifyBasedOnPosteriors()
{
OutputImagePointer labels = this->GetOutput();
ImageRegionType imageRegion = labels->GetBufferedRegion();
PosteriorsImageType * posteriorsImage =
dynamic_cast< PosteriorsImageType * >( this->GetPosteriorImage() );
if( posteriorsImage == NULL )
{
itkExceptionMacro("Second output type does not correspond to expected Posteriors Image Type");
}
OutputImageIteratorType itrLabelsImage( labels, imageRegion );
PosteriorsImageIteratorType itrPosteriorsImage( posteriorsImage,imageRegion );
DecisionRulePointer decisionRule = DecisionRuleType::New();
itrLabelsImage.GoToBegin();
itrPosteriorsImage.GoToBegin();
while ( !itrLabelsImage.IsAtEnd() )
{
itrLabelsImage.Set( static_cast< TLabelsType >(
decisionRule->Evaluate( itrPosteriorsImage.Get())) );
++itrLabelsImage;
++itrPosteriorsImage;
}
}
} // end namespace itk
#endif