Skip to content

Interactive image stacks in R

September 19, 2013

I’ve been quiet for the last few months because the work I was doing on pseudolikelihood and critical temperature developed into a paper that I had to write up and submit. Hopefully this will be the second journal article of my PhD thesis by publication, not counting the short paper I’ve submitted to the ICCR 2013 proceedings, nor the 5 manuscript pages of lit review that I’ve contributed to a middle-author paper that’s currently in prep.

A reader of this blog sent me a question about my posts on DICOM (part 1, part 2): specifically how to interact with the DICOM image as a stack of 2D slices. There are two components of this: first, I store the voxel intensity values as a numeric 3D array in R; secondly, I use the excellent misc3d package by Dai Feng and Luke Tierney to scroll interactively through the axial slices. Details (and R code) after the jump:

The first step is to read the image data into R, just like we did before:

library(oro.dicom)
dcmImages <- readDICOM("/dev/DICOMRT2", recursive=FALSE, exclude="sql")
dcm.info <- dicomTable(dcmImages$hdr)
for (stack in unique(substring(rownames(dcm.info),15,20))) {
  if (substring(stack,1,2) == "CT") {
    print(stack)
    index <- which(substring(rownames(dcm.info),15,20) == stack)
    dcm.stack <- list(hdr=dcmImages$hdr[index], img=dcmImages$img[index])
    dcm.nifti <- dicom2nifti(dcm.stack, DIM=3, descrip=c("Manufacturer","ManufacturersModelName"))
    print(dcm.nifti)
  }
}

## [1] "CT4697"
## NIfTI-1 format
## Type : nifti
## Data Type : 4 (INT16)
## Bits per Pixel : 16
## Slice Code : 0 (Unknown)
## Intent Code : 0 (None)
## Qform Code : 2 (Aligned_Anat)
## Sform Code : 2 (Aligned_Anat)
## Dimension : 512 x 512 x 80
## Pixel Dimension : 0.88 x 0.88 x 2
## Voxel Units : mm
## Time Units : sec

The class nifti extends array, so we can apply any of the R array operations:

dim(dcm.nifti)

## [1] 512 512 80

min(dcm.nifti)

## [1] 0

sum(dcm.nifti == 0)/prod(dim(dcm.nifti))

## [1] 0.5581

Then the really cool trick is that it only takes a single command to display this (or any) array in an interactive window using Tcl/Tk

library(misc3d)
library(tkrplot)

## Loading required package: tcltk
## Loading Tcl/Tk interface ...
## done

slices3d(dcm.nifti)

It is straightforward to apply windowing or other image manipulations to the array prior to calling slices3d. The R package mritc contains routines for image segmentation as well.

Advertisements

From → Imaging, R

One Comment

Trackbacks & Pingbacks

  1. Reading DICOM-RT in R using RadOnc | Matt Moores

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s