forked from InsightSoftwareConsortium/ITK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GibbsPriorImageFilter1.cxx
333 lines (261 loc) · 11 KB
/
GibbsPriorImageFilter1.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
327
328
329
330
331
332
333
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: GibbsPriorImageFilter1.cxx
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.
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.
=========================================================================*/
#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif
#ifdef __BORLANDC__
#define ITK_LEAN_AND_MEAN
#endif
// Software Guide : BeginLatex
//
// This example illustrates the use of the \doxygen{RGBGibbsPriorFilter}.
// The filter outputs a binary segmentation that can be improved by the
// deformable model. It is the first part of our hybrid framework.
//
// First, we include the appropriate header file.
//
// Software Guide : EndLatex
#include <iostream>
#include <string>
#include <math.h>
// Software Guide : BeginCodeSnippet
#include "itkRGBGibbsPriorFilter.h"
// Software Guide : EndCodeSnippet
// classes help the Gibbs filter to segment the image
#include "itkImageClassifierBase.h"
#include "itkImageGaussianModelEstimator.h"
#include "itkMahalanobisDistanceMembershipFunction.h"
#include "itkMinimumDecisionRule.h"
// image storage and I/O classes
#include "itkSize.h"
#include "itkImage.h"
#include "itkVector.h"
#include "itkImageRegionIterator.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#define NUM_CLASSES 3
#define MAX_NUM_ITER 1
int main( int argc, char *argv[] )
{
if( argc != 4 )
{
std::cerr << "Missing Parameters " << std::endl;
std::cerr << "Usage: " << argv[0];
std::cerr << " inputImage trainimage outputImage" << std::endl;
return 1;
}
std::cout<< "Gibbs Prior Test Begins: " << std::endl;
// Software Guide : BeginLatex
//
// The input is a single channel 2D image; the channel number is
// \code{NUMBANDS} = 1, and \code{NDIMENSION} is set to 3.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
const unsigned short NUMBANDS = 1;
const unsigned short NDIMENSION = 3;
typedef itk::Image<itk::Vector<unsigned short,NUMBANDS>,NDIMENSION> VecImageType;
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The Gibbs prior segmentation is performed first to generate a rough
// segmentation that yields a sample of tissue from a region to be
// segmented, which will be combined to form the input for the
// isocontouring method. We define the pixel type of the output of the
// Gibbs prior filter to be \code{unsigned short}.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef itk::Image< unsigned short, NDIMENSION > ClassImageType;
// Software Guide : EndCodeSnippet
// We instantiate reader and writer types
//
typedef itk::ImageFileReader< ClassImageType > ReaderType;
typedef itk::ImageFileWriter< ClassImageType > WriterType;
ReaderType::Pointer inputimagereader = ReaderType::New();
ReaderType::Pointer trainingimagereader = ReaderType::New();
WriterType::Pointer writer = WriterType::New();
inputimagereader->SetFileName( argv[1] );
trainingimagereader->SetFileName( argv[2] );
writer->SetFileName( argv[3] );
// We convert the input into vector images
//
VecImageType::Pointer vecImage = VecImageType::New();
typedef VecImageType::PixelType VecImagePixelType;
VecImageType::SizeType vecImgSize = { {181 , 217, 1} };
VecImageType::IndexType index;
index.Fill(0);
VecImageType::RegionType region;
region.SetSize( vecImgSize );
region.SetIndex( index );
vecImage->SetLargestPossibleRegion( region );
vecImage->SetBufferedRegion( region );
vecImage->Allocate();
// setup the iterators
typedef VecImageType::PixelType::VectorType VecPixelType;
enum { VecImageDimension = VecImageType::ImageDimension };
typedef itk::ImageRegionIterator< VecImageType > VecIterator;
VecIterator vecIt( vecImage, vecImage->GetBufferedRegion() );
vecIt.GoToBegin();
inputimagereader->Update();
trainingimagereader->Update();
typedef itk::ImageRegionIterator< ClassImageType > ClassIterator;
ClassIterator inputIt( inputimagereader->GetOutput(), inputimagereader->GetOutput()->GetBufferedRegion() );
inputIt.GoToBegin();
//Set up the vector to store the image data
typedef VecImageType::PixelType DataVector;
DataVector dblVec;
while ( !vecIt.IsAtEnd() )
{
dblVec[0] = inputIt.Get();
vecIt.Set(dblVec);
++vecIt;
++inputIt;
}
//----------------------------------------------------------------------
//Set membership function (Using the statistics objects)
//----------------------------------------------------------------------
namespace stat = itk::Statistics;
typedef VecImageType::PixelType VecImagePixelType;
typedef stat::MahalanobisDistanceMembershipFunction< VecImagePixelType >
MembershipFunctionType;
typedef MembershipFunctionType::Pointer MembershipFunctionPointer;
typedef std::vector< MembershipFunctionPointer >
MembershipFunctionPointerVector;
//----------------------------------------------------------------------
// Set the image model estimator (train the class models)
//----------------------------------------------------------------------
typedef itk::ImageGaussianModelEstimator<VecImageType,
MembershipFunctionType, ClassImageType>
ImageGaussianModelEstimatorType;
ImageGaussianModelEstimatorType::Pointer
applyEstimateModel = ImageGaussianModelEstimatorType::New();
applyEstimateModel->SetNumberOfModels(NUM_CLASSES);
applyEstimateModel->SetInputImage(vecImage);
applyEstimateModel->SetTrainingImage(trainingimagereader->GetOutput());
//Run the gaussian classifier algorithm
applyEstimateModel->Update();
std::cout << " site 1 " << std::endl;
applyEstimateModel->Print(std::cout);
MembershipFunctionPointerVector membershipFunctions =
applyEstimateModel->GetMembershipFunctions();
std::cout << " site 2 " << std::endl;
//----------------------------------------------------------------------
//Set the decision rule
//----------------------------------------------------------------------
typedef itk::DecisionRuleBase::Pointer DecisionRuleBasePointer;
typedef itk::MinimumDecisionRule DecisionRuleType;
DecisionRuleType::Pointer myDecisionRule = DecisionRuleType::New();
std::cout << " site 3 " << std::endl;
//----------------------------------------------------------------------
// Set the classifier to be used and assigne the parameters for the
// supervised classifier algorithm except the input image which is
// grabbed from the Gibbs application pipeline.
//----------------------------------------------------------------------
//---------------------------------------------------------------------
typedef VecImagePixelType MeasurementVectorType;
// Software Guide : BeginLatex
//
// Then we define the classifier that is needed
// for the Gibbs prior model to make correct segmenting decisions.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef itk::ImageClassifierBase< VecImageType, ClassImageType > ClassifierType;
typedef itk::ClassifierBase<VecImageType>::Pointer ClassifierBasePointer;
typedef ClassifierType::Pointer ClassifierPointer;
ClassifierPointer myClassifier = ClassifierType::New();
// Software Guide : EndCodeSnippet
// Set the Classifier parameters
myClassifier->SetNumberOfClasses(NUM_CLASSES);
// Set the decison rule
myClassifier->SetDecisionRule((DecisionRuleBasePointer) myDecisionRule );
//Add the membership functions
for( unsigned int i=0; i<NUM_CLASSES; i++ )
{
myClassifier->AddMembershipFunction( membershipFunctions[i] );
}
//Set the Gibbs Prior labeller
// Software Guide : BeginLatex
//
// After that we can define the multi-channel Gibbs prior model.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef itk::RGBGibbsPriorFilter<VecImageType,ClassImageType>
GibbsPriorFilterType;
GibbsPriorFilterType::Pointer applyGibbsImageFilter =
GibbsPriorFilterType::New();
// Software Guide : EndCodeSnippet
// Set the MRF labeller parameters
// Software Guide : BeginLatex
//
// The parameters for the Gibbs prior filter are defined
// below. \code{NumberOfClasses} indicates how many different objects are in
// the image. The maximum number of iterations is the number of
// minimization steps. \code{ClusterSize} sets the lower limit on the
// object's size. The boundary gradient is the estimate of the variance
// between objects and background at the boundary region.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
applyGibbsImageFilter->SetNumberOfClasses(NUM_CLASSES);
applyGibbsImageFilter->SetMaximumNumberOfIterations(MAX_NUM_ITER);
applyGibbsImageFilter->SetClusterSize(10);
applyGibbsImageFilter->SetBoundaryGradient(6);
applyGibbsImageFilter->SetObjectLabel(1);
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// We now set the input classifier for the Gibbs prior filter and the
// input to the classifier. The classifier will calculate the mean and
// variance of the object using the class image, and the results will be
// used as parameters for the Gibbs prior model.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
applyGibbsImageFilter->SetInput(vecImage);
applyGibbsImageFilter->SetClassifier( myClassifier );
applyGibbsImageFilter->SetTrainingImage(trainingimagereader->GetOutput());
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// Finally we execute the Gibbs prior filter using the Update() method.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
applyGibbsImageFilter->Update();
// Software Guide : EndCodeSnippet
std::cout << "applyGibbsImageFilter: " << applyGibbsImageFilter;
writer->SetInput( applyGibbsImageFilter->GetOutput() );
writer->Update();
// Software Guide : BeginLatex
//
// We execute this program on the image \code{brainweb89.png}. The
// following parameters are passed to the command line:
//
// \small
// \begin{verbatim}
//GibbsGuide.exe brainweb89.png brainweb89_train.png brainweb_gp.png
// \end{verbatim}
// \normalsize
//
// \code{brainweb89train} is a training image that helps to estimate the object statistics.
//
// Note that in order to successfully segment other images, one has to
// create suitable training images for them. We can also segment color
// (RGB) and other multi-channel images.
//
// Software Guide : EndLatex
return 0;
}