Read ROIs from Dicom file and convert to pixel index

I’m trying to extract ROIs from Dicom files and convert the coordinates from mm in the patient coordinate system to a pixel indexes on the CT 3D array. The ROIs are saved in their own dicom files, and were drawn on the CT scan.

I haven’t been able to locate a code example on how to this. Can someone help me with an example?

So far I have tried:
reader = sitk.ImageSeriesReader() reader.SetFileNames([PathToDicomFileWithROIs])
roi = reader.Execute()

But it throws the error:
RuntimeError: Exception thrown in SimpleITK ImageSeriesReader_Execute: ../../Code/IO/src/sitkImageReaderBase.cxx:107: sitk::ERROR: Unable to determine ImageIO reader for <PathToDicomFileWithROIs>

Hello @malteekj,

If your ROIs belong to the same DICOM series then just read them as you would any other DICOM series. Examples can be found in this SimpleITK Jupyter notebook describing image IO or in the read-the-docs examples.

Hi zivy,

Thanks for the quick answer. Sorry if it’s just me that’s a little dense, but I tried to move my .dcm file with the ROIs into the folder with my Dicom series for the CT scan, but the .dcm is not read when using the “GetGDCMSeriesFileNames” method:

dicom_names = reader.GetGDCMSeriesFileNames(pathDicomCT)

This returns a tuple with only the file names of the CT scan, but not the .dcm file of the ROIs. Notice that the ROI is not an image array, but (3006,0039) ROI Contour Sequence.

Again, sorry if it’s that’s slow, but I haven’t been able to find anything that specifically addresses ROIs from Dicom files, and would very much like to use this API. Is SimpleITK equipped to handle (3006,0039) ROI Contour Sequence?

Hello @malteekj,

When a directory contains multiple series and you call GetGDCMSeriesFileNames without specifying which series it will return the first one. To select a specific series:

reader = sitk.ImageSeriesReader()
series_IDs = reader.GetGDCMSeriesIDs(data_directory)
# read the last series in the directory
image = sitk.ReadImage(reader.GetGDCMSeriesFileNames(data_directory, series_IDs[-1]))

With respect to the ROI contour sequence, not sure about this as have never used it. @dchen has more experience with it, hopefully he can give some sound advice.

Yeah, I’ve been working on some code to convert DICOM ROI contour sequences to VTK polylines. It uses pydicom to read in DICOM RT.

You can look at my code here:

It’s not documented or well tested, but seems to work for the files I tried it on. The standalone script is in the tools subdirectory.