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

  • 1
    Why not do it in two steps: dctopgm8 000005.dcm 000005.pbm and convert 000005.pbm 000005.png ? – Ljm Dullaart Oct 30 '19 at 9:43
  • @LjmDullaart Because I'd rather understand what parameters I'm missing so I can do the conversion in a single step. – Stéphane Oct 30 '19 at 9:45
  • 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 Oct 30 '19 at 9:53
  • @Seth I would like to convert the DICOM to PNG format with a single conversion command. – Stéphane Oct 30 '19 at 9:55
  • Already answered here>>>>>>>stackoverflow.com/questions/46968678/… – Moab Oct 30 '19 at 13:36

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")
| improve this answer | |

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)

| improve this answer | |
  • 2
    No, 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 Oct 30 '19 at 10:33
  • I opened your example in gimp and I think it is working prntscr.com/pq2s30 I also think GIMP can be used for batch convert but I am not 100% on this. – Rok Oct 30 '19 at 10:42
  • 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 Oct 30 '19 at 10:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.