使用 python pydicom 显示来自 DCM 图像的多个帧

Display multiple frames from a DCM image using python pydicom

我正在尝试读取和显示 dicom 图像。该图像有 49 帧。 PixelData 在数组中有 24887296 个元素。由于pixel_array不能直接使用,请指导如何显示.dcm图像的多帧?输入图像为bscan图像。

代码如下


import matplotlib.pyplot as plt
import pydicom

dataset = pydicom.dcmread('bscan.dcm')
frame_generator = pydicom.encaps.generate_pixel_data_frame(dataset.PixelData)

if 'PixelData' in dataset:
    rows = int(dataset.Rows)
    cols = int(dataset.Columns)
    print("Image size.......: {rows:d} x {cols:d}, {size:d} bytes".format(
        rows=rows, cols=cols, size=len(dataset.PixelData)))
    if 'PixelSpacing' in dataset:
        print("Pixel spacing....:", dataset.PixelSpacing)

plt.imshow(dataset.pixel_array)
plt.show()

输出

Image size.......: 496 x 512, 24887296 bytes
Pixel spacing....: [0.011606, 0.003872]
Slice location...: (missing)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-6-bef78c9b38bf> in <module>()
     10 print("Slice location...:", dataset.get('SliceLocation', "(missing)"))
     11 
---> 12 plt.imshow(dataset.pixel_array)
     13 plt.show()

6 frames
/usr/local/lib/python3.6/dist-packages/pydicom/pixel_data_handlers/numpy_handler.py in get_pixeldata(ds, read_only)
    274         raise AttributeError(
    275             "Unable to convert the pixel data as the following required "
--> 276             "elements are missing from the dataset: " + ", ".join(missing)
    277         )
    278 

AttributeError: Unable to convert the pixel data as the following required elements are missing from the dataset: SamplesPerPixel

如异常消息所述,您的图像缺少强制性 DICOM 标签。 修补此具体图像的一种可能性是手动设置它:

dataset = pydicom.dcmread('bscan.dcm')
if 'SamplesPerPixel' not in dataset:
    dataset.SamplesPerPixel = 1  # or whatever the correct value should be, depending on the data

只要其他标签都设置正确,就可以计算出来:

dataset.SamplesPerPixel = len(dataset.PixelData) / (dataset.get('NumberOfFrames', 1) * dataset.Rows * dataset.Columns * dataset.BitsAllocated / 8)