DICOM RLE Support in Slicer

Hi everyone,
I was wondering if Slicer is able to convert the pixel data of a binary label map to DICOM Run-Length-Encoding (RLE). Does anyone know if this is possible and how?

Thanks for your help! much appreciated!

Yes, you can do this in Slicer. After you installed “Quantative Reporting” extension, you can export segmentation node to DICOM Segmentation Object.

Thanks for the answer!

I had looked into that module but I couldn’t find any trace of RLE. I will have a closer look but is there perhaps an example of c++/python code that I could look at?

It is implemented at one level lower, in DCMTK.

It seems that the default segmentation encoding is bit packing and not RLE (see here). @fedorov, do you remember why you did not end up exposing the RLE option on the itkimage2segimage interface?

Bit packing allows random access to any part of the segmentation without decompressing the entire volume and for very complex segmentations its compression ratio may be similar or better than RLE. Why would you prefer using RLE over bit packing?

1 Like

We added and tested reading of RLE-compressed segmentations, and did some experiments with writing, but I don’t recall if the result was documented. I think the main concern is that introducing compression will further complicate interoperability. Note that RLE would be applied after bit packing (for binary segmentations). Bit packing alone can be challenging for interoperability, and I am not convinced we should make it even more challenging.

No one asked about this before - it could be added as an option, but I would want to know how bit packing + RLE is supported in the existing tools before adding it.

1 Like

I have to convert bitmasks to RLE for a particular framework (detectron2) that uses on COCO data format. I use a python library called pycocotools to convert bitmask => RLE like so:

from pycocotools import mask
import numpy as np

# Example bitmask array. Replace with your actual bitmask.
bitmask = np.array([
    [0,0,0,0,0],
    [0,1,1,1,0],
    [0,1,0,1,0],
    [0,1,1,1,0],
    [0,0,0,0,0]
])

rle_mask = mask.encode(np.asarray(bitmask, order="F", dtype=np.uint8))

For more context this colab notebook shows how it is used in the training of a liver segmentation algorithm.

I don’t think that COCO’s RLE compression is related to DICOM. There are many RLE implementations and RLE codecs specified in the DICOM standard are completely different from what COCO uses.

A possible data flow would be:

  • Load each image from DICOM into 3D Slicer, segment it, and save both the image and segmentation in NRRD file format. In addition to this, you may also export the segmentations in DICOM segmentation objects for archival (DICOM segmentation object is more future-proof, because it stores referenced object UIDs, standard terminologies, etc. in standard fields).
  • To generate training data, you would convert the NRRD files to COCO json files with a small Python script.

I guess pydicom would be able to do the RLE too, right?

pydicom would do the same kind of RLE as DCMTK. I would not recommend to use RLE compression in your DICOM segmentation objects because it would lead to incompatibility with many software packages and would not help in any way with getting segmentations to COCO format (you would need to decompress the DICOM RLE, unpack the bits, and use COCO’s trivial RLE encoding).