Getting the contours of a DICOM structure as an array in Python

So, if I have an image (CT, MRI, etc.) or even a dose from radiation therapy, I can pull the doses or values ​​of the images into an array through:

import dicom ds = dicom.read_file("dicom_file.dcm") print ds.pixel_array 

It is quite simple, and gives me the ability to manipulate images / doses as I want. However, often you also have a structure file that contains various contour structures that you can see in the image viewer or something like that. Again, pretty simple.

My problem is that I will also like these separate structures as an array. And if I run the same code, I just get TypeError: No pixel data found in this dataset.

I assume that the structure of the DICOM files is not “made” in the same way as the DICOM files with doses / images.

So is there a solution that I could not find? I also looked at the dicompyler_core package, but from what I could see, there was no way to “simply” output the various structures into arrays.

+8
python arrays dicom pydicom
source share
2 answers

Here is an interactive session illustrating the location of the data using the rtstruct.dcm file included in pydicom:

 >>> import dicom >>> ds = dicom.read_file("rtstruct.dcm", force=True) >>> ds.dir("contour") ['ROIContourSequence'] >>> ctrs = ds.ROIContourSequence >>> ctrs[0] (3006, 002a) ROI Display Color IS: ['220', '160', '120'] (3006, 0040) Contour Sequence 3 item(s) ---- (3006, 0042) Contour Geometric Type CS: 'CLOSED_PLANAR' (3006, 0046) Number of Contour Points IS: '5' (3006, 0048) Contour Number IS: '1' (3006, 0050) Contour Data DS: ['-200.0', '150.0', '-20 0.0', '-200.0', '-150.0', '-200.0', '200.0', '-150.0', '-200.0', '200.0', '150.0 ', '-200.0', '-200.0', '150.0', '-200.0'] --------- (3006, 0042) Contour Geometric Type CS: 'CLOSED_PLANAR' (3006, 0046) Number of Contour Points IS: '6' (3006, 0048) Contour Number IS: '2' (3006, 0050) Contour Data DS: ['200.0', '-0.0', '-190. 0', '200.0', '-150.0', '-190.0', '-200.0', '-150.0', '-190.0', '-200.0', '150.0' , '-190.0', '200.0', '150.0', '-190.0', '200.0', '-0.0', '-190.0'] --------- (3006, 0042) Contour Geometric Type CS: 'CLOSED_PLANAR' (3006, 0046) Number of Contour Points IS: '6' (3006, 0048) Contour Number IS: '3' (3006, 0050) Contour Data DS: ['200.0', '-0.0', '-180. 0', '200.0', '-150.0', '-180.0', '-200.0', '-150.0', '-180.0', '-200.0', '150.0' , '-180.0', '200.0', '150.0', '-180.0', '200.0', '-0.0', '-180.0'] --------- (3006, 0084) Referenced ROI Number IS: '1' 

Data is saved (in this case, as usual) as a set of coordinates for each plane. To get data for one contour, for one plane you can use

 >>> ctrs[0].ContourSequence[0].ContourData ['-200.0', '150.0', '-200.0', '-200.0', '-150.0', '-200.0', '200.0', '-150.0', ' -200.0', '200.0', '150.0', '-200.0', '-200.0', '150.0', '-200.0'] 

These are the triplets of coordinates (x, y, z) one after another.

You can find additional information about each circuit (name, etc.) in the sequence StructureSetROISequence , for the index indicated by the ROI reference number.

You can get a complete array for all of them by going through each dataset in ContourSequence for that particular contour and adding them together into one array.

+3
source share

I created a class that takes the dicom folder with the RT file and creates a set of masks based on what you need.

https://github.com/brianmanderson/Dicom_Utilities

Hope this helps

0
source share

All Articles