File: BayesianClassifierInitializer.cxx

package info (click to toggle)
insighttoolkit5 5.4.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 704,588 kB
  • sloc: cpp: 784,579; ansic: 628,724; xml: 44,704; fortran: 34,250; python: 22,934; sh: 4,078; pascal: 2,636; lisp: 2,158; makefile: 461; yacc: 328; asm: 205; perl: 203; lex: 146; tcl: 132; javascript: 98; csh: 81
file content (187 lines) | stat: -rw-r--r-- 6,556 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
/*=========================================================================
 *
 *  Copyright NumFOCUS
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *         https://www.apache.org/licenses/LICENSE-2.0.txt
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *=========================================================================*/

//
// This is an example of the itk::BayesianClassifierInitializationImageFilter.
// The example's goal is to serve as an initializer for the
// BayesianClassifier.cxx example also found in this directory.
//
// This example takes an input image (to be classified) and generates
// membership images. The membership images determine the degree to which each
// pixel belongs to a class.
//
// The membership image generated by the filter is an
// itk::VectorImage, (with pixels organized as follows: For a 2D image,
// its essentially a 3D array on file with DataType[y][x][c] where c is the
// number of classes and DataType is the template parameter of the filter
// (defaults to float). For a 3D image, it will be organized as
// Datatype[z][y][x][c])
//
// The example also optionally takes in two more arguments, as a convenience
// to the user. These arguments extract the specified component 'c' from the
// membership image and rescale, so the user can fire up a typical image
// viewer and see the relative pixel memberships to class 'c'.
//
// Example args:
//   BrainProtonDensitySlice.png Memberships.mhd 4  2 Class2.png
//
// Here Memberships.mhd will be a 2x2x4 image containing pixel memberships
// Class2.png shows pixel memberships to the third class, (rescaled for
// display)
//
// Notes:
//   The default behaviour of the filter is to generate memberships by
//   centering
// gaussian density functions around K-means of the pixel intensities in the
// image. The filter allows you to specify your own membership functions as
// well.
//

#include "itkImage.h"
#include "itkBayesianClassifierInitializationImageFilter.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkRescaleIntensityImageFilter.h"
#include "itkImageRegionConstIterator.h"

int
main(int argc, char * argv[])
{

  constexpr unsigned int Dimension = 2;
  if (argc < 4)
  {
    std::cerr
      << "Usage arguments: InputImage MembershipImage numberOfClasses "
         "[componentToExtract ExtractedImage]"
      << std::endl;
    std::cerr
      << "  The MembershipImage image written is a VectorImage, ( an image "
         "with multiple components ) ";
    std::cerr << "Given that most viewers can't see vector images, we will "
                 "optionally "
                 "extract a component and ";
    std::cerr << "write it out as a scalar image as well." << std::endl;
    return EXIT_FAILURE;
  }

  using ImageType = itk::Image<unsigned char, Dimension>;
  using BayesianInitializerType =
    itk::BayesianClassifierInitializationImageFilter<ImageType>;
  auto bayesianInitializer = BayesianInitializerType::New();

  using ReaderType = itk::ImageFileReader<ImageType>;
  auto reader = ReaderType::New();
  reader->SetFileName(argv[1]);

  try
  {
    reader->Update();
  }
  catch (const itk::ExceptionObject & excp)
  {
    std::cerr << "Exception thrown " << std::endl;
    std::cerr << excp << std::endl;
    return EXIT_FAILURE;
  }

  bayesianInitializer->SetInput(reader->GetOutput());
  bayesianInitializer->SetNumberOfClasses(std::stoi(argv[3]));

  // TODO add test where we specify membership functions

  using WriterType =
    itk::ImageFileWriter<BayesianInitializerType::OutputImageType>;
  auto writer = WriterType::New();
  writer->SetInput(bayesianInitializer->GetOutput());
  writer->SetFileName(argv[2]);

  try
  {
    bayesianInitializer->Update();
  }
  catch (const itk::ExceptionObject & excp)
  {
    std::cerr << "Exception thrown " << std::endl;
    std::cerr << excp << std::endl;
    return EXIT_FAILURE;
  }

  try
  {
    writer->Update();
  }
  catch (const itk::ExceptionObject & excp)
  {
    std::cerr << "Exception thrown " << std::endl;
    std::cerr << excp << std::endl;
    return EXIT_FAILURE;
  }

  if (argv[4] && argv[5])
  {
    using MembershipImageType = BayesianInitializerType::OutputImageType;
    using ExtractedComponentImageType =
      itk::Image<MembershipImageType::InternalPixelType, Dimension>;
    auto extractedComponentImage = ExtractedComponentImageType::New();
    extractedComponentImage->CopyInformation(
      bayesianInitializer->GetOutput());
    extractedComponentImage->SetBufferedRegion(
      bayesianInitializer->GetOutput()->GetBufferedRegion());
    extractedComponentImage->SetRequestedRegion(
      bayesianInitializer->GetOutput()->GetRequestedRegion());
    extractedComponentImage->Allocate();
    using ConstIteratorType =
      itk::ImageRegionConstIterator<MembershipImageType>;
    using IteratorType =
      itk::ImageRegionIterator<ExtractedComponentImageType>;
    ConstIteratorType cit(
      bayesianInitializer->GetOutput(),
      bayesianInitializer->GetOutput()->GetBufferedRegion());
    IteratorType it(extractedComponentImage,
                    extractedComponentImage->GetLargestPossibleRegion());

    const unsigned int componentToExtract = std::stoi(argv[4]);
    cit.GoToBegin();
    it.GoToBegin();
    while (!cit.IsAtEnd())
    {
      it.Set(cit.Get()[componentToExtract]);
      ++it;
      ++cit;
    }

    // Write out the rescaled extracted component
    using OutputImageType = itk::Image<unsigned char, Dimension>;
    using RescalerType =
      itk::RescaleIntensityImageFilter<ExtractedComponentImageType,
                                       OutputImageType>;
    auto rescaler = RescalerType::New();
    rescaler->SetInput(extractedComponentImage);
    rescaler->SetOutputMinimum(0);
    rescaler->SetOutputMaximum(255);
    using ExtractedComponentWriterType =
      itk::ImageFileWriter<OutputImageType>;
    auto rescaledImageWriter = ExtractedComponentWriterType::New();
    rescaledImageWriter->SetInput(rescaler->GetOutput());
    rescaledImageWriter->SetFileName(argv[5]);
    rescaledImageWriter->Update();
  }

  return EXIT_SUCCESS;
}