File: itkOrientImageFilterTest.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 (159 lines) | stat: -rw-r--r-- 5,700 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
/*=========================================================================
 *
 *  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 "itkOrientImageFilter.h"
#include "itkImageToImageFilter.h"
#include "itkTestingMacros.h"
#include "vnl/vnl_sample.h"

using ImageType = itk::Image<unsigned int, 3>;

ImageType::Pointer
CreateRandomImage()
{
  const ImageType::SizeType  imageSize = { { 4, 4, 4 } };
  const ImageType::IndexType imageIndex = { { 0, 0, 0 } };
  ImageType::RegionType      region{ imageIndex, imageSize };
  auto                       img = ImageType::New();
  img->SetRegions(region);
  img->Allocate();
  itk::ImageRegionIterator<ImageType> ri(img, region);
  while (!ri.IsAtEnd())
  {
    ri.Set(static_cast<unsigned int>(vnl_sample_uniform(0, 32767)));
    ++ri;
  }
  return img;
}

static void
PrintImg(ImageType::Pointer img)
{
  ImageType::IndexType Index;
  for (Index[2] = 0; Index[2] < 4; Index[2]++)
  {
    for (Index[1] = 0; Index[1] < 4; Index[1]++)
    {
      for (Index[0] = 0; Index[0] < 4; Index[0]++)
      {
        std::cerr << img->GetPixel(Index) << ' ';
      }
      std::cerr << std::endl;
    }
    std::cerr << std::endl;
  }
}

int
itkOrientImageFilterTest(int argc, char * argv[])
{
  if (argc != 2)
  {
    std::cerr << "Missing Parameters." << std::endl;
    std::cerr << "Usage: " << itkNameOfTestExecutableMacro(argv) << " useImageDirection" << std::endl;
    return EXIT_FAILURE;
  }

  itk::MultiThreaderBase::SetGlobalMaximumNumberOfThreads(1);
  ImageType::Pointer randImage = CreateRandomImage();
  std::cerr << "Original" << std::endl;
  PrintImg(randImage);

  itk::OrientImageFilter<ImageType, ImageType>::Pointer orienter = itk::OrientImageFilter<ImageType, ImageType>::New();

  ITK_EXERCISE_BASIC_OBJECT_METHODS(orienter, OrientImageFilter, ImageToImageFilter);


  auto useImageDirection = static_cast<bool>(std::stoi(argv[1]));
  ITK_TEST_SET_GET_BOOLEAN(orienter, UseImageDirection, useImageDirection);

  orienter->SetGivenCoordinateOrientation(
    itk::SpatialOrientationEnums::ValidCoordinateOrientations::ITK_COORDINATE_ORIENTATION_RIP);
  orienter->SetInput(randImage);

  // Try permuting axes
  orienter->SetDesiredCoordinateOrientation(
    itk::SpatialOrientationEnums::ValidCoordinateOrientations::ITK_COORDINATE_ORIENTATION_IRP);
  orienter->Update();
  ImageType::Pointer IRP = orienter->GetOutput();
  std::cerr << "IRP" << std::endl;
  PrintImg(IRP);

  ImageType::RegionType::SizeType originalSize = randImage->GetLargestPossibleRegion().GetSize();
  ImageType::RegionType::SizeType transformedSize = IRP->GetLargestPossibleRegion().GetSize();
  ImageType::IndexType            originalIndex, transformedIndex;

  for (originalIndex[2] = transformedIndex[2] = 0;
       originalIndex[2] < static_cast<ImageType::IndexType::IndexValueType>(originalSize[2]);
       originalIndex[2]++, transformedIndex[2]++)
  {
    for (originalIndex[1] = transformedIndex[0] = 0;
         originalIndex[1] < static_cast<ImageType::IndexType::IndexValueType>(originalSize[1]);
         originalIndex[1]++, transformedIndex[0]++)
    {
      for (originalIndex[0] = transformedIndex[1] = 0;
           originalIndex[0] < static_cast<ImageType::IndexType::IndexValueType>(originalSize[0]);
           originalIndex[0]++, transformedIndex[1]++)
      {
        ImageType::PixelType orig = randImage->GetPixel(originalIndex);
        ImageType::PixelType xfrm = IRP->GetPixel(transformedIndex);
        if (orig != xfrm)
        {
          return EXIT_FAILURE;
        }
      }
    }
  }

  // Go to LIP to check flipping an axis
  orienter = itk::OrientImageFilter<ImageType, ImageType>::New();
  orienter->SetInput(randImage);
  orienter->SetGivenCoordinateOrientation(
    itk::SpatialOrientationEnums::ValidCoordinateOrientations::ITK_COORDINATE_ORIENTATION_RIP);
  orienter->SetDesiredCoordinateOrientation(
    itk::SpatialOrientationEnums::ValidCoordinateOrientations::ITK_COORDINATE_ORIENTATION_LIP);
  orienter->Update();
  ImageType::Pointer LIP = orienter->GetOutput();
  std::cerr << "LIP" << std::endl;
  PrintImg(LIP);
  transformedSize = LIP->GetLargestPossibleRegion().GetSize();

  for (originalIndex[2] = transformedIndex[2] = 0;
       originalIndex[2] < static_cast<ImageType::IndexType::IndexValueType>(originalSize[2]);
       originalIndex[2]++, transformedIndex[2]++)
  {
    for (originalIndex[1] = transformedIndex[1] = 0;
         originalIndex[1] < static_cast<ImageType::IndexType::IndexValueType>(originalSize[1]);
         originalIndex[1]++, transformedIndex[1]++)
    {
      for (originalIndex[0] = 0, transformedIndex[0] = transformedSize[0] - 1;
           originalIndex[0] < static_cast<ImageType::IndexType::IndexValueType>(originalSize[0]);
           originalIndex[0]++, transformedIndex[0]--)
      {
        ImageType::PixelType orig = randImage->GetPixel(originalIndex);
        ImageType::PixelType xfrm = LIP->GetPixel(transformedIndex);
        if (orig != xfrm)
        {
          return EXIT_FAILURE;
        }
      }
    }
  }

  return EXIT_SUCCESS;
}