Linux – How to convert DICOM images to PNG

image processingimagemagicklinux

I have some DICOM medical imaging files downloaded from Cancer Imaging Archive. I can convert them from DICOM to several other formats, but as you'll see the conversion isn't working as expected in most cases.

These are the various conversions I've figured out so far:

  • DICOM to netpbm format: dctopgm8 000005.dcm 000005.pbm
  • DICOM to pnm format: dctopnm -byteorder little 000005.dcm 000005.pnm
  • DICOM to png format: dcm2pnm +on 000005.dcm 000005.png
  • DICOM to png format (via ImageMagick): convert 000005.dcm 000005.png

Of those, the .pbm is the only one that seems to give great results.
It looks like this:

pbm file

The .pnm looks like this, which is not quite an inverse image, but somehow looks wrong:

pnm file

And both of the .png conversions look like this, which is a very washed out image, perhaps due to problems with an alpha channel, gamma, or…?

png file

The problem is I need these to be in PNG, not PBM. And while I could add an additional conversion from PBM to PNG, I'd rather call convert only once and do the full conversion in a single command.

Anyone know what parameters I might be missing in the calls to dcm2pnm or ImageMagick's convert to get the images looking as expected?


Edit: including a sample .dcm image: 000005.dcm

Best Answer

It looks like you're trying to convert at 16-bit image to 8-bit. It looks like a CT image, where pixel values typically go from -1000 (air) to 0 (water) to 3000 (dense bone).

I'm guessing the PBM program is mapping the 16-bit to 8-bit by rescaling the pixel values. It looks like the PNM version is only taking the lower 8-bits and ignoring the upper 8. The PNG image probably has the entire 16 bit data, since PNG supports it, but your viewer displays only the upper 8-bits, ignoring the lower 8.

You need to rescale the pixel intensities to 0-255 from -32768-32767 (or 0 to 63356 if you view them as unsigned 16 bit ints).

You can do this using SimpleITK in Python like so:

import SimpleITK as sitk

img = sitk.ReadImage("000005.dcm")
# rescale intensity range from [-1000,1000] to [0,255]
img = sitk.IntensityWindowing(img, -1000, 1000, 0, 255)
# convert 16-bit pixels to 8-bit
img = sitk.Cast(img, sitk.sitkUInt8)

sitk.WriteImage(img, "000005.png")
Related Question