Read DICOM (.dcm) pixeldata

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
rullbandspelare
Posts: 5
Joined: 2017-11-23T00:45:38-07:00
Authentication code: 1152

Read DICOM (.dcm) pixeldata

Post by rullbandspelare »

Hi!
Any help is appreciated
I whant to extract the CT pixeldata for computation.

Code: Select all

> convert -compress none CT.dcm CT.pgm
Gives me a .pgm ASCII file that looks like this:
I get 15568(3CD0 Hex) instead of 973(03CD Hex). The Pixel value is not what I expected!

Code: Select all

"P2
512 512
65535
15568 15568 15568 15568 ..."
The Pixel Value is 973 and a HU value of -51 according to Fiji Dicom viewer.
The DICOM file pixels looks like this in a HEX editor.

Code: Select all

cd 03 cd 03 cd 03 

Part of the DICOM dump:

Code: Select all

...
 | (0002,0010) : UI   Len: 20     HeaderTransferSyntaxUID        Value: [1.2.840.10008.1.2.1]
...
 | (0028,0002) : US   Len: 2      SamplesperPixel                Value: [1]
 | (0028,0004) : CS   Len: 12     PhotometricInterpretation      Value: [MONOCHROME2 ]
 | (0028,0010) : US   Len: 2      Rows0028                       Value: [512]
 | (0028,0011) : US   Len: 2      Columns0028                    Value: [512]
 | (0028,0030) : DS   Len: 4      PixelSpacing                   Value: [1\1 ]
 | (0028,0100) : US   Len: 2      BitsAllocated0028              Value: [16]
 | (0028,0101) : US   Len: 2      BitsStored                     Value: [12]
 | (0028,0102) : US   Len: 2      HighBit                        Value: [11]
 | (0028,0103) : US   Len: 2      PixelRepresentation            Value: [0]
 | (0028,1050) : DS   Len: 6      WindowCenter                   Value: [50\300]
 | (0028,1051) : DS   Len: 8      WindowWidth                    Value: [350\2000]
 | (0028,1052) : DS   Len: 6      RescaleIntercept               Value: [-1024 ]
 | (0028,1053) : DS   Len: 2      RescaleSlope                   Value: [1 ]
 | (0028,1054) : LO   Len: 2      Rescaletype                    Value: [HU]
...
Thanks!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Read DICOM (.dcm) pixeldata

Post by snibgo »

I'm not a developer, but for those who are:

Please say what version of IM you are using, on what platform. What does "convert version" say?

I suppose the problem is reading the DCM, not writing the PGM, but can you confirm that? If you save to a PNG, does the image look good?
snibgo's IM pages: im.snibgo.com
rullbandspelare
Posts: 5
Joined: 2017-11-23T00:45:38-07:00
Authentication code: 1152

Re: Read DICOM (.dcm) pixeldata

Post by rullbandspelare »

Version: ImageMagick 7.0.7-11 Q16 x64 2017-11-12 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2015 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Visual C++: 180040629
Features: Cipher DPC HDRI
Delegates (built-in): bzlib cairo flif freetype jng jp2 jpeg lcms lqr openexr pangocairo png ps raw rsvg tiff webp xml zlib

Yes, a .png looks ok as does the .pgm. It looks like a X-ray image.
The problem is, i think that the levels are wrong.
Most conversion tools that convert from DICOM to image are for display on a monitor and look good for the human eye. But I do not whant any modifications of the levels while converting. I whant to keep the original CT values
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Read DICOM (.dcm) pixeldata

Post by snibgo »

In version 7, you should use "magick" not "convert".
rullbandspelare wrote:Yes, a .png looks ok as does the .pgm. It looks like a X-ray image.
Okay, so IM is reading the DCM okay (that's the hard part). Perhaps writing the PGM has a bug. Try this:

Code: Select all

f:\web\im>magick xc:gray(10%) xc:gray(20%) +append -compress None pgm:
P2
2 1
65535
6554 13107
Do you get the same output?
snibgo's IM pages: im.snibgo.com
rullbandspelare
Posts: 5
Joined: 2017-11-23T00:45:38-07:00
Authentication code: 1152

Re: Read DICOM (.dcm) pixeldata

Post by rullbandspelare »

HI!
Yes I get the same result as You.

I get the same pixel values if i convert with other tools too. But they are for best display for the human eye.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Read DICOM (.dcm) pixeldata

Post by snibgo »

Hmm. Reading DCM works, and writing PGM works, but they don't work together? Maybe IM thinks the DCM data isn't sRGB, and converts it. Try this:

Code: Select all

convert CT.dcm -set colorspace sRGB -compress none CT.pgm
snibgo's IM pages: im.snibgo.com
rullbandspelare
Posts: 5
Joined: 2017-11-23T00:45:38-07:00
Authentication code: 1152

Re: Read DICOM (.dcm) pixeldata

Post by rullbandspelare »

Sorry , no difference.
My thought was that some kind of windowing or LUT took place that changed the pixel values. Like -define dcm:display-range=reset or somethng with LittleEndian or signed/unsigned 16bit with 12bit stored.

The Dicom Image is Greyscale MONOCHROME2.

From dicom standard:
MONOCHROME2
Pixel data represent a single monochrome image plane. The minimum sample value is intended to be displayed as black after any VOI gray scale transformations have been performed. See PS3.4. This value may be used only when Samples per Pixel (0028,0002) has a value of 1.

Thanks!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Read DICOM (.dcm) pixeldata

Post by snibgo »

Having slept on this, I think there is no bug.

The data file is 12-bit, so the maximum value for each pixel is 2^12-1=4095. Your version of IM is Q16, so the maximum value is 2^16-1 = 65535.

When IM reads an image file, it expands or contracts values so the value for white is consistently 2^Q-1, so for Q16 the values are scaled to 0 to 65535. It has to do this, or algorithms won't work. That's the value you see when you write to PGM.

When you read the file with software that doesn't do that expansion, you get the 12-bit value.

Does that make sense?
rullbandspelare wrote:But I do not whant any modifications of the levels while converting. I whant to keep the original CT values
IM won't keep the original file values. If you want, you can divide them by 16: "-evaluate Divide 16".
snibgo's IM pages: im.snibgo.com
rullbandspelare
Posts: 5
Joined: 2017-11-23T00:45:38-07:00
Authentication code: 1152

Re: Read DICOM (.dcm) pixeldata

Post by rullbandspelare »

Thank You !That is realy good !
This will help very much!
:)
Post Reply