diff --git a/Modules/Filtering/MathematicalMorphology/include/itkAnchorErodeDilateLine.h b/Modules/Filtering/MathematicalMorphology/include/itkAnchorErodeDilateLine.h index d3a29bd740d..9d355878d4b 100644 --- a/Modules/Filtering/MathematicalMorphology/include/itkAnchorErodeDilateLine.h +++ b/Modules/Filtering/MathematicalMorphology/include/itkAnchorErodeDilateLine.h @@ -102,7 +102,7 @@ class ITK_TEMPLATE_EXPORT AnchorErodeDilateLine Compare(const InputImagePixelType & a, const InputImagePixelType & b) { TCompare compare; - return compare(a, b) || Math::AlmostEquals(a, b); + return !compare(b, a); } }; // end of class diff --git a/Modules/Filtering/MathematicalMorphology/include/itkReconstructionImageFilter.hxx b/Modules/Filtering/MathematicalMorphology/include/itkReconstructionImageFilter.hxx index ed17f43e870..d81d9dca7bc 100644 --- a/Modules/Filtering/MathematicalMorphology/include/itkReconstructionImageFilter.hxx +++ b/Modules/Filtering/MathematicalMorphology/include/itkReconstructionImageFilter.hxx @@ -187,11 +187,11 @@ ReconstructionImageFilter::GenerateData() { if (compare(0, 1)) { - itkExceptionStringMacro("Marker pixels must be <= mask pixels."); + itkExceptionStringMacro("Marker pixels must be >= mask pixels."); } else { - itkExceptionStringMacro("Marker pixels must be >= mask pixels."); + itkExceptionStringMacro("Marker pixels must be <= mask pixels."); } } diff --git a/Modules/Filtering/MathematicalMorphology/include/itkSharedMorphologyUtilities.hxx b/Modules/Filtering/MathematicalMorphology/include/itkSharedMorphologyUtilities.hxx index d8b5a5ff7a0..876b1bef035 100644 --- a/Modules/Filtering/MathematicalMorphology/include/itkSharedMorphologyUtilities.hxx +++ b/Modules/Filtering/MathematicalMorphology/include/itkSharedMorphologyUtilities.hxx @@ -20,7 +20,6 @@ #include "itkImageRegionConstIterator.h" #include "itkNeighborhoodAlgorithm.h" -#include namespace itk { @@ -288,42 +287,9 @@ MakeEnlargedFace(const typename TInputImage::ConstPointer itkNotUsed(input), const typename TInputImage::RegionType AllImage, const TLine line) { - // the face list calculator strategy fails in multithreaded mode - // with 1D kernels - // because it doesn't return faces of the sub-blocks if they don't - // fall along the edge of the image - using RegionType = typename TInputImage::RegionType; - using SizeType = typename TInputImage::SizeType; - using IndexType = typename TInputImage::IndexType; - using FaceListType = std::list; - FaceListType faceList; - - for (unsigned int i = 0; i < TInputImage::ImageDimension; ++i) - { - SizeType S1 = AllImage.GetSize(); - IndexType I2 = AllImage.GetIndex(); - - S1[i] = 1; - RegionType R1 = AllImage; - RegionType R2 = AllImage; - - // the first face will have the same starting index and one - // dimension removed - R1.SetSize(S1); - - I2[i] = I2[i] + AllImage.GetSize()[i] - 1; - R2 = { I2, S1 }; - faceList.push_back(R1); - faceList.push_back(R2); - // std::cout << R1 << R2 << std::endl; - } - auto fit = faceList.begin(); - - bool foundFace = false; + // figure out the dominant direction of the line float MaxComp = NumericTraits::NonpositiveMin(); unsigned int DomDir = 0; - // std::cout << "------------" << std::endl; - // figure out the dominant direction of the line for (unsigned int i = 0; i < TInputImage::RegionType::ImageDimension; ++i) { const auto abs_line_elmt_tmp = itk::Math::Absolute(line[i]); @@ -334,48 +300,24 @@ MakeEnlargedFace(const typename TInputImage::ConstPointer itkNotUsed(input), } } - typename TInputImage::RegionType RelevantRegion; - for (; fit != faceList.end(); ++fit) + // Select the face by DomDir, not by a size of 1: AllImage itself may have size 1 along other dimensions. + typename TInputImage::RegionType RelevantRegion = AllImage; + RelevantRegion.SetSize(DomDir, 1); + bool foundFace = true; + if (line[DomDir] < -0.000001) { - // check whether this face is suitable for parallel sweeping - i.e - // whether the line is within 45 degrees of the perpendicular - // Figure out the perpendicular using the region size - unsigned int FaceDir = 0; - // std::cout << "Face " << *fit << std::endl; - for (unsigned int i = 0; i < TInputImage::RegionType::ImageDimension; ++i) - { - if (fit->GetSize()[i] == 1) - { - FaceDir = i; - } - } - if (FaceDir == DomDir) // within 1 degree - { - // now check whether the line goes inside the image from this face - if (NeedToDoFace(AllImage, *fit, line)) - { - // std::cout << "Using face: " << *fit << line << std::endl; - RelevantRegion = *fit; - foundFace = true; - break; - } - } + RelevantRegion.SetIndex(DomDir, + AllImage.GetIndex(DomDir) + static_cast(AllImage.GetSize(DomDir)) - 1); + } + else if (line[DomDir] <= 0.000001) + { + foundFace = false; } if (foundFace) { // enlarge the region so that sweeping the line across it will // cause all pixels to be visited. - // find the dimension not within the face - unsigned int NonFaceDim = 0; - - for (unsigned int i = 0; i < TInputImage::RegionType::ImageDimension; ++i) - { - if (RelevantRegion.GetSize()[i] == 1) - { - NonFaceDim = i; - break; - } - } + const unsigned int NonFaceDim = DomDir; // figure out how much extra each other dimension needs to be extended typename TInputImage::SizeType NewSize = RelevantRegion.GetSize(); diff --git a/Modules/Filtering/MathematicalMorphology/test/CMakeLists.txt b/Modules/Filtering/MathematicalMorphology/test/CMakeLists.txt index e5efda720c1..9239a455ca1 100644 --- a/Modules/Filtering/MathematicalMorphology/test/CMakeLists.txt +++ b/Modules/Filtering/MathematicalMorphology/test/CMakeLists.txt @@ -55,7 +55,13 @@ createtestdriver(ITKMathematicalMorphology "${ITKMathematicalMorphology-Test_LIB "${ITKMathematicalMorphologyTests}" ) -set(ITKMathematicalMorphologyGTests itkMathematicalMorphologyEnumsGTest.cxx) +set( + ITKMathematicalMorphologyGTests + itkAnchorErodeDilateImageFilterGTest.cxx + itkFlatMorphologyDegenerateRegionGTest.cxx + itkMathematicalMorphologyEnumsGTest.cxx + itkReconstructionImageFilterGTest.cxx +) creategoogletestdriver( ITKMathematicalMorphology "${ITKMathematicalMorphology-Test_LIBRARIES}" diff --git a/Modules/Filtering/MathematicalMorphology/test/itkAnchorErodeDilateImageFilterGTest.cxx b/Modules/Filtering/MathematicalMorphology/test/itkAnchorErodeDilateImageFilterGTest.cxx new file mode 100644 index 00000000000..e916690fc7b --- /dev/null +++ b/Modules/Filtering/MathematicalMorphology/test/itkAnchorErodeDilateImageFilterGTest.cxx @@ -0,0 +1,107 @@ +/*========================================================================= + * + * 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 "itkAnchorDilateImageFilter.h" +#include "itkAnchorErodeImageFilter.h" +#include "itkClosingByReconstructionImageFilter.h" +#include "itkFlatStructuringElement.h" +#include "itkGTest.h" + +#include +#include +#include +#include + +namespace +{ +using ImageType = itk::Image; +using KernelType = itk::FlatStructuringElement<2>; + +// A plateau whose neighboring values differ by one ULP. +std::vector +MakeUlpStaircase(float background, float direction) +{ + std::vector values(16, background); + float value = 50.0f; + for (size_t i = 4; i < 12; ++i) + { + values[i] = value; + value = std::nextafter(value, direction); + } + return values; +} + +// Identical rows, so the expected result in every row is the 1D extreme along the row. +ImageType::Pointer +MakeImage(const std::vector & values) +{ + constexpr size_t numberOfRows = 3; + auto image = ImageType::New(); + image->SetRegions(ImageType::RegionType(ImageType::SizeType{ { values.size(), numberOfRows } })); + image->Allocate(); + for (size_t row = 0; row < numberOfRows; ++row) + { + std::copy(values.cbegin(), values.cend(), image->GetBufferPointer() + row * values.size()); + } + return image; +} + +template +void +ExpectExactExtremes(const std::vector & values, TCompare compare) +{ + auto filter = TFilter::New(); + filter->SetInput(MakeImage(values)); + filter->SetKernel(KernelType::Box(KernelType::RadiusType::Filled(1))); + filter->Update(); + + const float * const output = filter->GetOutput()->GetBufferPointer(); + for (size_t i = 0; i < filter->GetOutput()->GetBufferedRegion().GetNumberOfPixels(); ++i) + { + const size_t x = i % values.size(); + const auto first = values.cbegin() + (x == 0 ? 0 : x - 1); + const auto last = values.cbegin() + std::min(x + 2, values.size()); + EXPECT_EQ(output[i], *std::min_element(first, last, compare)) << "pixel " << i; + } +} +} // namespace + +TEST(AnchorErodeDilateImageFilter, DilateIsExactForNearlyEqualFloats) +{ + using FilterType = itk::AnchorDilateImageFilter; + ExpectExactExtremes(MakeUlpStaircase(0.0f, 0.0f), std::greater()); + ExpectExactExtremes(MakeUlpStaircase(0.0f, 100.0f), std::greater()); +} + +TEST(AnchorErodeDilateImageFilter, ErodeIsExactForNearlyEqualFloats) +{ + using FilterType = itk::AnchorErodeImageFilter; + ExpectExactExtremes(MakeUlpStaircase(100.0f, 0.0f), std::less()); + ExpectExactExtremes(MakeUlpStaircase(100.0f, 100.0f), std::less()); +} + +TEST(AnchorErodeDilateImageFilter, ClosingByReconstructionOfNearlyEqualFloats) +{ + for (const float direction : { 0.0f, 100.0f }) + { + auto filter = itk::ClosingByReconstructionImageFilter::New(); + filter->SetInput(MakeImage(MakeUlpStaircase(0.0f, direction))); + filter->SetKernel(KernelType::Box(KernelType::RadiusType::Filled(1))); + EXPECT_NO_THROW(filter->Update()); + } +} diff --git a/Modules/Filtering/MathematicalMorphology/test/itkFlatMorphologyDegenerateRegionGTest.cxx b/Modules/Filtering/MathematicalMorphology/test/itkFlatMorphologyDegenerateRegionGTest.cxx new file mode 100644 index 00000000000..291bc6b7332 --- /dev/null +++ b/Modules/Filtering/MathematicalMorphology/test/itkFlatMorphologyDegenerateRegionGTest.cxx @@ -0,0 +1,97 @@ +/*========================================================================= + * + * 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 "itkFlatStructuringElement.h" +#include "itkGrayscaleDilateImageFilter.h" +#include "itkGrayscaleMorphologicalOpeningImageFilter.h" +#include "itkImageRegionConstIteratorWithIndex.h" +#include "itkGTest.h" + +namespace +{ +using ImageType = itk::Image; +using KernelType = itk::FlatStructuringElement<2>; +using AlgorithmEnum = itk::MathematicalMorphologyEnums::Algorithm; + +ImageType::Pointer +MakeImage(const ImageType::SizeType & size) +{ + auto image = ImageType::New(); + image->SetRegions(ImageType::RegionType(size)); + image->Allocate(); + float value = 0.0f; + for (float *pixel = image->GetBufferPointer(), *end = pixel + image->GetBufferedRegion().GetNumberOfPixels(); + pixel != end; + ++pixel) + { + *pixel = value; + value = (value >= 4.0f) ? 0.0f : value + 1.0f; + } + return image; +} + +template +ImageType::Pointer +Filter(const ImageType * input, const KernelType::RadiusType & radius, AlgorithmEnum algorithm) +{ + auto filter = TFilter::New(); + filter->SetInput(input); + filter->SetKernel(KernelType::Box(radius)); + filter->SetAlgorithm(algorithm); + filter->Update(); + return filter->GetOutput(); +} + +// Regions with size 1 along a dimension arise from thin images and from per-thread regions. +template +void +ExpectSameAsBasicAlgorithm() +{ + const ImageType::SizeType sizes[] = { { { 16, 1 } }, { { 1, 16 } }, { { 16, 3 } }, { { 3, 16 } } }; + const KernelType::RadiusType radii[] = { { { 1, 1 } }, { { 1, 0 } }, { { 0, 1 } } }; + for (const auto algorithm : { AlgorithmEnum::ANCHOR, AlgorithmEnum::VHGW }) + { + for (const auto & size : sizes) + { + for (const auto & radius : radii) + { + SCOPED_TRACE(testing::Message() << algorithm << " size " << size << " radius " << radius); + const auto input = MakeImage(size); + const auto actual = Filter(input, radius, algorithm); + const auto expected = Filter(input, radius, AlgorithmEnum::BASIC); + for (itk::ImageRegionConstIteratorWithIndex it(expected, expected->GetBufferedRegion()); + !it.IsAtEnd(); + ++it) + { + EXPECT_EQ(actual->GetPixel(it.GetIndex()), it.Get()) << "at " << it.GetIndex(); + } + } + } + } +} +} // namespace + +TEST(FlatMorphologyDegenerateRegion, DilateMatchesBasicAlgorithm) +{ + ExpectSameAsBasicAlgorithm>(); +} + +TEST(FlatMorphologyDegenerateRegion, OpeningMatchesBasicAlgorithm) +{ + ExpectSameAsBasicAlgorithm>(); +} diff --git a/Modules/Filtering/MathematicalMorphology/test/itkReconstructionImageFilterGTest.cxx b/Modules/Filtering/MathematicalMorphology/test/itkReconstructionImageFilterGTest.cxx new file mode 100644 index 00000000000..ca7c0bad980 --- /dev/null +++ b/Modules/Filtering/MathematicalMorphology/test/itkReconstructionImageFilterGTest.cxx @@ -0,0 +1,64 @@ +/*========================================================================= + * + * 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 "itkReconstructionByDilationImageFilter.h" +#include "itkReconstructionByErosionImageFilter.h" +#include "itkGTest.h" + +#include + +namespace +{ +using ImageType = itk::Image; + +ImageType::Pointer +MakeImage(unsigned char value) +{ + auto image = ImageType::New(); + image->SetRegions(ImageType::RegionType(ImageType::SizeType::Filled(4))); + image->Allocate(); + image->FillBuffer(value); + return image; +} + +template +std::string +GetPreconditionMessage(unsigned char marker, unsigned char mask) +{ + auto filter = TFilter::New(); + filter->SetMarkerImage(MakeImage(marker)); + filter->SetMaskImage(MakeImage(mask)); + try + { + filter->Update(); + } + catch (const itk::ExceptionObject & e) + { + return e.GetDescription(); + } + return {}; +} +} // namespace + +TEST(ReconstructionImageFilter, PreconditionMessageStatesRequiredOrder) +{ + EXPECT_EQ((GetPreconditionMessage>(1, 2)), + "Marker pixels must be >= mask pixels."); + EXPECT_EQ((GetPreconditionMessage>(2, 1)), + "Marker pixels must be <= mask pixels."); +}