File: itkConvertLabelMapFilterTest2.cxx

package info (click to toggle)
insighttoolkit5 5.4.3-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 704,384 kB
  • sloc: cpp: 783,592; ansic: 628,724; xml: 44,704; fortran: 34,250; python: 22,874; sh: 4,078; pascal: 2,636; lisp: 2,158; makefile: 464; yacc: 328; asm: 205; perl: 203; lex: 146; tcl: 132; javascript: 98; csh: 81
file content (275 lines) | stat: -rw-r--r-- 13,481 bytes parent folder | download
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
/*=========================================================================
 *
 *  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.
 *
 *=========================================================================*/
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"

#include "itkLabelImageToLabelMapFilter.h"
#include "itkLabelImageToShapeLabelMapFilter.h"
#include "itkLabelImageToStatisticsLabelMapFilter.h"
#include "itkConvertLabelMapFilter.h"
#include "itkTestingMacros.h"
#include "itkSimpleFilterWatcher.h"

int
itkConvertLabelMapFilterTest2(int argc, char * argv[])
{
  if (argc != 3)
  {
    std::cerr << "Missing parameters." << std::endl;
    std::cerr << "Usage: " << itkNameOfTestExecutableMacro(argv);
    std::cerr << " inputLabelImage outputLabelImage";
    std::cerr << std::endl;
    return EXIT_FAILURE;
  }

  constexpr unsigned int dim = 2;

  using InputPixelType = unsigned char;
  using InputImageType = itk::Image<InputPixelType, dim>;

  using OutputPixelType = unsigned short;
  using OutputImageType = itk::Image<OutputPixelType, dim>;

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

  //////////////////////////////////////////////////////////////////////////////////
  // test ShapeLabelMap conversion: unsigned char type -> unsigned short type
  //////////////////////////////////////////////////////////////////////////////////
  // convert label image to ShapeLabelMap
  using L2SType = itk::LabelImageToShapeLabelMapFilter<InputImageType>;
  auto l2s = L2SType::New();
  l2s->SetInput(reader->GetOutput());
  l2s->Update();
  // convert ShapeLabelMap from unsigned char type to unsigned short type
  using InputShapeLabelMapType = L2SType::OutputImageType;
  using OutputShapeLabelMapType = itk::LabelMap<itk::ShapeLabelObject<unsigned short, dim>>;
  using CastShapeType = itk::ConvertLabelMapFilter<InputShapeLabelMapType, OutputShapeLabelMapType>;
  auto castShape = CastShapeType::New();
  castShape->SetInput(l2s->GetOutput());
  castShape->Update();
  // verify ShapeLabelMap contains equivalent information after conversion
  bool shapeLabelMapIsKeptSame = true;
  if (l2s->GetOutput()->GetNumberOfLabelObjects() !=
      castShape->GetOutput()->GetNumberOfLabelObjects()) // verify two label maps have the same number of label objects
  {
    shapeLabelMapIsKeptSame = false;
  }
  for (InputShapeLabelMapType::ConstIterator it(l2s->GetOutput()); !it.IsAtEnd(); ++it)
  {
    const InputShapeLabelMapType::LabelObjectType * labelObject = it.GetLabelObject();
    InputShapeLabelMapType::LabelType               label = labelObject->GetLabel();

    if (!castShape->GetOutput()->HasLabel(label)) // verify label first
    {
      shapeLabelMapIsKeptSame = false;
    }
    else
    {
      const OutputShapeLabelMapType::LabelObjectType * newLabelObject =
        castShape->GetOutput()->GetLabelObject(label); // find output label object with the same label
      // verify voxels are the same
      // if number of pixels are the same, AND all pixels in inputLabelObject are also in outputLabelObject, then pixels
      // are kept the same
      if (labelObject->Size() != newLabelObject->Size())
      {
        shapeLabelMapIsKeptSame = false;
      }
      for (unsigned int pixelId = 0; pixelId < labelObject->Size(); ++pixelId)
      {
        if (!newLabelObject->HasIndex(labelObject->GetIndex(pixelId)))
        {
          shapeLabelMapIsKeptSame = false;
          break;
        }
      }
      // verify shape information
      if (newLabelObject->GetBoundingBox() != labelObject->GetBoundingBox() ||
          newLabelObject->GetCentroid() != labelObject->GetCentroid() ||
          newLabelObject->GetElongation() != labelObject->GetElongation() ||
          newLabelObject->GetEquivalentEllipsoidDiameter() != labelObject->GetEquivalentEllipsoidDiameter() ||
          newLabelObject->GetEquivalentSphericalPerimeter() != labelObject->GetEquivalentSphericalPerimeter() ||
          newLabelObject->GetEquivalentSphericalRadius() != labelObject->GetEquivalentSphericalRadius() ||
          newLabelObject->GetFeretDiameter() != labelObject->GetFeretDiameter() ||
          newLabelObject->GetFlatness() != labelObject->GetFlatness() ||
          newLabelObject->GetNumberOfPixels() != labelObject->GetNumberOfPixels() ||
          newLabelObject->GetNumberOfPixelsOnBorder() != labelObject->GetNumberOfPixelsOnBorder() ||
          newLabelObject->GetPerimeter() != labelObject->GetPerimeter() ||
          newLabelObject->GetPerimeterOnBorder() != labelObject->GetPerimeterOnBorder() ||
          newLabelObject->GetPerimeterOnBorderRatio() != labelObject->GetPerimeterOnBorderRatio() ||
          newLabelObject->GetPhysicalSize() != labelObject->GetPhysicalSize() ||
          newLabelObject->GetPrincipalAxes() != labelObject->GetPrincipalAxes() ||
          newLabelObject->GetPrincipalMoments() != labelObject->GetPrincipalMoments() ||
          newLabelObject->GetRoundness() != labelObject->GetRoundness())
      {
        shapeLabelMapIsKeptSame = false;
      }
    }

    // if one labelObject is not kept the same, then just exit program with failure
    if (!shapeLabelMapIsKeptSame)
    {
      std::cerr << "shapeLabelMapIsKeptSame is false" << std::endl;
      return EXIT_FAILURE;
    }
  }
  std::cerr << "ShapeLabelMap test is finished and passed" << std::endl;

  //////////////////////////////////////////////////////////////////////////////////
  // test StatisticsLabelMap conversion: unsigned char type -> unsigned short type
  //////////////////////////////////////////////////////////////////////////////////
  // convert label image to StatisticsLabelMap
  using L2StatType = itk::LabelImageToStatisticsLabelMapFilter<InputImageType, InputImageType>;
  auto l2stat = L2StatType::New();
  l2stat->SetInput(reader->GetOutput());
  l2stat->SetFeatureImage(reader->GetOutput()); // use input label image itself as the feature image
  l2stat->Update();
  // convert StatisticsLabelMap from unsigned char type to unsigned short type
  using InputStatisticsLabelMapType = L2StatType::OutputImageType;
  using OutputStatisticsLabelMapType = itk::LabelMap<itk::StatisticsLabelObject<unsigned short, dim>>;
  using CastStatType = itk::ConvertLabelMapFilter<InputStatisticsLabelMapType, OutputStatisticsLabelMapType>;
  auto castStatistics = CastStatType::New();
  castStatistics->SetInput(l2stat->GetOutput());
  castStatistics->Update();
  // verify StatisticsLabelMap contains equivalent information after conversion
  bool statisticsLabelMapIsKeptSame = true;
  if (l2stat->GetOutput()->GetNumberOfLabelObjects() !=
      castStatistics->GetOutput()
        ->GetNumberOfLabelObjects()) // verify two label maps have the same number of label objects
  {
    statisticsLabelMapIsKeptSame = false;
  }
  for (InputStatisticsLabelMapType::ConstIterator it(l2stat->GetOutput()); !it.IsAtEnd(); ++it)
  {
    const InputStatisticsLabelMapType::LabelObjectType * labelObject = it.GetLabelObject();
    InputStatisticsLabelMapType::LabelType               label = labelObject->GetLabel();

    if (!castStatistics->GetOutput()->HasLabel(label)) // verify label first
    {
      statisticsLabelMapIsKeptSame = false;
    }
    else
    {
      const OutputStatisticsLabelMapType::LabelObjectType * newLabelObject =
        castStatistics->GetOutput()->GetLabelObject(label); // find output label object with the same label
      // verify voxels are the same
      // if number of pixels are the same, AND all pixels in inputLabelObject are also in outputLabelObject, then pixels
      // are kept the same
      if (labelObject->Size() != newLabelObject->Size())
      {
        statisticsLabelMapIsKeptSame = false;
      }
      for (unsigned int pixelId = 0; pixelId < labelObject->Size(); ++pixelId)
      {
        if (!newLabelObject->HasIndex(labelObject->GetIndex(pixelId)))
        {
          statisticsLabelMapIsKeptSame = false;
          break;
        }
      }
      // verify shape information inherited from ShapeLabelObject
      if (newLabelObject->GetBoundingBox() != labelObject->GetBoundingBox() ||
          newLabelObject->GetCentroid() != labelObject->GetCentroid() ||
          newLabelObject->GetElongation() != labelObject->GetElongation() ||
          newLabelObject->GetEquivalentEllipsoidDiameter() != labelObject->GetEquivalentEllipsoidDiameter() ||
          newLabelObject->GetEquivalentSphericalPerimeter() != labelObject->GetEquivalentSphericalPerimeter() ||
          newLabelObject->GetEquivalentSphericalRadius() != labelObject->GetEquivalentSphericalRadius() ||
          newLabelObject->GetFeretDiameter() != labelObject->GetFeretDiameter() ||
          newLabelObject->GetFlatness() != labelObject->GetFlatness() ||
          newLabelObject->GetNumberOfPixels() != labelObject->GetNumberOfPixels() ||
          newLabelObject->GetNumberOfPixelsOnBorder() != labelObject->GetNumberOfPixelsOnBorder() ||
          newLabelObject->GetPerimeter() != labelObject->GetPerimeter() ||
          newLabelObject->GetPerimeterOnBorder() != labelObject->GetPerimeterOnBorder() ||
          newLabelObject->GetPerimeterOnBorderRatio() != labelObject->GetPerimeterOnBorderRatio() ||
          newLabelObject->GetPhysicalSize() != labelObject->GetPhysicalSize() ||
          newLabelObject->GetPrincipalAxes() != labelObject->GetPrincipalAxes() ||
          newLabelObject->GetPrincipalMoments() != labelObject->GetPrincipalMoments() ||
          newLabelObject->GetRoundness() != labelObject->GetRoundness())
      {
        statisticsLabelMapIsKeptSame = false;
      }
      // verify statistics information
      if (newLabelObject->GetCenterOfGravity() != labelObject->GetCenterOfGravity() ||
          newLabelObject->GetHistogram() != labelObject->GetHistogram() ||
          newLabelObject->GetKurtosis() != labelObject->GetKurtosis() ||
          newLabelObject->GetMaximum() != labelObject->GetMaximum() ||
          newLabelObject->GetMaximumIndex() != labelObject->GetMaximumIndex() ||
          newLabelObject->GetMean() != labelObject->GetMean() ||
          newLabelObject->GetMedian() != labelObject->GetMedian() ||
          newLabelObject->GetMinimum() != labelObject->GetMinimum() ||
          newLabelObject->GetMinimumIndex() != labelObject->GetMinimumIndex() ||
          newLabelObject->GetSkewness() != labelObject->GetSkewness() ||
          newLabelObject->GetStandardDeviation() != labelObject->GetStandardDeviation() ||
          newLabelObject->GetSum() != labelObject->GetSum() ||
          newLabelObject->GetVariance() != labelObject->GetVariance() ||
          newLabelObject->GetWeightedElongation() != labelObject->GetWeightedElongation() ||
          newLabelObject->GetWeightedFlatness() != labelObject->GetWeightedFlatness() ||
          newLabelObject->GetWeightedPrincipalAxes() != labelObject->GetWeightedPrincipalAxes() ||
          newLabelObject->GetWeightedPrincipalMoments() != labelObject->GetWeightedPrincipalMoments())
      {
        statisticsLabelMapIsKeptSame = false;
      }
    }

    // if one labelObject is not kept the same, then just exit program with failure
    if (!statisticsLabelMapIsKeptSame)
    {
      std::cerr << "statisticsLabelMapIsKeptSame is false" << std::endl;
      return EXIT_FAILURE;
    }
  }
  std::cerr << "statisticsLabelMap test is finished and passed" << std::endl;

  //////////////////////////////////////////////////////////////////////////////////
  // test LabelMap conversion: unsigned char type -> unsigned short type
  //////////////////////////////////////////////////////////////////////////////////
  // convert label image to label map
  using L2MType = itk::LabelImageToLabelMapFilter<InputImageType>;
  auto l2m = L2MType::New();
  l2m->SetInput(reader->GetOutput());

  using LabelObjectType = itk::LabelObject<OutputPixelType, dim>;
  using LabelMapType = itk::LabelMap<LabelObjectType>;

  // convert label map from unsigned char to unsigned short type
  using CastType = itk::ConvertLabelMapFilter<L2MType::OutputImageType, LabelMapType>;
  auto cast = CastType::New();
  cast->SetInput(l2m->GetOutput());
  itk::SimpleFilterWatcher watcher(cast, "cast");

  using L2IType = itk::LabelMapToLabelImageFilter<LabelMapType, OutputImageType>;
  auto l2i = L2IType::New();
  l2i->SetInput(cast->GetOutput());

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

  ITK_TRY_EXPECT_NO_EXCEPTION(writer->Update());

  // for visual validation
  std::cout << " ============ original label map ============" << std::endl;
  l2m->GetOutput()->PrintLabelObjects();
  std::cout << " ============ casted label map ============" << std::endl;
  cast->GetOutput()->PrintLabelObjects();

  return EXIT_SUCCESS;
}