Skip to content

Commit

Permalink
Allow non-consecutive segment numbers for labelmaps, or for any exist…
Browse files Browse the repository at this point in the history
…ing seg
  • Loading branch information
CPBridge committed Jan 4, 2025
1 parent 02a2abe commit 1bb9336
Show file tree
Hide file tree
Showing 3 changed files with 193 additions and 90 deletions.
33 changes: 26 additions & 7 deletions src/highdicom/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ def __init__(
apply_presentation_lut: bool = True,
apply_palette_color_lut: bool | None = None,
remove_palette_color_values: Sequence[int] | None = None,
palette_color_background_index: int = 0,
apply_icc_profile: bool | None = None,
):
"""
Expand Down Expand Up @@ -270,9 +271,14 @@ def __init__(
transform is applied.
remove_palette_color_values: Sequence[int] | None, optional
Remove values from the palette color LUT (if any) by altering the
LUT so that these values map to RGB(0, 0, 0) instead of their
original value. This is intended to remove segments from a palette
color labelmap segmentation.
LUT so that these values map to the RGB value at position
``palette_color_background_index`` instead of their original value.
This is intended to remove segments from a palette color labelmap
segmentation.
palette_color_background_index: int, optional
The index (i.e. input) of the palette color LUT that corresponds to
background. Relevant only if ``remove_palette_color_values`` is
provided.
apply_icc_profile: bool | None, optional
Whether colors should be corrected by applying an ICC
transformation. Will only be performed if metadata contain an
Expand Down Expand Up @@ -477,7 +483,13 @@ def __init__(
to_remove = np.array(
remove_palette_color_values
) - self._effective_lut_first_mapped_value
self._effective_lut_data[to_remove, :] = 0
target = (
palette_color_background_index -
self._effective_lut_first_mapped_value
)
self._effective_lut_data[
to_remove, :
] = self._effective_lut_data[target, :]

elif self._color_type == _ImageColorType.MONOCHROME:
# Create a list of all datasets to check for transforms for
Expand Down Expand Up @@ -2234,6 +2246,7 @@ def _get_pixels_by_frame(
apply_presentation_lut: bool = True,
apply_palette_color_lut: bool | None = None,
remove_palette_color_values: Sequence[int] | None = None,
palette_color_background_index: int = 0,
apply_icc_profile: bool | None = None,
) -> np.ndarray:
"""Construct a pixel array given an array of frame numbers.
Expand Down Expand Up @@ -2357,9 +2370,14 @@ def _get_pixels_by_frame(
transform is applied.
remove_palette_color_values: Sequence[int] | None, optional
Remove values from the palette color LUT (if any) by altering the
LUT so that these values map to RGB(0, 0, 0) instead of their
original value. This is intended to remove segments from a palette
color labelmap segmentation.
LUT so that these values map to the RGB value at position
``palette_color_background_index`` instead of their original value.
This is intended to remove segments from a palette color labelmap
segmentation.
palette_color_background_index: int, optional
The index (i.e. input) of the palette color LUT that corresponds to
background. Relevant only if ``remove_palette_color_values`` is
provided.
apply_icc_profile: bool | None, optional
Whether colors should be corrected by applying an ICC
transformation. Will only be performed if metadata contain an
Expand Down Expand Up @@ -2388,6 +2406,7 @@ def _get_pixels_by_frame(
apply_presentation_lut=apply_presentation_lut,
apply_palette_color_lut=apply_palette_color_lut,
remove_palette_color_values=remove_palette_color_values,
palette_color_background_index=palette_color_background_index,
apply_icc_profile=apply_icc_profile,
output_dtype=dtype,
)
Expand Down
6 changes: 4 additions & 2 deletions src/highdicom/seg/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,10 @@ def __init__(
""" # noqa: E501
super().__init__()
if segment_number < 1:
raise ValueError("Segment number must be a positive integer")
if segment_number < 1 or segment_number > 65535:
raise ValueError(
"Segment number must be a positive integer below 65536."
)
self.SegmentNumber = segment_number
self.SegmentLabel = segment_label
self.SegmentedPropertyCategoryCodeSequence = [
Expand Down
Loading

0 comments on commit 1bb9336

Please # to comment.