Skip to content

BUG: Fix LabelMap and label-statistics correctness bugs from issue 6575#6590

Open
hjmjohnson wants to merge 6 commits into
InsightSoftwareConsortium:mainfrom
hjmjohnson:bug-labelmap-6575
Open

BUG: Fix LabelMap and label-statistics correctness bugs from issue 6575#6590
hjmjohnson wants to merge 6 commits into
InsightSoftwareConsortium:mainfrom
hjmjohnson:bug-labelmap-6575

Conversation

@hjmjohnson

Copy link
Copy Markdown
Member

Fixes six LabelMap / label-statistics correctness bugs catalogued in #6575 (items B40–B45): one BUG: commit per item. All in-memory logic, no fixtures; three items ship dedicated fail-before/pass-after GoogleTests and the rest are covered by the module regression (136/136 local).

Per-item summary
Item Site Fix
B40 MergeLabelMapFilter (KEEP) The collision-deferred deque was declared outside the per-input loop and never cleared, so with ≥3 inputs an object deferred on one input was pushed again on the next — aliasing one object under two label keys. Declare the deque inside the loop.
B41 LabelMapContourOverlayImageFilter (SLICE_CONTOUR) The slice-radius guard tested the write cursor j against SliceDimension where it meant the read index i; SliceDimension == 0 left the radius all-zero and drew no contour. Test the read index.
B42 LabelMapMaskImageFilter (Crop) An empty/zero-line object set left the bounding-box min/max sentinels untouched, so maxs - mins + 1 overflowed (UB) and the filter allocated a 2×2 image at index 2⁶³−1. Use an empty region when no lines are found (both crop sites).
B43 AttributeUniqueLabelMapFilter The "remove objects without lines" pass used an inverted condition (while (it.IsAtEnd())) and never ran, so an object whose pixels were all claimed by a higher-priority object survived with zero lines (and fed the B42 crop path).
B44 LabelMap::PushLabelObject On a completely full label range the gap scan found no free label but the "map full" test (label == lastLabel) never held, so AddLabelObject silently overwrote the object's own entry. Track whether a free label was found and throw otherwise.
B45 StatisticsLabelMapFilter Weighted elongation/flatness used unclamped principal moments and one shared guard, so an indefinite weighted covariance from a signed feature image produced NaN where ShapeLabelMapFilter returns 0. Clamp the moments and guard each ratio, matching ShapeLabelMapFilter.
Testing

New itkLabelMapBugfixGTest.cxx adds three tests, each verified to fail on the unfixed tree and pass after:

  • B44 — a full 8-bit label range makes PushLabelObject throw (silently overwrote before).
  • B45 — a cross-shaped object with positive x-arm / negative y-arm feature weights gives an indefinite weighted covariance; weighted elongation/flatness stay finite (NaN before).
  • B43 — two fully-overlapping objects: uniqueness empties one, which is then removed (count 2 → 1; stayed 2 before).

B40/B41/B42 are covered by the existing ITKLabelMap + ITKImageFusion regression (136/136 pass locally); dedicated fail-before tests for those can be added on request. pre-commit run --all-files is clean.

Findings and analysis by the #6575 reporter; see that ledger for the full derivations.

In KEEP mode the deque of collision-deferred objects was declared outside
the per-input loop and drained without being cleared, so with three or
more inputs an object deferred while merging one input was pushed again
while merging the next -- aliasing one object under two label keys.
Declare the deque inside the loop.

Issue InsightSoftwareConsortium#6575, item B40.
…Overlay

The SLICE_CONTOUR slice-radius loop tested the write cursor against
SliceDimension where it meant the read index, so SliceDimension 0 left
the radius all-zero and drew no contour at all. Test the read index.

Issue InsightSoftwareConsortium#6575, item B41.
With no matching object lines the crop bounding box kept its min/max
sentinels, so the region size computation overflowed (undefined
behavior) and the filter allocated a 2x2 image at a nonsense index. Use
an empty region when no lines are found.

Issue InsightSoftwareConsortium#6575, item B42.
The removal loop condition was inverted (while (it.IsAtEnd())), so the
pass never ran and an object whose every pixel was claimed by a
higher-priority object survived with zero lines. Adds a GoogleTest.

Issue InsightSoftwareConsortium#6575, item B43.
The last-resort gap scan compared the walked label against lastLabel to
detect a full map, but on a completely full range that test never held,
so no free label was assigned and AddLabelObject silently overwrote the
object's own entry. Track whether a free label was found. Adds a test.

Issue InsightSoftwareConsortium#6575, item B44.
Weighted elongation and flatness used unclamped principal moments and a
single shared guard, so an indefinite weighted covariance from a signed
feature image produced NaN where ShapeLabelMapFilter returns 0. Clamp
the moments and guard each ratio, matching ShapeLabelMapFilter. Adds a
test.

Issue InsightSoftwareConsortium#6575, item B45.
@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 Jul 11, 2026
@hjmjohnson

Copy link
Copy Markdown
Member Author

@greptile review

@greptile-apps

greptile-apps Bot commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes several LabelMap and label-statistics correctness bugs. The main changes are:

  • Correct slice contour radius mapping for the skipped slice dimension.
  • Remove emptied label objects after uniqueness filtering.
  • Throw when PushLabelObject cannot find a free label.
  • Use an empty crop region when matching objects have no lines.
  • Clear deferred merge objects per input.
  • Clamp weighted principal moments and guard weighted shape ratios.
  • Add LabelMap regression tests for the covered cases.

Confidence Score: 5/5

This looks safe to merge.

No blocking issues found in the changed code.

No files need attention.

T-Rex T-Rex Logs

What T-Rex did

  • The build-environment evidence shows cmake, ctest, ninja, and pixi are unavailable even though /usr/bin/c++ exists.
  • An attempted CMake configure with the requested narrow flags ran and failed with 'cmake: command not found' and EXIT_CODE: 127.
  • The wiring-evidence confirms the new GTest file exists, contains B43/B44/B45 test declarations, and is wired into ITKLabelMapGTests.

View all artifacts

T-Rex Ran code and verified through T-Rex

Important Files Changed

Filename Overview
Modules/Filtering/ImageFusion/include/itkLabelMapContourOverlayImageFilter.hxx Corrects the contour thickness remapping to skip the selected slice dimension.
Modules/Filtering/LabelMap/include/itkAttributeUniqueLabelMapFilter.hxx Runs the removal pass for label objects that become empty during uniqueness filtering.
Modules/Filtering/LabelMap/include/itkLabelMap.hxx Adds explicit free-label tracking before adding a pushed label object.
Modules/Filtering/LabelMap/include/itkLabelMapMaskImageFilter.hxx Builds a zero-size crop region when no object lines contribute to the crop box.
Modules/Filtering/LabelMap/include/itkMergeLabelMapFilter.hxx Scopes deferred collision objects to each input during keep-mode merging.
Modules/Filtering/LabelMap/include/itkStatisticsLabelMapFilter.hxx Matches weighted shape ratio handling to the clamped principal moment behavior.
Modules/Filtering/LabelMap/test/CMakeLists.txt Registers the new LabelMap bugfix test source.
Modules/Filtering/LabelMap/test/itkLabelMapBugfixGTest.cxx Adds regression tests for full label ranges, signed weighted shape metrics, and emptied unique-label objects.

Reviews (3): Last reviewed commit: "BUG: Clamp weighted moments in Statistic..." | Re-trigger Greptile

@hjmjohnson

Copy link
Copy Markdown
Member Author

@greptileai review this draft before I make it official

@hjmjohnson hjmjohnson marked this pull request as ready for review July 12, 2026 02:05
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.

1 participant