BUG: Reject class counts that overflow non-contiguous labels#6596
BUG: Reject class counts that overflow non-contiguous labels#6596physwkim wants to merge 1 commit into
Conversation
|
| Filename | Overview |
|---|---|
| Modules/Segmentation/Classifiers/include/itkScalarImageKmeansImageFilter.hxx | Adds a non-contiguous label representability check, but rejects the valid one-class bool output case. |
| Modules/Segmentation/Classifiers/itk-module.cmake | Adds ITKGoogleTest as a test dependency for the module. |
| Modules/Segmentation/Classifiers/test/CMakeLists.txt | Registers a GoogleTest driver for the new Classifiers test source. |
| Modules/Segmentation/Classifiers/test/itkScalarImageKmeansImageFilterGTest.cxx | Adds a regression test that expects too many non-contiguous labels to throw. |
Reviews (1): Last reviewed commit: "BUG: Reject class counts that overflow n..." | Re-trigger Greptile
8138a84 to
2c8a6ea
Compare
|
Second force-push = review fixes from a high-effort automated review: the precondition is now compiled only for integral output pixel types (casting a float/double |
42cc8a3 to
602a6a6
Compare
Non-contiguous label spacing divides the output pixel range by the class count; once that quotient drops below 2, the interval computation underflows in unsigned arithmetic and produces wrapped label values instead of a diagnosable error. Addresses item B14 of InsightSoftwareConsortium#6575. Co-Authored-By: Hans J. Johnson <hans-johnson@uiowa.edu>
602a6a6 to
cf1c5f2
Compare
|
Force-push |
Reject class counts whose non-contiguous labels cannot be represented in the output pixel type, instead of computing a wrapped label interval. Addresses B14 of #6575.
Root cause
itkScalarImageKmeansImageFilter.hxx:112-116:numberOfClassesism_InitialMeans.size(). For auint8output the quotient reaches 0 once the class count exceeds 255, and0 - 1in unsigned arithmetic giveslabelInterval == 0xFFFFFFFF; the laterlabel += labelInterval(:124) and the outside-region labellabelInterval * numberOfClasses(:189) then wrap. Short of full wraparound the interval already degenerates to 0 once the class count passes half the pixel range, collapsing every class label — and the outside label — onto the same value.VerifyPreconditions()only rejected an empty mean list.The fix adds the exact representability precondition (
2 * numberOfClasses <= NumericTraits<OutputPixelType>::max(), the condition under whichmax()/N - 1 >= 1holds) rather than clamping the arithmetic, so an unrepresentable configuration is diagnosed instead of silently producing indistinguishable labels.Local validation
New GoogleTest
ScalarImageKmeansImageFilter.NonContiguousLabelsTooManyClassesThrows(module had no GTest driver; one is added).Fail-before:
Expected: filter->Update() throws an exception of type itk::ExceptionObject. Actual: it throws nothing.Pass-after:
[ PASSED ] 1 test.ctest -L ITKClassifiers: identical pass/fail set before and after, verified against a pristine checkout (the failures are missing ExternalData baselines and KWStyle in this offline build tree, not regressions).itkScalarImageKmeansImageFilter3DTest, which exercisesUseNonContiguousLabels(true)on real data, passes both before and after.AI assistance