File: itkSpatialObjectToImageStatisticsCalculatorTest.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 (229 lines) | stat: -rw-r--r-- 7,221 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
/*=========================================================================
 *
 *  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 <iostream>
#include "itkSpatialObjectToImageStatisticsCalculator.h"
#include "itkSpatialObjectToImageFilter.h"
#include "itkEllipseSpatialObject.h"
#include "itkImageSliceIteratorWithIndex.h"
#include "itkTestingMacros.h"

int
itkSpatialObjectToImageStatisticsCalculatorTest(int, char *[])
{
  using PixelType = unsigned char;
  using ImageType = itk::Image<PixelType, 2>;
  using EllipseType = itk::EllipseSpatialObject<2>;

  // Image Definition
  ImageType::SizeType size;
  size.Fill(50);
  ImageType::SpacingType spacing;
  spacing.Fill(1);

  // Circle definition
  auto ellipse = EllipseType::New();
  ellipse->SetRadiusInObjectSpace(10);
  ellipse->Update();

  EllipseType::VectorType offset;
  offset.Fill(25);
  ellipse->GetModifiableObjectToParentTransform()->SetOffset(offset);
  ellipse->Update();


  // Create a test image
  using ImageFilterType = itk::SpatialObjectToImageFilter<EllipseType, ImageType>;
  auto filter = ImageFilterType::New();
  filter->SetInput(ellipse);
  filter->SetSize(size);
  filter->SetSpacing(spacing);
  filter->SetInsideValue(255);
  filter->Update();

  ImageType::Pointer image = filter->GetOutput();

  offset.Fill(25);
  ellipse->GetModifiableObjectToParentTransform()->SetOffset(offset);
  ellipse->Update();

  using CalculatorType = itk::SpatialObjectToImageStatisticsCalculator<ImageType, EllipseType>;
  auto calculator = CalculatorType::New();

  ITK_EXERCISE_BASIC_OBJECT_METHODS(calculator, SpatialObjectToImageStatisticsCalculator, Object);


  calculator->SetImage(image);
  calculator->SetSpatialObject(ellipse);

  unsigned int sampleDirection = CalculatorType::SampleDimension - 1;
  calculator->SetSampleDirection(sampleDirection);
  ITK_TEST_SET_GET_VALUE(sampleDirection, calculator->GetSampleDirection());

  calculator->Update();

  std::cout << " --- Ellipse and Image perfectly aligned --- " << std::endl;
  std::cout << "Sample mean = " << calculator->GetMean() << std::endl;
  std::cout << "Sample covariance = " << calculator->GetCovarianceMatrix();

  if (calculator->GetMean() != itk::MakeFilled<CalculatorType::VectorType>(255) ||
      itk::Math::NotAlmostEquals(calculator->GetCovarianceMatrix()[0][0], CalculatorType::MatrixType::ValueType{}))
  {
    std::cout << "[FAILED]" << std::endl;
    return EXIT_FAILURE;
  }

  std::cout << "[PASSED]" << std::endl;

  offset.Fill(20);
  ellipse->GetModifiableObjectToParentTransform()->SetOffset(offset);
  ellipse->Update();
  calculator->Update();

  std::cout << " --- Ellipse and Image mismatched left --- " << std::endl;
  std::cout << "Sample mean = " << calculator->GetMean() << std::endl;
  std::cout << "Sample covariance = " << calculator->GetCovarianceMatrix();

  if ((itk::Math::abs(calculator->GetMean()[0] - 140.0) > 1.0) ||
      (itk::Math::abs(calculator->GetCovarianceMatrix()[0][0] - 16141.0) > 1.0))
  {
    std::cout << "[FAILED]" << std::endl;
    return EXIT_FAILURE;
  }

  std::cout << "[PASSED]" << std::endl;

  std::cout << " --- Ellipse and Image mismatched right --- " << std::endl;

  offset.Fill(30);
  ellipse->GetModifiableObjectToParentTransform()->SetOffset(offset);
  ellipse->ComputeObjectToParentTransform();
  ellipse->Update();
  calculator->Update();

  std::cout << "Sample mean = " << calculator->GetMean() << std::endl;
  std::cout << "Sample covariance = " << calculator->GetCovarianceMatrix();

  if ((itk::Math::abs(calculator->GetMean()[0] - 140.0) > 1.0) ||
      (itk::Math::abs(calculator->GetCovarianceMatrix()[0][0] - 16141.0) > 1.0))
  {
    std::cout << "[FAILED]" << std::endl;
    return EXIT_FAILURE;
  }

  std::cout << "[PASSED]" << std::endl;

  std::cout << " --- Testing higher dimensionality --- " << std::endl;

  // Create a new 3D image
  using Image3DType = itk::Image<PixelType, 3>;
  auto image3D = Image3DType::New();

  using RegionType = Image3DType::RegionType;
  using SizeType = Image3DType::SizeType;
  using IndexType = Image3DType::IndexType;

  SizeType size3D;
  size3D[0] = 50;
  size3D[1] = 50;
  size3D[2] = 3;
  IndexType start;
  start.Fill(0);
  RegionType region3D;
  region3D.SetIndex(start);
  region3D.SetSize(size3D);
  image3D->SetRegions(region3D);
  image3D->Allocate();
  image3D->FillBuffer(255);

  // Fill the image
  std::cout << "Allocating image." << std::endl;
  using SliceIteratorType = itk::ImageSliceIteratorWithIndex<Image3DType>;
  SliceIteratorType it(image3D, region3D);

  it.SetFirstDirection(0);  // 0=x, 1=y, 2=z
  it.SetSecondDirection(1); // 0=x, 1=y, 2=z

  unsigned int value = 0;
  while (!it.IsAtEnd())
  {
    while (!it.IsAtEndOfSlice())
    {
      while (!it.IsAtEndOfLine())
      {
        it.Set(value);
        ++it;
      }
      it.NextLine();
    }
    it.NextSlice();
    value++;
  }

  std::cout << "Allocating spatial object." << std::endl;
  using Ellipse3DType = itk::EllipseSpatialObject<3>;
  auto   ellipse3D = Ellipse3DType::New();
  double radii[3];
  radii[0] = 10;
  radii[1] = 10;
  radii[2] = 0;
  ellipse3D->SetRadiusInObjectSpace(radii);

  Ellipse3DType::VectorType offset3D;
  offset3D.Fill(25);
  offset3D[2] = 0; // first slice
  ellipse3D->GetModifiableObjectToParentTransform()->SetOffset(offset3D);
  ellipse3D->Update();

  // Create a new calculator with a sample size of 3
  std::cout << "Updating calculator." << std::endl;
  using Calculator3DType = itk::SpatialObjectToImageStatisticsCalculator<Image3DType, Ellipse3DType, 3>;
  auto calculator3D = Calculator3DType::New();
  calculator3D->SetImage(image3D);
  calculator3D->SetSpatialObject(ellipse3D);
  calculator3D->Update();

  std::cout << "Sample mean = " << calculator3D->GetMean() << std::endl;
  std::cout << "Sample covariance = " << calculator3D->GetCovarianceMatrix();

  if ((itk::Math::abs(calculator3D->GetMean()[0] - 0.0) > 1.0) ||
      (itk::Math::abs(calculator3D->GetMean()[1] - 1.0) > 1.0) ||
      (itk::Math::abs(calculator3D->GetMean()[2] - 2.0) > 1.0))
  {
    std::cout << "[FAILED]" << std::endl;
    return EXIT_FAILURE;
  }

  std::cout << "Number of pixels = " << calculator3D->GetNumberOfPixels() << std::endl;
  if (calculator3D->GetNumberOfPixels() != 305)
  {
    std::cout << "[FAILED]" << std::endl;
    return EXIT_FAILURE;
  }

  std::cout << "Sum = " << calculator3D->GetSum() << std::endl;
  if (calculator3D->GetSum() != 915)
  {
    std::cout << "[FAILED]" << std::endl;
    return EXIT_FAILURE;
  }


  std::cout << "Test finished" << std::endl;
  return EXIT_SUCCESS;
}