Skip to content
Open
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
10 changes: 10 additions & 0 deletions monailabel/datastore/utils/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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),
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
)
segment_descriptions.append(seg_desc)

Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions tests/unit/datastore/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import tempfile
import unittest

import highdicom as hd
import numpy as np
import pydicom
import SimpleITK as sitk
Expand Down Expand Up @@ -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]

Expand Down