Skip to content

Mismatch between DICOM segmentation volume and original image #7055

@mingxin-zheng

Description

@mingxin-zheng

Describe the bug
Could be related to #6928

Background:

This issue appears when I am working with DICOM segmentations generated by OHIF viewer.
There are dicom images and segmentations (labels) in OHIF.
I can read the dicom images succesfully with MONAI, but the labels in DICOM segmentation "SE00000.dcm" are organized by invidual slices. So if I read it directly with LoadImaged, the volume sizes of image and label will be mismatch.

In my example below, the s0004 (total segmentator) has a image size of [255, 177, 440], but the "label" generated by the DICOM segmentation is (7987, 177, 255)

I have a script to fix this:

import os
import pydicom
import numpy as np
from monai.transforms import LoadImage, SaveImage
from monai.data.meta_tensor import MetaTensor

print("Reading")

# Read the DICOM directory
dicom_dir = "s0004 ANONYMOUS/Unknown Study/CT"
seg_file_path = "s0004 ANONYMOUS/Unknown Study/SEG s0004_seg/SE000000.dcm"
output_dir = "."
# CT/CT_trans.nii.gz contains in the seg in `output_dir`

dicom_img = LoadImage()(dicom_dir)
seg_vol = np.zeros(dicom_img.shape)
# print(seg_vol.shape)

# Read the segmentation DICOM file
seg_dcm = pydicom.dcmread(seg_file_path)
# Extract the seg_record from the segmentation DICOM file
seg_record = seg_dcm.pixel_array  # assuming that seg_dcm.pixel_array gives the segmentation record

print("Shape of the dicom seg: ", seg_record.shape)
print("Shaping")
# # Iterate over the PerFrameFunctionalGroupsSequence and update the seg_vol array
for i, frame_item in enumerate(seg_dcm.PerFrameFunctionalGroupsSequence):
    frame_content_seq = frame_item.FrameContentSequence[0]
    class_id, slice_index = frame_content_seq.DimensionIndexValues
    
    # Assuming seg_record[i, y, x] gives the i-th segmentation record for seg_vol[x, y, slice_index - 1]
    seg_slice = seg_record[i, :, :].T
    seg_vol[:, :, slice_index - 1][seg_slice > 0] = class_id

print("Writing")

seg = MetaTensor(seg_vol, affine=dicom_img.affine, meta=dicom_img.meta)
SaveImage(output_dir=output_dir, output_dtype="uint16")(seg)

print(f"Shape of DICOM read from {dicom_dir}")
print(dicom_img.shape)
seg_output = LoadImage()(os.path.join(output_dir, "CT", "CT_trans.nii.gz"))
print(f"Shape of nii.gz written to {output_dir}")
print(seg_output.shape)

To Reproduce
Steps to reproduce the behavior:

  1. Go to '...'
  2. Install '....'
  3. Run commands '....'

Below is the data and script to reproduce:
data_and_script.zip

Expected behavior
A clear and concise description of what you expected to happen.

Screenshots
If applicable, add screenshots to help explain your problem.

Environment

Ensuring you use the relevant python executable, please paste the output of:

python -c 'import monai; monai.config.print_debug_info()'

Additional context
Add any other context about the problem here.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions