This article will be the first article in the Ruby on Medicine (RoM) series focused on how Ruby can be applied to the medical domain.

In this tutorial, I will show you how to convert a DICOM file to a JPG image, which allows us to perform different operations on that image, which we will see in upcoming articles of this series.

The operating system I use for this tutorial is Ubuntu 14.10. If you are using another OS, don’t worry. I will refer you to the links that may be useful to use in the appropriate sections below.

What is DICOM?

Many of you are probably not too familiar with medical terms. That’s totally fine. Just to get you through this fray, let me start by introducing DICOM, the file format used as a starting point for this article.

I really liked this post by Roni, where he introduces DICOM (Digital Imaging and Communications in Medicine). I’ll use Roni’s post to briefly describe DICOM here.

Surely you’ve heard of terms like X-Ray, Ultrasound, CT, and MRI. Such medical images support DICOM and use it extensively. The core of the DICOM file specification is both a file format and a networking protocol.

All medical images are saved in such the DICOM format (i.e. dcm). This format not only includes images, but it also includes the patient’s information (i.e., name, gender, birth date), the equipment data (meaning, the device used), and other information.

The DICOM network protocol is used by medical applications connected to a hospital network to exchange information. It is also used to search and restore studies in the archive.

Of course, there is much more to DICOM, but it is beyond the scope of this article.

Why Convert DICOM to JPG?

Why convert a DICOM image to an image format like JPG? This are several potential reasons, some of which are:

DICOM files are known for their large sizes. Since we aim to work with the image part of the file only, converting to a JPG will both give an image and reduce file size. Obviously, this makes it much easier to share online.

As previously mentioned, DICOM files contain important data about patients. Thus, when transferring such files online, this sensitive information is exposed. This could be a violation of patient privacy. When we convert the DICOM file to an image, we are making the image anonymous, as the patient’s information is no longer there.

One of the aims of this series is to perform different image processing and computer vision tasks on medical images, like detecting a tumor, for instance. In these cases, the image part of the DICOM file serves the purpose. Almost anything can read or manipulate a JPG.

Setup

Let’s get started by making sure the environment has everything needed.

Ruby

If you don’t have Ruby installed, run the following command in the terminal to install Ruby:

sudo-apt get install ruby-full

The following command will display the version of Ruby version installed:

ruby -v

It should look something like this:

ruby 2.1.2p95 (2014-05-08) [x86_64-linux-gnu]

If you’re not on Ubuntu, install Ruby for your OS with the link below:

Ruby DICOM

Ruby DICOM is the library we will be using to convert DICOM to JPG.

From Ruby DICOM’s GitHub page:

Ruby DICOM is a small and simple library for handling DICOM in Ruby. DICOM (Digital Imaging and Communications in Medicine) is a standard for handling, storing, printing, and transmitting information in medical imaging. It includes a file format definition and a network communications protocol. Ruby DICOM supports reading from, editing and writing to this file format. It also features basic support for select network communication modalities like querying, moving, sending and receiving files.

To install Ruby DICOM, run the following command in the terminal:

Ubuntu: sudo gem install dicom

Windows: gem install dicom

Mac OS X: gem install dicom

RMagick

Viewing the converted image using Ruby, means it is time to install RMagick.

But, what is RMagick? Based on RMagick’s user guide:

RMagick is a binding from Ruby to the ImageMagickTM image manipulation library. ImageMagickTM is a free software suite to create, edit, and compose bitmap images. It can read, convert and write images in a large variety of formats. Images can be cropped, colors can be changed, various effects can be applied, images can be rotated and combined, and text, lines, polygons, ellipses and Bézier curves can be added to images and stretched and rotated.

RMagick is a complete interface to ImageMagick. Version 1.0.0 was released on February, 2003. Within its four major classes and 18 minor classes, RMagick defines over 650 methods and 350 constants. RMagick exploits Ruby idioms such as blocks and iterators, Struct classes, symbols, ?- and !-suffixed methods, and exceptions.

In order to install RMagick, run the following commands in your terminal:

Now, you have a JPG image that can easily be used by different image processing operations. I’ll show you some of these operations in upcoming articles.

Before I wrap up this tutorial, I want to mention that you can convert the DICOM file to any image type you like (i.e. PNG, BMP). Just simply replace the .jpg part by the image file with your extension of choice.

Are there any specific topics you would like to see in this series? If so, leave them in the comments below.

What I find interesting and frustrating about DICOM is many imagers include the HUD in the actual image component of the file. DICOM is full of metadata, and, for example, the Phillips ultrasound units our baby scans have been done with include patient name and data in the image.

Loading these images up in something like OsiriX (a DICOM viewer for mac) then go ahead and superimpose the metadata in the DICOM file over the metadata visually-encoded in the image . Yo dawg, I herd u liek metadata! Sigh.