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
|
/*=========================================================================
*
* 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 "itkChangeInformationImageFilter.h"
#include "itkConstantBoundaryCondition.h"
#include "itkFFTConvolutionImageFilter.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkPeriodicBoundaryCondition.h"
#include "itkSimpleFilterWatcher.h"
#include "itkTestingMacros.h"
#include "itkZeroFluxNeumannBoundaryCondition.h"
#include "itkObjectFactoryBase.h"
#include "itkVnlRealToHalfHermitianForwardFFTImageFilter.h"
#include "itkVnlHalfHermitianToRealInverseFFTImageFilter.h"
#if defined(ITK_USE_FFTWD) || defined(ITK_USE_FFTWF)
# include "itkFFTWRealToHalfHermitianForwardFFTImageFilter.h"
# include "itkFFTWHalfHermitianToRealInverseFFTImageFilter.h"
#endif
int
itkFFTConvolutionImageFilterTest(int argc, char * argv[])
{
if (argc < 4)
{
std::cout << "Usage: " << itkNameOfTestExecutableMacro(argv) << " inputImage "
<< "kernelImage "
<< "outputImage "
<< "[sizeGreatestPrimeFactor] "
<< "[normalizeImage] "
<< "[outputRegionMode] "
<< "[boundaryCondition] " << std::endl;
return EXIT_FAILURE;
}
constexpr int ImageDimension = 2;
using PixelType = float;
using ImageType = itk::Image<PixelType, ImageDimension>;
using ReaderType = itk::ImageFileReader<ImageType>;
auto reader1 = ReaderType::New();
reader1->SetFileName(argv[1]);
ITK_TRY_EXPECT_NO_EXCEPTION(reader1->Update());
auto reader2 = ReaderType::New();
reader2->SetFileName(argv[2]);
ITK_TRY_EXPECT_NO_EXCEPTION(reader2->Update());
using ConvolutionFilterType = itk::FFTConvolutionImageFilter<ImageType>;
auto convoluter = ConvolutionFilterType::New();
ITK_EXERCISE_BASIC_OBJECT_METHODS(convoluter, FFTConvolutionImageFilter, ConvolutionImageFilterBase);
// Test empty image exception
auto emptyImage = ImageType::New();
convoluter->SetInput(emptyImage);
try
{
convoluter->Update();
std::cerr << "Failed to throw expected exception" << std::endl;
return EXIT_FAILURE;
}
catch (const itk::ExceptionObject & excp)
{
std::cout << excp << std::endl;
std::cout << "Caught EXPECTED exception for empty image as input" << std::endl;
}
// Test generality of filter by changing the image index
using ChangeInformationFilterType = itk::ChangeInformationImageFilter<ImageType>;
auto inputChanger = ChangeInformationFilterType::New();
inputChanger->ChangeRegionOn();
ImageType::OffsetType inputOffset = { { -2, 3 } };
inputChanger->SetOutputOffset(inputOffset);
inputChanger->SetInput(reader1->GetOutput());
convoluter->SetInput(inputChanger->GetOutput());
// Test generality of filter by changing the kernel index
auto kernelChanger = ChangeInformationFilterType::New();
kernelChanger->ChangeRegionOn();
ImageType::OffsetType kernelOffset = { { 3, -5 } };
kernelChanger->SetOutputOffset(kernelOffset);
kernelChanger->SetInput(reader2->GetOutput());
convoluter->SetKernelImage(kernelChanger->GetOutput());
if (argc >= 5)
{
ConvolutionFilterType::SizeValueType sizeGreatestPrimeFactor = std::stoi(argv[4]);
if (!itk::Math::IsPrime(sizeGreatestPrimeFactor))
{
std::cerr << "A prime number is expected for the greatest prime factor size!" << std::endl;
return EXIT_FAILURE;
}
convoluter->SetSizeGreatestPrimeFactor(sizeGreatestPrimeFactor);
ITK_TEST_SET_GET_VALUE(sizeGreatestPrimeFactor, convoluter->GetSizeGreatestPrimeFactor());
}
if (argc >= 6)
{
auto normalize = static_cast<bool>(std::stoi(argv[5]));
convoluter->SetNormalize(normalize);
ITK_TEST_SET_GET_VALUE(normalize, convoluter->GetNormalize());
if (normalize)
{
convoluter->NormalizeOn();
ITK_TEST_EXPECT_TRUE(convoluter->GetNormalize());
}
else
{
convoluter->NormalizeOff();
ITK_TEST_EXPECT_TRUE(!convoluter->GetNormalize());
}
}
if (argc >= 7)
{
std::string outputRegionMode(argv[6]);
if (outputRegionMode == "SAME")
{
convoluter->SetOutputRegionMode(itk::ConvolutionImageFilterBaseEnums::ConvolutionImageFilterOutputRegion::SAME);
ITK_TEST_SET_GET_VALUE(itk::ConvolutionImageFilterBaseEnums::ConvolutionImageFilterOutputRegion::SAME,
convoluter->GetOutputRegionMode());
}
else if (outputRegionMode == "VALID")
{
convoluter->SetOutputRegionMode(itk::ConvolutionImageFilterBaseEnums::ConvolutionImageFilterOutputRegion::VALID);
ITK_TEST_SET_GET_VALUE(itk::ConvolutionImageFilterBaseEnums::ConvolutionImageFilterOutputRegion::VALID,
convoluter->GetOutputRegionMode());
}
else
{
std::cerr << "Invalid OutputRegionMode '" << outputRegionMode << "'." << std::endl;
std::cerr << "Valid values are SAME or VALID." << std::endl;
return EXIT_FAILURE;
}
if (outputRegionMode == "SAME")
{
convoluter->SetOutputRegionModeToSame();
ITK_TEST_SET_GET_VALUE(ConvolutionFilterType::OutputRegionModeEnum::SAME, convoluter->GetOutputRegionMode());
}
else
{
convoluter->SetOutputRegionModeToValid();
ITK_TEST_SET_GET_VALUE(ConvolutionFilterType::OutputRegionModeEnum::VALID, convoluter->GetOutputRegionMode());
}
}
itk::ConstantBoundaryCondition<ImageType> constantBoundaryCondition;
convoluter->SetBoundaryCondition(&constantBoundaryCondition);
itk::PeriodicBoundaryCondition<ImageType> periodicBoundaryCondition;
itk::ZeroFluxNeumannBoundaryCondition<ImageType> zeroFluxNeumannBoundaryCondition;
if (argc >= 7)
{
std::string boundaryCondition(argv[7]);
if (boundaryCondition == "CONSTANT")
{
convoluter->SetBoundaryCondition(&constantBoundaryCondition);
ITK_TEST_SET_GET_VALUE(&constantBoundaryCondition, convoluter->GetBoundaryCondition());
}
else if (boundaryCondition == "PERIODIC")
{
convoluter->SetBoundaryCondition(&periodicBoundaryCondition);
ITK_TEST_SET_GET_VALUE(&periodicBoundaryCondition, convoluter->GetBoundaryCondition());
}
else if (boundaryCondition == "ZEROFLUXNEUMANN")
{
convoluter->SetBoundaryCondition(&zeroFluxNeumannBoundaryCondition);
ITK_TEST_SET_GET_VALUE(&zeroFluxNeumannBoundaryCondition, convoluter->GetBoundaryCondition());
}
else
{
std::cerr << "Invalid BoundaryCondition '" << boundaryCondition << "'." << std::endl;
std::cerr << "Valid values are CONSTANT, PERIODIC or ZEROFLUXNEUMANN." << std::endl;
return EXIT_FAILURE;
}
}
itk::SimpleFilterWatcher watcher(convoluter, "filter");
ITK_TRY_EXPECT_NO_EXCEPTION(convoluter->Update());
using WriterType = itk::ImageFileWriter<ImageType>;
auto writer = WriterType::New();
writer->SetFileName(argv[3]);
writer->SetInput(convoluter->GetOutput());
ITK_TRY_EXPECT_NO_EXCEPTION(writer->Update());
// Test VALID output region mode with kernel that is larger than
// the input image. Should result in a zero-size valid region.
auto largeKernel = ImageType::New();
ImageType::RegionType kernelRegion(reader1->GetOutput()->GetLargestPossibleRegion().GetSize());
kernelRegion.PadByRadius(5);
largeKernel->SetRegions(kernelRegion);
largeKernel->Allocate();
convoluter->SetOutputRegionModeToValid();
convoluter->SetInput(reader1->GetOutput());
convoluter->SetKernelImage(largeKernel);
ITK_TRY_EXPECT_EXCEPTION(convoluter->Update());
// Test for invalid request region.
ImageType::IndexType invalidIndex;
invalidIndex.Fill(1000);
ImageType::SizeType invalidSize;
invalidSize.Fill(1000);
ImageType::RegionType invalidRequestRegion(invalidIndex, invalidSize);
convoluter->GetOutput()->SetRequestedRegion(invalidRequestRegion);
ITK_TRY_EXPECT_EXCEPTION(convoluter->Update());
return EXIT_SUCCESS;
}
|