How to convert DICOM images to PNG?

3

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

Stéphane

Posted 2019-10-30T09:37:34.490

Reputation: 201

1Why not do it in two steps: dctopgm8 000005.dcm 000005.pbm and convert 000005.pbm 000005.png ? – Ljm Dullaart – 2019-10-30T09:43:25.310

@LjmDullaart Because I'd rather understand what parameters I'm missing so I can do the conversion in a single step. – Stéphane – 2019-10-30T09:45:15.937

I don't really get what the problem is. Your last command is a single conversion command. The other commands, if still necessary, highlight what needs to be done. For a single command dcmj2pnm seems to provide what you're asking for? – Seth – 2019-10-30T09:53:04.513

@Seth I would like to convert the DICOM to PNG format with a single conversion command. – Stéphane – 2019-10-30T09:55:00.647

@Moab No, as you can see above, that technique does not work. The images are not converted correctly. The only one that seems to work correctly is the .pbm, but what I need is .png or .jpg. – Stéphane – 2019-10-30T14:23:17.137

It may not be able to be done in Linux, I see several windows utilities that claim to do it, but this one has to be complied>>>>>>https://support.dcmtk.org/docs/dcmj2pnm.html

– Moab – 2019-10-30T15:00:43.193

@Moab, no, that one is part of the usual dcmtk package on Ubuntu, but it doesn't convert the image correctly. See the images I posted in my question. So far, the only one that seems to work correctly is the .pbm converter. – Stéphane – 2019-10-30T15:10:42.727

@Seth I don't know how you can say that. I even included the images that resulted from running those commands. – Stéphane – 2019-10-31T08:37:55.090

Because you haven't. There is a j in that command that isn't in the other. While it's is possible that it will have the same result at least give it a shot. You could also experiment with the input commands for either command and the LUT options. Using verbose and image info might shed some more light on your input and what's going wrong during conversion. – Seth – 2019-10-31T09:25:04.377

Answers

2

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")

Dave Chen

Posted 2019-10-30T09:37:34.490

Reputation: 121

-1

If you can use GIMP https://www.gimp.org/ you can easily open image and export to other formats.

Why it didnt work with your converter is another question. I had same problem (not with this format or converter) that converter expected different bitness that sources image had (8-bit vs. 16-bit)

Rok

Posted 2019-10-30T09:37:34.490

Reputation: 1

1No, GIMP wont work, I have thousands of images I need to convert. And when I use GIMP on DICOM files, it opens them like the .pnm conversion sample I posted in the question, where the images are strangely inverted. – Stéphane – 2019-10-30T10:33:20.753

I opened your example in gimp and I think it is working http://prntscr.com/pq2s30 I also think GIMP can be used for batch convert but I am not 100% on this.

– Rok – 2019-10-30T10:42:45.547

No, that image is not correct. See the top of my question. It should look like that first image, while in gimp it looks like the 2nd image. – Stéphane – 2019-10-30T10:50:21.430