Skip to content

Reading DICOM-RT in R using RadOnc

February 1, 2017

Previously I’ve written my own R code to access DICOM-RT structure sets in group 3006 of the meta-data. Shortly after I wrote that original post, Reid F. Thompson made his R package RadOnc available on CRAN. Unfortunately, my old code no longer works with the current version of the oro.dicom R package, therefore I would recommend using RadOnc instead. The code below is focused on importing the 3D geometry, but the R package has a lot of other features that you might find useful: for example, calculation of Dice similarity coefficient and Hausdorff distance; as well as import of dose-volume histograms (DVH).

The example DICOM-RT image used in this code is available from here. The code runs fine on Windows, but in order to run it on OS X you will need to install an X-Windows server such as XQuartz. I’m currently running Mac OS 10.9.5 (Mavericks) with XQuartz 2.7.11. You will require administrator privileges to install this on your Mac. If you get an error message about /opt/X11/lib/libGLU.1.dylib, that will be why.

## Loading required package: geometry
## Loading required package: magic
## Loading required package: abind
## Loading required package: oro.dicom
## oro.dicom: Rigorous - DICOM Input / Output (version = 0.5.0)
## Loading required package: ptinpoly
## Loading required package: misc3d
rtstruct <- read.DICOM.RT(path="~/Downloads/ITK4SampleDCMRT/", modality="MR", verbose=TRUE, DVH=FALSE, modality="MR")
## Reading 61 DICOM files from path: '~/Downloads/ITK4SampleDCMRT/' ... FINISHED
## Extracting MR data ... FINISHED [60 slices, 1.5x1.5x3.0mm res]
## Warning in read.DICOM.RT(path = "~/Downloads/ITK4SampleDCMRT/", verbose =
## TRUE, : Unable to extract DVH data from DICOM-RT (no dose grid available)
## Reading structure set from file: '/Users/stsrjs/Downloads/ITK4SampleDCMRT//RS.' ... (7 structures identified)
## Warning in read.DICOM.RT(path = "~/Downloads/ITK4SampleDCMRT/", verbose =
## TRUE, : Structure(s) 'GS1', 'GS2', 'GS3' are empty
## Processing (7) structures:
##   JP: MRI_RECTUM_RT [32 axial slice(s), 5784 point(s)] ... FINISHED
##   JP: MRI_BONEOUTER_RT [182 axial slice(s), 77052 point(s)] ... FINISHED
##   JP: MRI_BLADDER_RT [23 axial slice(s), 11058 point(s)] ... FINISHED
##   JP: MRI_PROSTATE_RO [12 axial slice(s), 3024 point(s)] ... FINISHED
## [1] "RT data '~/Downloads/ITK4SampleDCMRT/' containing CT image (256x256x60), 7 structure(s)"
## [1] "List containing 7 structure3D objects (GS1 JP, MRI_RECTUM_RT JP, MRI_BONEOUTER_RT JP, GS2 JP, GS3 JP, MRI_BLADDER_RT JP, MRI_PROSTATE_RO JP)"

Interactive 3D visualisation of the bony anatomy contours

The interactive 3D visualisation using rgl is very nice, but it would also be helpful to be able to scroll through the axial slices one at a time. For example, see my earlier post about misc3d:

## Loading required package: tcltk

## <environment: 0x7fa20396f898>


Broswing axial slices of the MRI interactively using Tcl/Tk

The slices3d function does support overlays, so this seems like it would be fairly straightforward to do.

The following code demonstrates how to calculate the Hausdorff distance between the bladder and prostate and plot a visualisation of the contours:

compareStructures(rtstruct$structures[c(6,7)], method="hausdorff", hausdorff.method="mean")
## Analyzing structure 1/2 (MRI_BLADDER_RT JP) ...
## Analyzing structure 2/2 (MRI_PROSTATE_RO JP) ... FINISHED
##                    MRI_BLADDER_RT JP MRI_PROSTATE_RO JP
## MRI_BLADDER_RT JP            0.00000           29.46258
## MRI_PROSTATE_RO JP          29.46258            0.00000

Previously, the best way to quantify volumetric differences would be to use something like the SlicerRT extension for ITK. However, you then still needed to import that quantification into R for statistical modelling. Being able to do everything in R makes it much easier to follow best practices for reproducible research (such as this blog post).

It is also possible to plot the contours, slice by slice:

compareStructures(rtstruct$structures[c(6,7)], method="axial", plot=TRUE)

Contours of the bladder and prostate

Admittedly, this is quite an artificial example, since the usual use case would be to compare two contours of the same patient. For example, to quantify volumetric changes over time. The RadOnc package has an excellent vignette that goes into more detail than what I’ve done here. There is also the 2014 journal article by Reid Thompson (details below). My original code is useful if you want to study the DICOM standard itself, but of course you need to use version 0.3-7 of oro.dicom to get it to work. The R package RadOnc is actively maintained and compatible with the latest versions of other packages on CRAN. It also offers additional features that are very handy for anyone who wants to perform statistical analysis of radiotherapy contours and dosimetry.

Further Reading

Dice, L.R. (1945) “Measures of the amount of ecologic association between species” Ecology 26(3): 297-302.

Dowling, J.; Malaterre, M; Greer, P.B. & Salvado, O. (2009) “Importing Contours from DICOM-RT Structure SetsThe Insight Journal

Hargrave, C.E.; Mason, N.; Guidi, R.; Miller, J-A; Becker, J.; Moores, M.T.; Mengersen, K.; Poulsen, M. & Harden, F. (2016) “Automated replication of cone beam CT-guided treatments in the Pinnacle³ treatment planning system for adaptive radiotherapy” J. Med. Rad. Sci. 63(1): 48-58.

Hausdorff, F. (1914) “Grundzüge der Mengenlehre” Veit and Company, Leipzig.

Moores, M.T.; Hargrave, C.E.; Deegan, T.; Poulsen, M.; Harden, F. & Mengersen, K. (2015) “An external field prior for the hidden Potts model with application to cone-beam computed tomography” Comput. Stat. Data Anal. 86: 27-41.

Pinter, C.; Lasso, A.;Wang, A.; Jaffray, D. & Fichtinger, G. (2012) “SlicerRT: Radiation therapy research toolkit for 3D Slicer” Med. Phys. 39(10): 6332–6338.

Thompson, R.F. (2014) “RadOnc: An R Package for Analysis of Dose-Volume Histogram and Three-Dimensional Structural DataJ. Rad. Onc. Info. 6(1): 98-100.

Whitcher, B.; Schmid, V.J. & Thornton, A. (2011) “Working with the DICOM and NIfTI Data Standards in R” J. Statist. Soft. 44(6): 1-28.

From → R

  1. Joseph permalink

    Hi Matt!

    Thanks for the tutorial. Just 2 things:

    1) [typo] modality=”MR” is repeated on the line `rtstruct <- read.DICOM.RT…`
    2) [error] On this line `plot(rtstruct$structures[[3]])`, I get the following error: `Error in plot.window(…) : need finite 'xlim' values`.

    Any pointers? Because, up until this point, the code works exactly. I am using the same data set.


  2. Joseph permalink

    Sorry, my bad. The error is a result of a typo. Please discard!

  3. Jacob permalink

    Hi Matt! Thank you very much for your tutorial. Do you know if there is any way to import .roi (region of interest) into R and apply it on a CT-image e.g to calculate mean HU of a region of interest?

    • Hi Jacob, what format would these .roi files be in? The nice thing about DICOM-RT is that you can match the coordinate reference system of the image stack (eg. a CT scan) to the coordinates of the contours of each axial slice. It takes a bit of work to convert RTSTRUCT contours into a bitmask, but it is possible. You can then convert the bitmask into array indices using which(bitmask==1, arr.ind=TRUE)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s


Computational Bayesian statistics

Bayes' Food Cake

A bit of statistics, a bit of cakes. - Blogs to Learn R from the Community

Computational Bayesian statistics

Richard Everitt's blog

Computational Bayesian statistics

Let's Look at the Figures

David Firth's blog

Nicholas Tierney

Computational Bayesian statistics

Sweet Tea, Science

Two southern scientistas will be bringing you all that is awesome in STEM as we complete our PhDs. Ecology, statistics, sass.

Mad (Data) Scientist

Musings, useful code etc. on R and data science

Darren Wilkinson's blog

Statistics, computing, functional programming, data science, Bayes, stochastic modelling, systems biology and bioinformatics

(badness 10000)

Computational Bayesian statistics

Igor Kromin

Computational Bayesian statistics


I can't get no

Xi'an's Og

an attempt at bloggin, nothing more...

Sam Clifford

Postdoctoral Fellow, Bayesian Statistics, Aerosol Science

%d bloggers like this: