diff --git a/monailabel/datastore/utils/convert.py b/monailabel/datastore/utils/convert.py index b990ba86c..48a7b26dc 100644 --- a/monailabel/datastore/utils/convert.py +++ b/monailabel/datastore/utils/convert.py @@ -206,6 +206,15 @@ def _highdicom_nifti_to_dicom_seg( # Default type code type_code = Code("78961009", "SCT", name) + # Same color source the ITK/dcmqi path uses (_itk_nifti_to_dicom_seg + # above): an explicit per-segment color from label_info, falling back + # to GENERIC_ANATOMY_COLORS by name. Without this, highdicom leaves + # RecommendedDisplayCIELabValue unset and every segment renders in + # whatever default/arbitrary palette the viewer picks -- the same + # user-visible symptom as #1751, just via this code path instead. + rgb = list(info.get("color", GENERIC_ANATOMY_COLORS.get(name, (255, 0, 0))))[0:3] + rgb = [int(x) for x in rgb] + # Create highdicom segment description # Use sequential segment numbers (1, 2, 3...) to match remapped pixel array seg_desc = hd.seg.SegmentDescription( @@ -217,6 +226,7 @@ def _highdicom_nifti_to_dicom_seg( name="MONAILABEL", family=codes.DCM.ArtificialIntelligence, version=model_name ), algorithm_type="AUTOMATIC", + display_color=hd.color.CIELabColor.from_rgb(*rgb), ) segment_descriptions.append(seg_desc) diff --git a/requirements.txt b/requirements.txt index 747f5d7d6..3ddeff667 100644 --- a/requirements.txt +++ b/requirements.txt @@ -25,7 +25,7 @@ expiring_dict==1.1.0 cachetools==5.3.3 watchdog==4.0.0 pydicom==3.0.2 -highdicom==0.26.1 +highdicom==0.27.0 pynetdicom==2.0.2 pynrrd==1.1.3 SimpleITK diff --git a/setup.cfg b/setup.cfg index a1a846106..bd2dbea19 100644 --- a/setup.cfg +++ b/setup.cfg @@ -51,7 +51,7 @@ install_requires = cachetools>=5.3.3 watchdog>=4.0.0 pydicom>=3.0.1 - highdicom>=0.26.1 + highdicom>=0.27.0 pynetdicom>=2.0.2 pynrrd>=1.1.3 SimpleITK diff --git a/tests/unit/datastore/test_convert.py b/tests/unit/datastore/test_convert.py index d94e38175..e9905dda6 100644 --- a/tests/unit/datastore/test_convert.py +++ b/tests/unit/datastore/test_convert.py @@ -13,6 +13,7 @@ import tempfile import unittest +import highdicom as hd import numpy as np import pydicom import SimpleITK as sitk @@ -702,6 +703,22 @@ def _test_multiple_segments_with_different_properties_impl(self, use_itk): assert hasattr(seg, "SegmentedPropertyCategoryCodeSequence") assert hasattr(seg, "SegmentedPropertyTypeCodeSequence") + # The highdicom path previously left RecommendedDisplayCIELabValue + # unset entirely (see #1751, same user-visible symptom via a + # different code path than that issue's ITK/dcmqi fix). Only + # asserted for the highdicom path here since dcmqi's own + # RGB->CIELab conversion on the ITK path isn't independently + # verified by this suite. + if not use_itk: + assert hasattr( + seg, "RecommendedDisplayCIELabValue" + ), f"Segment {i + 1}: missing RecommendedDisplayCIELabValue" + decoded_rgb = hd.color.CIELabColor.from_dicom_value(seg.RecommendedDisplayCIELabValue).to_rgb() + expected_rgb = tuple(expected_info["color"]) + assert all( + abs(a - b) <= 2 for a, b in zip(decoded_rgb, expected_rgb) + ), f"Segment {i + 1}: expected color close to {expected_rgb}, got {decoded_rgb}" + # Verify all 3 real segments are present in metadata segments_in_metadata = [seg.SegmentNumber for seg in real_segments]