Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve conversion of DICOM-SEGs to ITK images in DICOMWeb datastore #1011

Merged
merged 7 commits into from
Sep 28, 2022
Merged
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
45 changes: 18 additions & 27 deletions monailabel/datastore/utils/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
import logging
import os
import pathlib
import shutil
import tempfile
import time

import numpy as np
import pydicom
import pydicom_seg
import SimpleITK
from monai.data import write_nifti
Expand All @@ -35,7 +35,7 @@ def dicom_to_nifti(series_dir, is_seg=False):
start = time.time()

if is_seg:
output_file = itk_dicom_seg_to_image(series_dir)
output_file = dicom_seg_to_itk_image(series_dir)
else:
# https://simpleitk.readthedocs.io/en/master/link_DicomConvert_docs.html
if os.path.isdir(series_dir) and len(os.listdir(series_dir)) > 1:
Expand Down Expand Up @@ -193,30 +193,21 @@ def itk_image_to_dicom_seg(label, series_dir, template):
return output_file


def itk_dicom_seg_to_image(label, output_type="nifti"):
# TODO:: Currently supports only one file
def dicom_seg_to_itk_image(label, output_ext=".seg.nrrd"):
filename = label if not os.path.isdir(label) else os.path.join(label, os.listdir(label)[0])
with tempfile.TemporaryDirectory() as output_dir:
command = "segimage2itkimage"
args = [
"--inputDICOM",
filename,
"--outputType",
output_type,
"--prefix",
"segment",
"--outputDirectory",
output_dir,
]
run_command(command, args)
output_files = [f for f in os.listdir(output_dir) if f.startswith("segment") and f.endswith(".nii.gz")]
if not output_files:
logger.warning(f"Failed to convert DICOM-SEG {label} to NIFTI")
return None

result_file = os.path.join(output_dir, output_files[0])
logger.info(f"Result/Output (NII) File: {result_file}")

output_file = tempfile.NamedTemporaryFile(suffix=".nii.gz").name
shutil.move(result_file, output_file)
return output_file
dcm = pydicom.dcmread(filename)
reader = pydicom_seg.MultiClassReader()
result = reader.read(dcm)
image = result.image

output_file = tempfile.NamedTemporaryFile(suffix=output_ext).name

SimpleITK.WriteImage(image, output_file, True)

if not os.path.exists(output_file):
logger.warning(f"Failed to convert DICOM-SEG {label} to ITK image")
return None

logger.info(f"Result/Output File: {output_file}")
return output_file
2 changes: 1 addition & 1 deletion monailabel/interfaces/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ def _download_dcmqi_tools(self):
target = os.path.join(self.app_dir, "bin")
os.makedirs(target, exist_ok=True)

dcmqi_tools = ["segimage2itkimage", "itkimage2segimage", "segimage2itkimage.exe", "itkimage2segimage.exe"]
dcmqi_tools = ["itkimage2segimage", "itkimage2segimage.exe"]
existing = [tool for tool in dcmqi_tools if shutil.which(tool) or os.path.exists(os.path.join(target, tool))]
logger.debug(f"Existing Tools: {existing}")

Expand Down