File: itkSobelOperatorImageConvolutionTest.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 (166 lines) | stat: -rw-r--r-- 6,330 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
/*=========================================================================
 *
 *  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 "itkConstNeighborhoodIterator.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkImageRegionIterator.h"
#include "itkNeighborhoodInnerProduct.h"
#include "itkSobelOperator.h"
#include "itkTestingMacros.h"
#include "itkRescaleIntensityImageFilter.h"


template <typename ImageType>
typename ImageType::Pointer
MakeOnes3x3Image()
{
  typename ImageType::Pointer onesImage = ImageType::New();
  {
    typename ImageType::SizeType   smallest_size{ { 3, 3 } };
    typename ImageType::IndexType  start_index{ { 0, 0 } };
    typename ImageType::RegionType my_region(start_index, smallest_size);
    onesImage->SetRegions(my_region);
  }
  onesImage->Allocate();
  onesImage->FillBuffer(1);
  return onesImage;
}

template <typename ImageType>
typename ImageType::Pointer
DoConvolution(typename ImageType::Pointer inputImage, unsigned long int direction)
{
  using PixelType = typename ImageType::PixelType;
  constexpr std::size_t Dimension = ImageType::ImageDimension;

  using SobelOperatorType = itk::SobelOperator<PixelType, Dimension>;

  using NeighborhoodIteratorType = itk::ConstNeighborhoodIterator<ImageType>;
  using IteratorType = itk::ImageRegionIterator<ImageType>;

  SobelOperatorType sobelOperator;

  sobelOperator.SetDirection(direction);

  itk::Size<Dimension> radius;
  radius.Fill(1);
  sobelOperator.CreateToRadius(radius);

  NeighborhoodIteratorType it(radius, inputImage, inputImage->GetRequestedRegion());

  auto outputImage = ImageType::New();
  outputImage->SetRegions(inputImage->GetRequestedRegion());
  outputImage->AllocateInitialized();

  IteratorType                             out(outputImage, inputImage->GetRequestedRegion());
  itk::NeighborhoodInnerProduct<ImageType> innerProduct;
  for (it.GoToBegin(), out.GoToBegin(); !it.IsAtEnd(); ++it, ++out)
  {
    const auto pixelValue = innerProduct(it, sobelOperator);
    out.Set(pixelValue);
  }
  return outputImage;
}

template <typename PixelType, unsigned long Dimension>
int
DoSimpleConvolutionTest(unsigned long direction, const std::string & pixelType)
{
  using ImageType = typename itk::Image<PixelType, Dimension>;

  typename ImageType::Pointer smallestOnesImage = MakeOnes3x3Image<ImageType>();
  typename ImageType::Pointer output3x3Image = DoConvolution<ImageType>(smallestOnesImage, direction);

  typename ImageType::IndexType center_index{ { 1, 1 } };
  typename ImageType::PixelType center_value = output3x3Image->GetPixel(center_index);
  if (center_value != 0)
  {
    std::cout << "ERROR: Constant image convolution with SobelOperator should return 0, "
              << "but value of " << +center_value << " was computed. [" << pixelType << "]" << std::endl;
    return EXIT_FAILURE;
  }
  return EXIT_SUCCESS;
}

int
itkSobelOperatorImageConvolutionTest(int argc, char * argv[])
{
  if (argc != 4)
  {
    std::cerr << "Missing parameters." << std::endl;
    std::cerr << "Usage: " << itkNameOfTestExecutableMacro(argv) << " inputFileName direction outputFileName"
              << std::endl;
    return EXIT_FAILURE;
  }
  const auto direction = std::stoul(argv[2]);
  int        return_status = EXIT_SUCCESS;

  constexpr unsigned int Dimension = 2;
  /* Sanity Checking For Sobel Operator */
  //{
  /*
   * Demonstrate that signed types do **NOT** work with SobelOperator
   * The unsigned checks require disableing Signed ConceptChecking in SobelOperator
   * added in March 2023.
   */
  /*
  return_status += DoSimpleConvolutionTest<unsigned char, 2>(direction, "unsigned char");
  return_status += DoSimpleConvolutionTest<unsigned short, 2>(direction, "unsigned short");
  return_status += DoSimpleConvolutionTest<unsigned int, 2>(direction, "unsigned int");
  return_status += DoSimpleConvolutionTest<unsigned long, 2>(direction, "unsigned long");
  */
  //}
  {
    /*
     * Demonstrate that signed types do work with SobelOperator
     */
    // Note: `char` can be unsigned on some platforms.
    return_status += DoSimpleConvolutionTest<signed char, 2>(direction, "signed char");
    return_status += DoSimpleConvolutionTest<short, 2>(direction, "short");
    return_status += DoSimpleConvolutionTest<int, 2>(direction, "int");
    return_status += DoSimpleConvolutionTest<long, 2>(direction, "long");
    return_status += DoSimpleConvolutionTest<float, 2>(direction, "float");
    return_status += DoSimpleConvolutionTest<double, 2>(direction, "double");
  }

  {
    /* Test on a real image */
    using PixelType = int16_t;
    using ImageType = itk::Image<PixelType, Dimension>;

    const auto inputImage = itk::ReadImage<ImageType>(argv[1]);

    auto signedSobelImage = DoConvolution<ImageType>(inputImage, direction);

    using OutputImageType = itk::Image<uint8_t, Dimension>;
    // Assume min/max values are approximately +/- same magnitude so that the output images
    // to be stored in uint8_t have an implied 0 at about pixel value 128.  Many web based viewers
    // for the difference images in the testing outputs render better in this positive png range.
    using RescaleIntensityType = itk::RescaleIntensityImageFilter<ImageType, OutputImageType>;
    RescaleIntensityType::Pointer rescalerForVisualization = RescaleIntensityType::New();
    rescalerForVisualization->SetInput(signedSobelImage);
    rescalerForVisualization->SetOutputMinimum(0);
    rescalerForVisualization->SetOutputMaximum(255);
    rescalerForVisualization->Update();
    itk::WriteImage(rescalerForVisualization->GetOutput(), argv[3]);
  }

  std::cout << "Test finished." << std::endl;
  return return_status;
}