Ruby on Medicine: Converting DICOM to JPG
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.
Let’s get started by making sure the environment has everything needed.
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:
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 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:
sudo gem install dicom
gem install dicom
- Mac OS X:
gem install dicom
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:
sudo apt-get install imagemagick sudo apt-get install libmagickwand-dev sudo gem install rmagick
Links for other OS:
With the environement ready to go, we need a DICOM file to call our very own. I have used the DIASTOLIX dataset (Alias Name) from here, and in particular, the file: IM-0004-0043.dcm
Viewing this DICOM file using a DICOM viewer like RadiAnt, we see something like the following:
The following script will convert the DICOM file to a JPG image:
require 'dicom' include DICOM dcm = DObject.read("IM-0004-0044.dcm") dcm_image = dcm.image dcm_image.normalize.write("IM-0004-0044.jpg") exit
Run this file using the syntax:
Which will output something like:
I, [2014-12-24T16:55:59.974029 #4056] INFO -- DICOM: DICOM file successfully read: IM-0004-0044.dcm
And, a new JPG file should be created. Display the created JPG image using RMagick comes to the play.
require 'RMagick' include Magick img = ImageList.new("IM-0004-0044.jpg") img.display
Putting it all together, the following script will convert DICOM to JPG and display the resulting image:
require 'RMagick' require 'dicom' include Magick include DICOM dcm = DObject.read("IM-0004-0044.dcm") dcm_image = dcm.image dcm_image.normalize.write("IM-0004-0044.jpg") img = ImageList.new("IM-0004-0044.jpg") img.display exit
When you run the program, you’ll see the following output:
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.