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
|
/*=========================================================================
*
* 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.
*
*=========================================================================*/
// The header file to be tested:
#include "itkResampleImageFilter.h"
#include "itkImage.h"
// Google Test header file:
#include <gtest/gtest.h>
// Standard C++ header files:
#include <limits>
#include <random>
namespace
{
// Returns the first pixel value from the output image of a ResampleImageFilter
// whose input is a 1x1 image, having the specified input pixel value. The
// filter uses a default interpolator and a default (identity) transform.
template <typename TPixel>
TPixel
GetFirstPixelFromFilterOutput(const TPixel inputPixel)
{
using ImageType = itk::Image<TPixel>;
const auto image = ImageType::New();
const typename ImageType::SizeType imageSize = { { 1, 1 } };
image->SetRegions(imageSize);
image->Allocate();
image->SetPixel({ { 0, 0 } }, inputPixel);
const auto filter = itk::ResampleImageFilter<ImageType, ImageType>::New();
filter->SetInput(image);
filter->SetSize(imageSize);
filter->Update();
return filter->GetOutput()->GetPixel({ { 0, 0 } });
}
// Test if the resample image filter has a valid output region defined
// A common mistake is to use SetReferenceImage without setting UseReferenceImageOn
// which results in the default output image size of 0,0,0.
template <typename TPixel>
TPixel
TestThrowErrorOnEmptyResampleSpace(const TPixel inputPixel, const bool setUseReferenceImageMemberFunction)
{
using ImageType = itk::Image<TPixel>;
const auto image = ImageType::New();
const typename ImageType::SizeType imageSize = { { 1, 1 } };
image->SetRegions(imageSize);
image->Allocate();
image->SetPixel({ { 0, 0 } }, inputPixel);
const auto filter = itk::ResampleImageFilter<ImageType, ImageType>::New();
filter->SetInput(image);
// NOTE: One might incorrectly assume that SetReferenceImage(image) has the same as effect as SetSize(imageSize)
// BUT in addition to SetUseReferenceImage(image), we must also specify UseReferenceImageOn()
filter->SetReferenceImage(image);
if (setUseReferenceImageMemberFunction)
{
filter->UseReferenceImageOn();
}
filter->Update();
typename ImageType::ConstPointer filterOutputImage = filter->GetOutput();
if (filterOutputImage->GetLargestPossibleRegion().GetNumberOfPixels() == 0)
{
return itk::NumericTraits<TPixel>::max();
}
return filterOutputImage->GetPixel({ { 0, 0 } });
}
// Tests that the value of the pixel from the 1x1 ResampleImageFilter output
// image is equal to the specified input pixel value, when the filter just
// uses default settings.
template <typename TPixel>
void
Expect_ResampleImageFilter_preserves_pixel_value(const TPixel inputPixel)
{
EXPECT_EQ(GetFirstPixelFromFilterOutput(inputPixel), inputPixel);
}
// Tests that a useful diagnostic is provided via a thrown exception if the
// resample image filter has used "SetReferenceImage()" but not "UseReferenceImageOn()".
template <typename TPixel>
void
Expect_ResampleImageFilter_thows_on_incomplete_configuration(const TPixel inputPixel)
{
EXPECT_THROW(TestThrowErrorOnEmptyResampleSpace(inputPixel, false), itk::ExceptionObject);
EXPECT_EQ(TestThrowErrorOnEmptyResampleSpace(inputPixel, true), inputPixel);
}
} // namespace
// Compile time check of mixing transform and precision types
template class itk::ResampleImageFilter<itk::Image<int>, itk::Image<int>, float, float>;
template class itk::ResampleImageFilter<itk::Image<int>, itk::Image<int>, double, float>;
template class itk::ResampleImageFilter<itk::Image<int>, itk::Image<int>, float, double>;
template class itk::ResampleImageFilter<itk::Image<int>, itk::Image<int>, double, double>;
TEST(ResampleImageFilter, FilterPreservesAnyDoublePixelValueByDefault)
{
using NumericLimits = std::numeric_limits<double>;
Expect_ResampleImageFilter_preserves_pixel_value(0.0);
Expect_ResampleImageFilter_preserves_pixel_value(1.0);
Expect_ResampleImageFilter_preserves_pixel_value(-1.0);
Expect_ResampleImageFilter_preserves_pixel_value(NumericLimits::lowest());
Expect_ResampleImageFilter_preserves_pixel_value(NumericLimits::denorm_min());
Expect_ResampleImageFilter_preserves_pixel_value(NumericLimits::min());
Expect_ResampleImageFilter_preserves_pixel_value(NumericLimits::epsilon());
Expect_ResampleImageFilter_preserves_pixel_value(NumericLimits::max());
// Note: The following two expectations would fail on ITK version <= 4.13.1:
Expect_ResampleImageFilter_preserves_pixel_value(NumericLimits::infinity());
Expect_ResampleImageFilter_preserves_pixel_value(-NumericLimits::infinity());
// Not A Number as input should yield Not A Number as output.
EXPECT_TRUE(std::isnan(GetFirstPixelFromFilterOutput(NumericLimits::quiet_NaN())));
// Check that the filter preserves any "random" pixel value:
std::default_random_engine randomEngine;
// The number of iterations is arbitrarily chosen.
const size_t numberOfIterations = 11;
for (size_t i = 0; i < numberOfIterations; ++i)
{
const double randomNumber1 = std::uniform_real_distribution<>{}(randomEngine);
const double randomNumber2 = std::uniform_real_distribution<>{ 0.0, NumericLimits::max() }(randomEngine);
Expect_ResampleImageFilter_preserves_pixel_value(randomNumber1);
Expect_ResampleImageFilter_preserves_pixel_value(-randomNumber1);
Expect_ResampleImageFilter_preserves_pixel_value(randomNumber2);
Expect_ResampleImageFilter_preserves_pixel_value(-randomNumber2);
}
}
TEST(ResampleImageFilter, FilterPreservesMinAndMaxInt64PixelValuesByDefault)
{
Expect_ResampleImageFilter_preserves_pixel_value(std::numeric_limits<int64_t>::min());
// Note: The following expectation would fail on ITK version <= 4.13.1:
Expect_ResampleImageFilter_preserves_pixel_value(std::numeric_limits<int64_t>::max());
}
TEST(ResampleImageFilter, Expect_ResampleImageFilter_thows_on_incomplete_configuration)
{
Expect_ResampleImageFilter_thows_on_incomplete_configuration(128.0);
}
|