Repository navigation
Description
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:
- Go to '...'
- Install '....'
- 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.
Activity
wyli commentedon Sep 27, 2023
Loading DICOM seg object is not supported.. @ericspod @vikashg would be great to understand the current best practice, any recommendations for offline preprocessing?
wyli commentedon Sep 27, 2023
one possibility is to use
segimage2itkimagefrom dcmqi https://qiicr.gitbook.io/dcmqi-guide/opening/cmd_tools/seg/segimage2itkimage converting the segmentation into niftithen use monai transforms to convert the seg.nii into the same space as the corresponding image:
mingxin-zheng commentedon Sep 27, 2023
Thanks @wyli for the investigation. This is close to what I have been doing now with the workaround script.
From what I have read from the segmentation object meta, it doesn't contain info of the original image. So it needs to info image (w, h, d) plus the affine to correctly align the label and the image.
ericspod commentedon Sep 27, 2023
I'm afraid for now this conversion is probably the best idea. We have been discussing the need for DICOM seg support with MONAI Deploy but haven't implementing support in core. @vikashg perhaps would have a better idea of seg software support.
KumoLiu commentedon Sep 28, 2023
An alternative way using pydicom_seg.