File: itkConvolutionImageFilterStreamingTest.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 (178 lines) | stat: -rw-r--r-- 7,025 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
/*=========================================================================
 *
 *  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 "itkConvolutionImageFilter.h"
#include "itkFFTConvolutionImageFilter.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkSimpleFilterWatcher.h"
#include "itkTestingMacros.h"
#include "itkGaussianImageSource.h"
#include "itkPipelineMonitorImageFilter.h"
#include "itkStreamingImageFilter.h"

using KernelImageType = itk::Image<float, 2>;

typename KernelImageType::Pointer
GenerateKernelForStreamingTest()
{
  using SourceType = itk::GaussianImageSource<KernelImageType>;
  using KernelSizeType = typename SourceType::SizeType;
  auto           source = SourceType::New();
  KernelSizeType kernelSize{ { 3, 5 } };
  source->SetSize(kernelSize);
  source->SetMean(2);
  source->SetSigma(3.0);
  source->SetScale(1.0);
  source->SetNormalized(true);
  source->Update();
  return source->GetOutput();
}

template <typename ConvolutionFilterType>
int
doConvolutionImageFilterStreamingTest(int argc, char * argv[])
{
  constexpr int ImageDimension = 2;

  using PixelType = float;
  using ImageType = itk::Image<PixelType, ImageDimension>;
  using RegionType = typename ImageType::RegionType;
  using SizeType = typename RegionType::SizeType;
  using IndexType = typename RegionType::IndexType;

  // Request a subregion of the largest possible output
  IndexType requestedIndex;
  requestedIndex[0] = std::atoi(argv[4]);
  requestedIndex[1] = std::atoi(argv[5]);
  SizeType requestedSize;
  requestedSize[0] = std::atoi(argv[6]);
  requestedSize[1] = std::atoi(argv[7]);
  RegionType requestedRegion(requestedIndex, requestedSize);

  auto regionMode = (argc > 9 && std::string("valid").compare(argv[8]) == 0
                       ? itk::ConvolutionImageFilterBaseEnums::ConvolutionImageFilterOutputRegion::VALID
                       : itk::ConvolutionImageFilterBaseEnums::ConvolutionImageFilterOutputRegion::SAME);

  using ReaderType = itk::ImageFileReader<ImageType>;

  auto reader1 = ReaderType::New();
  reader1->SetFileName(argv[2]);

  // Verify the requested region is a subregion of the largest possible region
  reader1->UpdateOutputInformation();
  ITK_TEST_EXPECT_TRUE(reader1->GetOutput()->GetLargestPossibleRegion().IsInside(requestedRegion));

  using PipelineMonitorType = itk::PipelineMonitorImageFilter<ImageType>;
  auto inputMonitor = PipelineMonitorType::New();
  inputMonitor->SetInput(reader1->GetOutput());

  KernelImageType::Pointer kernelImage = GenerateKernelForStreamingTest();

  auto convoluter = ConvolutionFilterType::New();
  convoluter->SetInput(inputMonitor->GetOutput());
  convoluter->SetKernelImage(kernelImage);
  convoluter->SetOutputRegionMode(regionMode);
  convoluter->SetReleaseDataFlag(true);
  itk::SimpleFilterWatcher watcher(convoluter, "filter");

  using PipelineMonitorType = itk::PipelineMonitorImageFilter<ImageType>;
  auto outputMonitor = PipelineMonitorType::New();
  outputMonitor->SetInput(convoluter->GetOutput());

  using StreamingFilterType = itk::StreamingImageFilter<ImageType, ImageType>;
  auto streamer = StreamingFilterType::New();
  streamer->SetInput(outputMonitor->GetOutput());
  const unsigned int numStreamDivisions = 10;
  streamer->SetNumberOfStreamDivisions(numStreamDivisions);
  streamer->GetOutput()->SetRequestedRegion(requestedRegion);
  ITK_TRY_EXPECT_NO_EXCEPTION(streamer->Update());

  // Verify the pipeline executed as expected
  // Expect requested region propagates twice due to ConvolutionImageFilter having
  // two inputs (Primary and KernelImage)
  const unsigned int requestsPerStream = 2;
  // Verify ConvolutionImageFilter upstream requests
  ITK_TEST_EXPECT_EQUAL(inputMonitor->GetOutputRequestedRegions().size(), requestsPerStream * numStreamDivisions);
  ITK_TEST_EXPECT_EQUAL(inputMonitor->GetNumberOfUpdates(), numStreamDivisions);

  // Verify ConvolutionImageFilter downstream outputs
  ITK_TEST_EXPECT_EQUAL(outputMonitor->GetNumberOfUpdates(), numStreamDivisions);
  ITK_TEST_EXPECT_EQUAL(outputMonitor->GetOutputRequestedRegions().size(), numStreamDivisions);
  ITK_TEST_EXPECT_TRUE(outputMonitor->VerifyInputFilterBufferedRequestedRegions());
  ITK_TEST_EXPECT_TRUE(outputMonitor->VerifyAllInputCanStream(numStreamDivisions));
  ITK_TEST_EXPECT_TRUE(outputMonitor->VerifyInputFilterMatchedUpdateOutputInformation());

  //// Write out buffered region resulting from convolution
  using RegionOfInterestFilterType = itk::RegionOfInterestImageFilter<ImageType, ImageType>;
  auto cropFilter = RegionOfInterestFilterType::New();
  cropFilter->SetInput(streamer->GetOutput());
  cropFilter->SetRegionOfInterest(streamer->GetOutput()->GetBufferedRegion());
  cropFilter->Update();

  using WriterType = itk::ImageFileWriter<ImageType>;
  auto writer = WriterType::New();
  writer->SetFileName(argv[3]);
  writer->SetInput(cropFilter->GetOutput());

  ITK_TRY_EXPECT_NO_EXCEPTION(writer->Update());
  std::cout << "Wrote output image to " << argv[3] << std::endl;

  return EXIT_SUCCESS;
}

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

  if (argc < 8)
  {
    std::cout << "Usage: " << itkNameOfTestExecutableMacro(argv) << "convolutionType "
              << "inputImage "
              << "outputImage "
              << "indexX indexY sizeX sizeY "
              << "[regionMode]" << std::endl;
    return EXIT_FAILURE;
  }

  constexpr int ImageDimension = 2;

  using PixelType = float;
  using ImageType = itk::Image<PixelType, ImageDimension>;

  if (std::string("frequency").compare(argv[1]) == 0)
  {
    using FrequencyConvolutionType = itk::FFTConvolutionImageFilter<ImageType>;

    // Do a quick filter sanity check before the test
    auto convoluter = FrequencyConvolutionType::New();
    ITK_EXERCISE_BASIC_OBJECT_METHODS(convoluter, FFTConvolutionImageFilter, ConvolutionImageFilterBase);

    return doConvolutionImageFilterStreamingTest<FrequencyConvolutionType>(argc, argv);
  }
  else // spatial
  {
    using SpatialConvolutionType = itk::ConvolutionImageFilter<ImageType>;

    // Do a quick filter sanity check before the test
    auto convoluter = SpatialConvolutionType::New();
    ITK_EXERCISE_BASIC_OBJECT_METHODS(convoluter, ConvolutionImageFilter, ConvolutionImageFilterBase);

    return doConvolutionImageFilterStreamingTest<SpatialConvolutionType>(argc, argv);
  }
}