|
| 1 | +/*========================================================================= |
| 2 | + * |
| 3 | + * Copyright NumFOCUS |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * https://www.apache.org/licenses/LICENSE-2.0.txt |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + *=========================================================================*/ |
| 18 | + |
| 19 | +// First include the header file to be tested: |
| 20 | +#include "itkGradientImageFilter.h" |
| 21 | + |
| 22 | +#include "itkDeref.h" |
| 23 | +#include "itkImage.h" |
| 24 | +#include "itkImageBufferRange.h" |
| 25 | +#include "itkIndexRange.h" |
| 26 | + |
| 27 | +#include <gtest/gtest.h> |
| 28 | + |
| 29 | + |
| 30 | +// Tests the output for a uniform input image. |
| 31 | +TEST(GradientImageFilter, UniformInputImage) |
| 32 | +{ |
| 33 | + static constexpr unsigned int Dimension{ 3 }; |
| 34 | + using PixelType = int; |
| 35 | + using ImageType = itk::Image<PixelType, Dimension>; |
| 36 | + using ImageSizeType = itk::Size<Dimension>; |
| 37 | + using FilterType = itk::GradientImageFilter<ImageType>; |
| 38 | + |
| 39 | + for (const PixelType inputPixelValue : |
| 40 | + { std::numeric_limits<PixelType>::lowest(), PixelType{ 1 }, std::numeric_limits<PixelType>::max() }) |
| 41 | + { |
| 42 | + const auto filter = FilterType::New(); |
| 43 | + |
| 44 | + // Keep the image size small, in order to keep the unit test fast! |
| 45 | + static constexpr auto imageSize = ImageSizeType::Filled(4); |
| 46 | + |
| 47 | + const auto inputImage = ImageType::New(); |
| 48 | + inputImage->SetRegions(imageSize); |
| 49 | + inputImage->Allocate(false); |
| 50 | + inputImage->FillBuffer(inputPixelValue); |
| 51 | + filter->SetInput(inputImage); |
| 52 | + filter->Update(); |
| 53 | + const auto & output = itk::Deref(filter->GetOutput()); |
| 54 | + |
| 55 | + const itk::ImageBufferRange outputImageBufferRange(output); |
| 56 | + |
| 57 | + EXPECT_EQ(outputImageBufferRange.size(), imageSize.CalculateProductOfElements()); |
| 58 | + |
| 59 | + for (const auto & outputValue : outputImageBufferRange) |
| 60 | + { |
| 61 | + // Expect all output pixels to be zero. |
| 62 | + EXPECT_EQ(outputValue, FilterType::OutputPixelType{}); |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | + |
| 68 | +// Tests the output for an input image that has a constant gradient along its first dimension. |
| 69 | +TEST(GradientImageFilter, ConstantGradientInputImage) |
| 70 | +{ |
| 71 | + static constexpr unsigned int Dimension{ 3 }; |
| 72 | + using PixelType = int; |
| 73 | + using ImageType = itk::Image<PixelType, Dimension>; |
| 74 | + using IndexType = itk::Index<Dimension>; |
| 75 | + using SizeType = itk::Size<Dimension>; |
| 76 | + using FilterType = itk::GradientImageFilter<ImageType>; |
| 77 | + using OutputValueType = FilterType::OutputValueType; |
| 78 | + |
| 79 | + for (int inputGradientValue{ -1 }; inputGradientValue <= 2; ++inputGradientValue) |
| 80 | + { |
| 81 | + const auto filter = FilterType::New(); |
| 82 | + |
| 83 | + // Keep the image size small, in order to keep the unit test fast! |
| 84 | + static constexpr auto imageSize = SizeType::Filled(4); |
| 85 | + |
| 86 | + const auto inputImage = ImageType::New(); |
| 87 | + inputImage->SetRegions(imageSize); |
| 88 | + inputImage->Allocate(false); |
| 89 | + |
| 90 | + for (const auto & index : itk::ZeroBasedIndexRange<Dimension>(imageSize)) |
| 91 | + { |
| 92 | + // A constant gradient along the first dimension of the image. |
| 93 | + inputImage->SetPixel(index, static_cast<PixelType>(inputGradientValue * index[0])); |
| 94 | + } |
| 95 | + |
| 96 | + filter->SetInput(inputImage); |
| 97 | + filter->Update(); |
| 98 | + const auto & output = itk::Deref(filter->GetOutput()); |
| 99 | + |
| 100 | + ASSERT_EQ(output.GetBufferedRegion().GetSize(), imageSize); |
| 101 | + |
| 102 | + // Only look at the inner region, to avoid boundary effects (which are beyond the scope of this unit test). |
| 103 | + const itk::ImageRegion innerRegion{ IndexType::Filled(1), imageSize - SizeType::Filled(2) }; |
| 104 | + |
| 105 | + for (const auto & index : itk::ImageRegionIndexRange<Dimension>(innerRegion)) |
| 106 | + { |
| 107 | + const auto outputPixelValue = output.GetPixel(index); |
| 108 | + |
| 109 | + EXPECT_EQ(outputPixelValue[0], inputGradientValue); |
| 110 | + |
| 111 | + for (unsigned int i{ 1 }; i < Dimension; ++i) |
| 112 | + { |
| 113 | + EXPECT_EQ(outputPixelValue[i], OutputValueType{ 0 }); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments