9.21. DICOM RTSTRUCT Writer Operator

This application is NOT for medical use.

9.21.1. Overview

This example application creates a DICOM RT Dataset, of modality type RTSTRUCT, from a segmentation mask image in MetaImage format, and optionally from the original DICOM series. It can also directly consume a text file containing the detected contours instead of the segmentation mask image. If the corresponding DICOM Part 10 files for the series are provided, the referenced SOP instance UIDs (and other attributes) will be set accordingly in the RTSTRUCT. The output is a DICOM instance of modality type RTSTRUCT stored in a DICOM Part 10 file.

9.21.2. Inputs

This application, in the form of a Docker container, expects an input folder (/input by default) and an optional DICOM file folder (/dcm). Both need to be mapped to the host folders when the Docker container is started.

In the /input folder, one of the following two types of files must be present:

  1. An image file in MetaImage format, which is the segmentation mask with a single label

  2. A JSON file that contains the nested list of contours

The /dcm folder contains the original DICOM instance files of the DICOM series used to generate the segmentation mask. If they are absent or missing some instances, the DICOM RT Dataset can still be created, but the attributes referring to original SOP instances will be absent or have empty values.

9.21.3. Outputs

This application saves the DICOM RTSTRUCT to the output folder (/output by default) in DICOM File Format. The file name is generated by suffixing the input file name with -RTSTRUCT and the extension dcm. The output folder must be mapped to a host folder. Logs generated by the application are saved in a folder (/logs by default), which similarly must be mapped to a host folder.

9.21.4. Directory Structure

The directories in the container are shown below. The core of the application code is under the folder rtstruct_writer.

/app_rtstruct_writer
├── buildContainers.sh
├── Dockerfile
├── __init__.py
├── logging_config.json
├── main.py
├── ngc
│   ├── metadata.json
│   └── overview.md
├── public
│   └── docs
│       └── README.md
├── requirements.txt
├── rtstruct_writer
│   ├── app.py
│   ├── contour_detector.py
│   ├── dicom_parser.py
│   ├── __init__.py
│   ├── rtstruct_writer.py
│   └── runtime_envs.py
├── run_app_docker.sh
└── test-data
    ├── dcm
    │   ├── IMG0001.dcm
    │   ├── IMG0002.dcm
    │   ├── IMG0003.dcm
    │   ├── IMG0004.dcm
    │   ├── IMG0005.dcm
    │   ├── IMG0006.dcm
    │   ├── IMG0007.dcm
    │   ├── IMG0008.dcm
    │   ├── IMG0009.dcm
    │   ├── IMG0010.dcm
    │   ├── IMG0011.dcm
    │   ├── IMG0012.dcm
    │   ├── IMG0013.dcm
    │   ├── IMG0014.dcm
    │   ├── IMG0015.dcm
    │   ├── IMG0016.dcm
    │   ├── IMG0017.dcm
    │   ├── IMG0018.dcm
    │   └── IMG0019.dcm
    ├── json
    │   └── 1.2.826.0.1.3680043.2.1125.1.48532560258338587890405155270906492.output.json
    └── mhd
        ├── 1.2.826.0.1.3680043.2.1125.1.48532560258338587890405155270906492.output.mhd
        └── 1.2.826.0.1.3680043.2.1125.1.48532560258338587890405155270906492.output.raw

9.21.5. Executing the Operator Locally

If you want to see the internals of the container and manually run the application, follow these steps:

  1. Start the container in interactive mode. See the next section on how to run the container, and replace the docker run command with docker run --entrypoint /bin/bash.

  2. Once in the the Docker terminal, ensure the current directory is /app_rtstruct_writer.

  3. Copy test-data/input/mhd/* to /input and test-data/dcm/* to /dcm.

  4. Create /output and /logsfolders.

  5. Enter the command python ./main.py".

  6. Once finished, type exit.

9.21.6. Executing Operator in Docker

9.21.6.1. Prerequisites

  • The segmentation mask image file in MetaImage format

  • The original dcm files from the single DICOM series used for the segmentation

9.21.6.2. Step 1

Change to your working directory (e.g. my_test).

9.21.6.3. Step 2

Create, if they do not exist, the following directories under your working directory:

  • input and copy over the segmentation mask image file.

  • dcm and copy over the dcm files of the original DICOM series.

  • output for the generated DICOM RTSTRUCT dcm file.

  • logs for log files.

9.21.6.4. Step 3

In your working directory, create a shell script (e.g. run_app_docker.sh or other name if you prefer), copy the sample content below, save it, and make sure the variable TAG has the same value as the actual container tag:

SCRIPT_DIR=$(dirname "$(readlink -f "$0")")
TESTDATA_DIR=$(readlink -f "${SCRIPT_DIR}"/test-data)
APP_NAME="app_rtstruct_writer"
INPUT_TYPE="mhd"   # Change to json if using detected contours

# Build Docker image
docker build -t ${APP_NAME} -f ${SCRIPT_DIR}/Dockerfile ${SCRIPT_DIR}

# Run ${APP_NAME} container.
docker run --name ${APP_NAME} -t --rm \
    -v ${TESTDATA_DIR}/${INPUT_TYPE}:/input \
    -v ${SCRIPT_DIR}/output:/output \
    -v ${SCRIPT_DIR}/logs:/logs \
    -v ${TESTDATA_DIR}/dcm:/dcm \
    -e DEBUG_VSCODE \
    -e DEBUG_VSCODE_PORT \
    -e NVIDIA_CLARA_NOSYNCLOCK=TRUE \
    ${APP_NAME}

echo "${APP_NAME} has finished."

9.21.6.5. Step 4

Execute the script below and wait for the application container to finish:

./run_app_docker.sh

9.21.6.6. Step 5

Check for the following output files:

  • The segmentation results in the output directory

  • A file with the same name as the input file, suffixed with -RTSTRUCT and the extension .dcm.

9.21.6.7. Step 6

To visualize the results, use 3D Slicer with RT Extension or a DICOM viewer that supports RTSTRUCT. For detailed steps, see the viewer documentation. The key steps are as follows:

  1. Import the DICOM RTSTRUCT dcm file as well as the original DICOM series.

  2. Load both the original and the RTSTRUCT series.

  3. Scroll through the slices.

  4. To view 3D rendering, select the Volume Rendering module and enable the loaded volume.