Pydicom: how to read MRI dicom files?

Created on 12 Jun 2020  路  4Comments  路  Source: pydicom/pydicom

I have some MRI images, and I want to read data from dicom files.

It works for CT image, but fails to MRI image.

# U = m*SV + b
# where U is in output units, m is the rescale slope, SV is the stored value, and b is the rescale intercept.
ds = pydicom.dcmread('./test.dcm')
d = ds.pixel_array*ds.RescaleSlope + ds.RescaleIntercept
d = np.clip(d, -160, 240)
d = (d + 800)*255.0/400
d = d.astype(np.uint8)
cv2.imwrite("a2.bmp", d2)

What is the linear transformation function corresponding to the CT?

Note that RecaleSlope / RescaleIntercept are mandatory only for some kinds of images (mostly CT, PET), and the example code is for CT images (you probably know this anyway).
If you are not sure that all your images are of this kind, or if some CT images may lack the tags, you have to make sure that the tag is actually there, for example:

   # Convert to Hounsfield units (HU)
   intercept = scans[0].RescaleIntercept if 'RescaleIntercept' in scans[0] else -1024
   slope = scans[0].RescaleSlope if 'RescaleSlope' in scans[0] else 1

(note that these defaults are for CT images)

_Originally posted by @mrbean-bremen in https://github.com/pydicom/pydicom/issues/818#issuecomment-472939203_

pixel-data question

All 4 comments

As far as I'm aware, the IOD for MR doesn't include the Modality LUT module or any way to apply a similar rescale operation to CT. It does however include the VOI LUT module like CT, so if you wanted a unified visualisation pathway that applies to both you could do:

from pydicom import dcmread
from pydicom.pixel_data_handlers import util

ds = dcmread('path/to/dataset.dcm')
arr = ds.pixel_array
# Apply rescale operation (if required, otherwise returns arr unchanged)
rescaled_arr = util.apply_modality_lut(arr, ds)
# Apply windowing operation (if required, otherwise returns arr unchanged)
windowed_rescaled_arr = util.apply_voi_lut(rescaled_arr, ds, index=0)

API reference, note that there may be multiple views when windowing (hence the index keyword parameter).

Note that some MR scanners (at least Philips scanners, IIRC) may set modailty LUTs in the form of modality LUT sequences. So it makes always sense to use the mentioned utility functions.

@anxingle does that resolve your issue?

@anxingle does that resolve your issue?

Yeah. Thank you for the timely replay.

Was this page helpful?
0 / 5 - 0 ratings