Converting DICOM files

From Wikistix
Jump to: navigation, search

I was lucky enough to receive some x-rays in DICOM (Digital Imaging and Communications in Medicine) format. As I generally live on UNIX systems, I looked for a way to convert these to something more generally available. Note that the process below will most likely only work for plain 2D greyscale images. 3D, 4D and 5D images can probably be converted using a similar, but more complex process.


Install the Grassroots DICOM Library package. On my system of choice, NetBSD, and enabling shared libs, the steps were something like:

$ cd /tmp
$ pax -rjf gdcm-2.2.1.tar.bz2
$ mkdir gdcm-build
$ cd gdcm-build
$ make -j 16 install
$ export PATH=${PATH}:/local/gdcm/bin


Install the libtiff package, we need raw2tiff. I'm assuming your packaging system of choice already has this.


Install the image manipulation toolkit, ImageMagick. I'm assuming your packaging system of choice already has this.


Grab the image dimensions, depth and type from the DICOM file. Extract the raw pixel data, then encapsulate that into a TIFF file to make it easier to manage. Finally use ImageMagick to convert into a reasonable PNG. Note that some playing with the leftshift may be necessary, or alternately, you can take the lazy approach and use -equalize. We'll assume the file is "input.dcm":

$ gdcmdump -i input.dcm
(0028,0002) US 1                                                  # 2,1 Samples per Pixel
(0028,0004) CS [MONOCHROME2 ]                                     # 12,1 Photometric Interpretation
(0028,0010) US 1760                                               # 2,1 Rows
(0028,0011) US 2140                                               # 2,1 Columns
(0028,0034) IS [1\1 ]                                             # 4,2 Pixel Aspect Ratio
(0028,0100) US 16                                                 # 2,1 Bits Allocated
(0028,0101) US 12                                                 # 2,1 Bits Stored
(0028,0102) US 11                                                 # 2,1 High Bit
(0028,0103) US 0                                                  # 2,1 Pixel Representation
$ gdcmraw -i input.dcm -o temp.raw
$ raw2tiff -p minisblack -w 2140 -l 1760 -d short -c zip temp.raw temp.tiff
$ convert temp.tiff -evaluate leftshift 4 -quality 100 -depth 4 temp.png

The resulting image was better than my brief attempts using GIMP to adjust levels, and has the benefit of easily being batched.

Misinformation found herein copyright Paul Ripke (aka “stix”)