简体   繁体   中英

Display multiple frames from a DCM image using python pydicom

I am trying to read and display a dicom image. The image has 49 frames. The PixelData has 24887296 elements in the array. Since the pixel_array cannot be used directly, please guide me on how to display the multiple frames from .dcm image? The input image is bscan image.

Below is Code


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

Output

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

As the exception message says, your image is missing a mandatory DICOM tag. A possibility to patch this concrete image would be to set it manually:

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

Provided that the other tags are correctly set, you could calculate it:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM