Skip to content

BUG: Fix anchor morphology for nearly equal floats and thin regions - #7256

Merged
hjmjohnson merged 3 commits into
InsightSoftwareConsortium:mainfrom
dzenanz:anchor-morphology-fixes
Nov 29, 2026
Merged

hjmjohnson merged 3 commits into
InsightSoftwareConsortium:mainfrom
dzenanz:anchor-morphology-fixes

Conversation

@dzenanz

@dzenanz dzenanz commented Nov 27, 2026

Copy link
Copy Markdown
Member

Anchor and van Herk/Gil-Werman morphology: exact float comparison (float dilation could fall below the input, making ClosingByReconstructionImageFilter throw), and correct output for regions 1 pixel thick. Also fixes inverted reconstruction precondition messages.

AI assistance

Claude Code (Claude Opus 5.5): root-cause analysis, fixes, and regression GTests. Tested locally with MathematicalMorphology, BinaryMathematicalMorphology, and LabelMap tests.

🤖 Generated with Claude Code

AnchorErodeDilateLine::Compare is the non-strict form of TCompare.
Math::AlmostEquals let a float neighbor a few ULPs on the wrong side
replace the running extreme, so anchor dilation could return less than
the input pixel. ClosingByReconstructionImageFilter then threw
"Marker pixels must be <= mask pixels." on float images.
Reconstruction by erosion requires marker >= mask, and by dilation
marker <= mask; the messages stated the opposite.
MakeEnlargedFace identified each face's normal as its size-1 dimension.
For a region with size 1 along another dimension, no face matched the
line, so anchor and van Herk/Gil-Werman erode/dilate/open/close left
the output unwritten. Such regions come from thin images and from
per-thread regions when the kernel radius is 0 along the split axis.
@github-actions github-actions Bot added type:Bug Inconsistencies or issues which will cause an incorrect result under some or all circumstances type:Infrastructure Infrastructure/ecosystem related changes, such as CMake or buildbots type:Testing Ensure that the purpose of a class is met/the results on a wide set of test cases are correct area:Filtering Issues affecting the Filtering module labels Nov 27, 2026
@dzenanz

dzenanz commented Nov 27, 2026

Copy link
Copy Markdown
Member Author

@greptileai review this

@greptile-apps

greptile-apps Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 5/5 · The PR appears safe to merge; no actionable defects were found.RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no actionable defects were found.

Summary

Fixes exact float comparisons in anchor morphology and face selection for regions one pixel thick.

  • Anchor filters choose exact extrema for nearly equal float pixels.
  • Morphology sweeps use the line direction to cover thin regions.
  • Reconstruction filters state the required marker and mask order.

No actionable issues were found. The review checked source code and test wiring; builds and tests were not run.

Reviews (2) · Last reviewed commit: "BUG: Select sweep face by line direction..." · Reviewed by Greptile

@dzenanz
dzenanz marked this pull request as ready for review November 27, 2026 21:18

@hjmjohnson hjmjohnson left a comment

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.

Thanks, @dzenanz. I reviewed all three commits and checked them locally; I found nothing blocking. All comments below are nits.

  • Tests: on macOS with clang (Release build), the 6 new GTests pass at 31d5187e9cb. With the base versions of itkAnchorErodeDilateLine.h, itkReconstructionImageFilter.hxx, and itkSharedMorphologyUtilities.hxx and the same tests, all 6 fail, each for the reason its commit message gives:
    • dilation lands a few ULPs below the input, for example 49.9999962 vs 50;
    • ClosingByReconstruction throws;
    • the degenerate-region outputs differ from BASIC;
    • the precondition message is inverted.
  • Face selection: for regions with every dimension > 1, the new MakeEnlargedFace picks the same face as NeedToDoFace did. That holds for diagonal and negative line directions too, so normal output is unchanged. All four callers (anchor erode/dilate, anchor open/close, and vHGW) go through it.
  • Reconstruction: the comparison logic was already right, and the messages now match it for both erosion and dilation.

Nits:

  • NeedToDoFace (itkSharedMorphologyUtilities.h:28) now has no callers in the tree. Since it is public, keep it or deprecate it, as you prefer. <list> is still included in the .h.
  • !compare(b, a) is true when either value is NaN, while the old compare || AlmostEquals was false. That is unlikely to matter, but one clause in the commit 1 body would record it.


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.

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.

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);

@hjmjohnson
hjmjohnson merged commit 7d73f2b into InsightSoftwareConsortium:main Nov 29, 2026
20 checks passed
@dzenanz
dzenanz deleted the anchor-morphology-fixes branch November 30, 2026 16:16
dzenanz added a commit to dzenanz/ITK that referenced this pull request Nov 30, 2026
Set the global default to 3 threads while constructing each filter, so
the 16x3 image always splits into 1-row per-thread regions. Composite
filters such as GrayscaleMorphologicalOpeningImageFilter construct
their inner filters with that default, so SetNumberOfWorkUnits on the
outer filter would not reach them. The test no longer depends on the
core count or ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS.

Also use DomDir directly in MakeEnlargedFace instead of its NonFaceDim
alias, and reword a comment to describe the current code.

Follow-up to review of InsightSoftwareConsortium#7256.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:Filtering Issues affecting the Filtering module type:Bug Inconsistencies or issues which will cause an incorrect result under some or all circumstances type:Infrastructure Infrastructure/ecosystem related changes, such as CMake or buildbots type:Testing Ensure that the purpose of a class is met/the results on a wide set of test cases are correct

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants