File: FuzzyConnectednessImageFilter.cxx

package info (click to toggle)
insighttoolkit 3.20.1%2Bgit20120521-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 80,652 kB
  • sloc: cpp: 458,133; ansic: 196,223; fortran: 28,000; python: 3,839; tcl: 1,811; sh: 1,184; java: 583; makefile: 430; csh: 220; perl: 193; xml: 20
file content (281 lines) | stat: -rw-r--r-- 9,857 bytes parent folder | download | duplicates (2)
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
/*=========================================================================

  Program:   Insight Segmentation & Registration Toolkit
  Module:    FuzzyConnectednessImageFilter.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.

=========================================================================*/
#ifdef _MSC_VER
#pragma warning ( disable : 4786 )
#endif

// Software Guide : BeginLatex
//
// This example illustrates the use of the
// \doxygen{SimpleFuzzyConnectednessScalarImageFilter}. This filter computes an
// affinity map from a seed point provided by the user. This affinity map
// indicates for every pixels how homogeneous is the path that will link it to
// the seed point.
//
// Please note that the Fuzzy Connectedness algorithm is covered by a patent
// \cite{Udupa1998}. For this reason the current example is located in the
// \texttt{Examples/Patented} subdirectory.
//
// In order to use this algorithm we should first include the header files of
// the filter and the image class.
//
// Software Guide : EndLatex 


// Software Guide : BeginCodeSnippet
#include "itkSimpleFuzzyConnectednessScalarImageFilter.h"
#include "itkImage.h"
// Software Guide : EndCodeSnippet


// Software Guide : BeginLatex
//
// Since the FuzzyConnectednessImageFilter requires an estimation of the
// gray level mean and variance for the region to be segmented, we use here the
// \doxygen{ConfidenceConnectedImageFilter} as a preprocessor that produces a
// rough segmentation and estimates from it the values of the mean and the
// variance.
//
// Software Guide : EndLatex 

// Software Guide : BeginCodeSnippet
#include "itkConfidenceConnectedImageFilter.h"
// Software Guide : EndCodeSnippet

#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"


int main( int argc, char *argv[] )
{
  if( argc < 7 )
    {
    std::cerr << "Missing Parameters " << std::endl;
    std::cerr << "Usage: " << argv[0];
    std::cerr << " inputImage outputImage outputAffinityMap " << std::endl;
    std::cerr << " seedX seedY multiplier " << std::endl;
    return 1;
    }


  //  Software Guide : BeginLatex
  //  
  //  Next, we declare the pixel type and image dimension and 
  //  specify the image type to be used as input.
  //
  //  Software Guide : EndLatex 

  // Software Guide : BeginCodeSnippet
  typedef  float            InputPixelType;
  const    unsigned int     Dimension = 2;
  typedef itk::Image< InputPixelType, Dimension >  InputImageType;
  // Software Guide : EndCodeSnippet

  //  Software Guide : BeginLatex
  //  
  //  Fuzzy connectedness computes first the affinity map and then thresholds
  //  its values in order to get a binary image as output. The type of the
  //  binary image is provided as the second template parameter of the filter.
  //
  //  Software Guide : EndLatex 

  // Software Guide : BeginCodeSnippet
  typedef   unsigned char                          BinaryPixelType;
  typedef itk::Image< BinaryPixelType, Dimension > BinaryImageType;
  // Software Guide : EndCodeSnippet

  //  Software Guide : BeginLatex
  //  
  //  The Confidence connected filter type is instantiated using the input
  //  image type and a binary image type for output.
  //
  //  Software Guide : EndLatex 

  // Software Guide : BeginCodeSnippet
  typedef itk::ConfidenceConnectedImageFilter< 
                                                  InputImageType, 
                                                  BinaryImageType 
                                                    >  ConfidenceConnectedFilterType;

  ConfidenceConnectedFilterType::Pointer confidenceConnectedFilter = 
                                                 ConfidenceConnectedFilterType::New();
  // Software Guide : EndCodeSnippet

  //  Software Guide : BeginLatex
  //  
  //  The fuzzy segmentation filter type is instantiated here using the input
  //  and binary image types as template parameters.
  //
  //  Software Guide : EndLatex 

  // Software Guide : BeginCodeSnippet
  typedef itk::SimpleFuzzyConnectednessScalarImageFilter< 
                                                  InputImageType, 
                                                  BinaryImageType 
                                                    >  FuzzySegmentationFilterType;
  // Software Guide : EndCodeSnippet


  //  Software Guide : BeginLatex
  //  
  //  The fuzzy connectedness segmentation filter is created by invoking the
  //  \code{New()} method and assigning the result to a
  //  \doxygen{SmartPointer}.
  //
  //  \index{itk::SimpleFuzzy\-Connectedness\-Scalar\-Image\-Filter!New()}
  //  \index{itk::SimpleFuzzy\-Connectedness\-Scalar\-Image\-Filter!Pointer}
  //
  //  Software Guide : EndLatex 

  // Software Guide : BeginCodeSnippet
  FuzzySegmentationFilterType::Pointer fuzzysegmenter = 
                                         FuzzySegmentationFilterType::New();
  // Software Guide : EndCodeSnippet

  //  Software Guide : BeginLatex
  //  
  //  The affinity map can be accessed through the type \code{FuzzySceneType}
  //
  //  Software Guide : EndLatex 
  
  // Software Guide : BeginCodeSnippet
  typedef FuzzySegmentationFilterType::FuzzySceneType  FuzzySceneType; 
  // Software Guide : EndCodeSnippet

  //  Software Guide : BeginLatex
  //  
  // We instantiate reader and writer types
  //
  //  Software Guide : EndLatex 
  typedef  itk::ImageFileReader< InputImageType  >    ReaderType;
  typedef  itk::ImageFileWriter< BinaryImageType >    WriterType;
  typedef  itk::ImageFileWriter< FuzzySceneType  >    FuzzyWriterType;

  ReaderType::Pointer reader = ReaderType::New();
  WriterType::Pointer writer = WriterType::New();

  FuzzyWriterType::Pointer fwriter = FuzzyWriterType::New();

  reader->SetFileName(  argv[1] );
  writer->SetFileName(  argv[2] );
  fwriter->SetFileName( argv[3] );

 
  InputImageType::IndexType index;

  index[0] = atoi(argv[4]);
  index[1] = atoi(argv[5]);

  const double varianceMultiplier = atof( argv[6] );

  //  Software Guide : BeginLatex
  //  
  //  The output of the reader is passed as input to the ConfidenceConnected image filter.
  //  Then the filter is executed in order to obtain estimations of the mean and variance
  //  gray values for the region to be segmented.
  //
  //  Software Guide : EndLatex 
  
  // Software Guide : BeginCodeSnippet
  confidenceConnectedFilter->SetInput( reader->GetOutput()  );
  confidenceConnectedFilter->SetMultiplier( varianceMultiplier );
  confidenceConnectedFilter->SetNumberOfIterations( 2 );
  confidenceConnectedFilter->AddSeed( index );

  confidenceConnectedFilter->Update();
  // Software Guide : EndCodeSnippet


  WriterType::Pointer confidenceWriter = WriterType::New();
  confidenceWriter->SetInput( confidenceConnectedFilter->GetOutput() );
  confidenceWriter->SetFileName("confidenceConnectedPreprocessing.png");
  confidenceWriter->Update();


  //  Software Guide : BeginLatex
  //  
  //  The input that is passed to the fuzzy segmentation filter is taken from
  //  the reader.
  //
  //  \index{itk::Simple\-Fuzzy\-Connectedness\-Scalar\-Image\-Filter!SetInput()}
  //
  //  Software Guide : EndLatex 
  
  // Software Guide : BeginCodeSnippet
  fuzzysegmenter->SetInput( reader->GetOutput() );
  // Software Guide : EndCodeSnippet

  const double  meanEstimation      = confidenceConnectedFilter->GetMean();
  const double  varianceEstimation  = confidenceConnectedFilter->GetVariance();

  std::cout << "Mean     estimation = " << meanEstimation     << std::endl;
  std::cout << "Variance estimation = " << varianceEstimation << std::endl;


  //  Software Guide : BeginLatex
  //  
  //  The parameters of the fuzzy segmentation filter are defined here. A seed
  //  point is provided with the method \code{SetObjectsSeed()} in order to
  //  initialize the region to be grown.  Estimated values for the mean and
  //  variance of the object intensities are also provided with the methods
  //  \code{SetMean()} and \code{SetVariance()}, respectively. A threshold
  //  value for generating the binary object is preset with the method
  //  \code{SetThreshold()}.  For details describing the role of the mean and
  //  variance on the computation of the segmentation, please see
  //  \cite{Udupa1996}. 
  //
  //  \index{itk::Simple\-Fuzzy\-Connectedness\-Scalar\-Image\-Filter!SetObjectsSeed()}
  //  \index{itk::Simple\-Fuzzy\-Connectedness\-Scalar\-Image\-Filter!SetMean()}
  //  \index{itk::Simple\-Fuzzy\-Connectedness\-Scalar\-Image\-Filter!SetVariance()}
  //  \index{itk::Simple\-Fuzzy\-Connectedness\-Scalar\-Image\-Filter!SetThreshold()}
  //
  //  Software Guide : EndLatex 

  // Software Guide : BeginCodeSnippet
  fuzzysegmenter->SetObjectSeed( index );
  fuzzysegmenter->SetMean( meanEstimation );
  fuzzysegmenter->SetVariance( varianceEstimation );
  fuzzysegmenter->SetThreshold( 0.5 );
  // Software Guide : EndCodeSnippet


  //  Software Guide : BeginLatex
  //  
  //  The execution of the fuzzy segmentation filter is triggered by the
  //  \code{Update()} method.
  //
  //  Software Guide : EndLatex 

  // Software Guide : BeginCodeSnippet
  fuzzysegmenter->Update();
  // Software Guide : EndCodeSnippet


  // Software Guide : BeginCodeSnippet
  writer->SetInput( fuzzysegmenter->GetOutput() );
  writer->Update();
  // Software Guide : EndCodeSnippet


  // Software Guide : BeginCodeSnippet
  fwriter->SetInput( fuzzysegmenter->GetFuzzyScene() );
  fwriter->Update();
  // Software Guide : EndCodeSnippet


  return 0;
}