Converting Medical Images from DICOM to JPG on Mac OS X

GE Centricity Viewer DVDs of radiology images (PET / MRI) are saved in DICOM format and come with software for viewing the images. In order to use these images in other places, it may be necessary to convert them to a more standard format.  I found one guide one sitepoint.com that covered the necessary steps, but I found that a number of details were missing in order to complete a successful installation on Mac OS X.

The following steps are what worked for me:

Prerequisites

  1. Install Xcode from App Store

    This is a prerequisite for compiling programs.

  2. Install Ruby

    Here’s a good guide:

    See tutsplus.com

  3. Install DICOM

    Now install dicom support for ruby (in terminal):

    $ gem install dicom

  4. Install Homebrew

    This is needed for the following two steps.

    $ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

    See brew.sh

  5. Install ImageMagick

    The default install does not include jpeg 2000 support by default, which is needed to work with DICOM files, so you will need to specify the jp2 option when you install it:

    $ brew install imagemagick --with-jp2

    See tsai.it

  6. Install pkg-config

    This is a prerequisite for the next step.

    $ brew install pkg-config

    See stackoverflow.com

  7. Install Rmagick Gem

    You may need to adjust the paths to match the current version numbers.

    $ sudo C_INCLUDE_PATH=/usr/local/Cellar/imagemagick/6.9.1-10/include/ImageMagick-6/ PKG_CONFIG_PATH=/usr/local/Cellar/imagemagick/6.9.1-10/lib/pkgconfig/ gem install rmagick

    See stackoverflow.com

Doing the conversion

  1. Ruby conversion script

    Create the following ruby program in a text editor and save it as dicom-convert.rb. This finds all matching files in the current directory and all subdirectories and converts each one to jpeg format. If the files have a .dcm file extension, use *.dcm, otherwise find a different wildcard (e.g. MRI*) that matches your files.

    require 'dicom'
    include DICOM
    dcimfiles = File.join("**", “*.dcm")
    filenames = Dir.glob(dcimfiles)
    filenames.each do |filename|
    dcm = DObject.read(filename)
    dcm_image = dcm.image
    dcm_image.normalize.write(filename + ".jpg")
    end
    exit
    

    Or if you wish to use the script manually on individual files, you can use:

    require 'dicom'
    include DICOM
    dcm = DObject.read("MRI000001.dcm")
    dcm_image = dcm.image
    dcm_image.normalize.write("MRI000001.jpg")
    end
    exit
    

    This is a useful resource if you need to customize the script further: ruby-doc.org

  2. Running the script

    Place the program in the root of your DICOM files directory, open terminal, change to that directory,and run the following command:

    $ ruby dicom-convert.rb

This was based upon a guide written on Sitepoint.com; however, I found that additional steps were required to get it to work successfully. The sitepoint guide also includes instructions for Windows and Linux.

2 Comments

Add Yours →

Leave a Reply