Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,11 @@ ReconstructionImageFilter<TInputImage, TOutputImage, TCompare>::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.");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

#include "itkImageRegionConstIterator.h"
#include "itkNeighborhoodAlgorithm.h"
#include <list>

namespace itk
{
Expand Down Expand Up @@ -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<RegionType>;
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<float>::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]);
Expand All @@ -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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: comments should describe the code as it is, not the approach it replaced.

Suggested change
// Select the face by DomDir, not by a size of 1: AllImage itself may have size 1 along other dimensions.
// AllImage may have size 1 along dimensions other than DomDir.

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<typename TInputImage::RegionType, TLine>(AllImage, *fit, line))
{
// std::cout << "Using face: " << *fit << line << std::endl;
RelevantRegion = *fit;
foundFace = true;
break;
}
}
RelevantRegion.SetIndex(DomDir,
AllImage.GetIndex(DomDir) + static_cast<IndexValueType>(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;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: NonFaceDim is only an alias for DomDir now; using DomDir directly in the lines below would read more plainly.


// figure out how much extra each other dimension needs to be extended
typename TInputImage::SizeType NewSize = RelevantRegion.GetSize();
Expand Down
8 changes: 7 additions & 1 deletion Modules/Filtering/MathematicalMorphology/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <algorithm>
#include <cmath>
#include <functional>
#include <vector>

namespace
{
using ImageType = itk::Image<float, 2>;
using KernelType = itk::FlatStructuringElement<2>;

// A plateau whose neighboring values differ by one ULP.
std::vector<float>
MakeUlpStaircase(float background, float direction)
{
std::vector<float> 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<float> & 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 <typename TFilter, typename TCompare>
void
ExpectExactExtremes(const std::vector<float> & 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<ImageType, KernelType>;
ExpectExactExtremes<FilterType>(MakeUlpStaircase(0.0f, 0.0f), std::greater<float>());
ExpectExactExtremes<FilterType>(MakeUlpStaircase(0.0f, 100.0f), std::greater<float>());
}

TEST(AnchorErodeDilateImageFilter, ErodeIsExactForNearlyEqualFloats)
{
using FilterType = itk::AnchorErodeImageFilter<ImageType, KernelType>;
ExpectExactExtremes<FilterType>(MakeUlpStaircase(100.0f, 0.0f), std::less<float>());
ExpectExactExtremes<FilterType>(MakeUlpStaircase(100.0f, 100.0f), std::less<float>());
}

TEST(AnchorErodeDilateImageFilter, ClosingByReconstructionOfNearlyEqualFloats)
{
for (const float direction : { 0.0f, 100.0f })
{
auto filter = itk::ClosingByReconstructionImageFilter<ImageType, ImageType, KernelType>::New();
filter->SetInput(MakeImage(MakeUlpStaircase(0.0f, direction)));
filter->SetKernel(KernelType::Box(KernelType::RadiusType::Filled(1)));
EXPECT_NO_THROW(filter->Update());
}
}
Original file line number Diff line number Diff line change
@@ -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<float, 2>;
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 <typename TFilter>
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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: the {16,3} / radius {1,0} case reaches 1-row per-thread regions only when the default work-unit count splits the image, so it depends on the runner's core count or ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS. Pinning it makes the per-thread case deterministic (the 16x1 and 1x16 cases already cover thin images on every runner):

Suggested change
filter->SetAlgorithm(algorithm);
filter->SetAlgorithm(algorithm);
filter->SetNumberOfWorkUnits(3);

filter->Update();
return filter->GetOutput();
}

// Regions with size 1 along a dimension arise from thin images and from per-thread regions.
template <typename TFilter>
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<TFilter>(input, radius, algorithm);
const auto expected = Filter<TFilter>(input, radius, AlgorithmEnum::BASIC);
for (itk::ImageRegionConstIteratorWithIndex<ImageType> it(expected, expected->GetBufferedRegion());
!it.IsAtEnd();
++it)
{
EXPECT_EQ(actual->GetPixel(it.GetIndex()), it.Get()) << "at " << it.GetIndex();
}
}
}
}
}
} // namespace

TEST(FlatMorphologyDegenerateRegion, DilateMatchesBasicAlgorithm)
{
ExpectSameAsBasicAlgorithm<itk::GrayscaleDilateImageFilter<ImageType, ImageType, KernelType>>();
}

TEST(FlatMorphologyDegenerateRegion, OpeningMatchesBasicAlgorithm)
{
ExpectSameAsBasicAlgorithm<itk::GrayscaleMorphologicalOpeningImageFilter<ImageType, ImageType, KernelType>>();
}
Loading
Loading