1

I'm trying get all images in a multi-frame DICOM file. Right now I was successfully able to see and save a single image in a single-frame DICOM file, by using the pydicom and matplotlib libraries, like so:

filename = pydicom.data.data_manager.get_files(*base folder path*,*dicom filename*)[0]
ds = pydicom.dcmread(filename)

plt.imshow(ds.pixel_array, cmap=plt.cm.bone)
plt.show()

Now, I wanted to be able to see and save all images in a multi-frame DICOM image, but by using this snippet of code, it returns the following error:

TypeError: Invalid shape (150, 768, 1024, 3) for image data

I've searched a bit on the web, but couldn't seem to find anything to enlighten me. I wanted to know if someone has passed through this, and what's the best way to overcome it, and be able to get all images in a multi-frame DICOM file.

Note: The similar questions found on Stack Overflow are either outdated or do not comply with what I want.

1 Answer 1

2

DICOM is essentially a 'three-dimensional' image format: it can store lots of images or 'slices'. However, since colour images are already 3-dimensional (height, width, RGB channels), DICOM datasets can actually be 4-dimensional (apparently... I didn't know this). At least, I think that's why there are four elements in the shape of your dataset.

My guess is that the dataset contains 150 images, each of which having 768 by 1024 pixels and 3 channels.

So assuming ds.pixel_array is array-like, you should be able to see the middle slice like so:

plt.imshow(ds.pixel_array[75]) 

To save them all, you'd do something like this pseudocode:

for i, slice in enumerate(ds.pixel_array):
    plt.imshow(slice)
    plt.savefig(f'slice_{i:03n}.png')

Obviously, that will just re-create what you essentially already have but in a less convenient format, but you get the idea.

5
  • Thank you! Now I can see a single frame! You're correct, there are 150 frames in this file, however, when I open this file with radiant viewer, I can see that there are 91 images inside this file. What's the correlation between images and frames? Do you, or anyone, know any formula or algorithm to get the exact image? Commented Apr 11, 2022 at 16:41
  • I've checked with my other multi-frame dicom files, and the number of images does coincide with the number of frames, except for that case. I wonder why... Thank you for the help, I'm going to accept your answer. Commented Apr 11, 2022 at 16:47
  • @pkingsjunior Just reading around, I was also coming to the conclusion that it seems wrong... maybe something weird in the file header? And BTW, you were trying to use the bone colourmap before; you can only use a colourmap on a greyscale (1-channel) image. You'd have to combine the channels somehow (or drop 2) to do that. Anyway, good luck!
    – Matt Hall
    Commented Apr 11, 2022 at 16:49
  • Thanks for the comment! Will make that correction. Commented Apr 11, 2022 at 17:01
  • One image should be one frame. Whatever the value of ds.NumberOfFrames is the correct number, and if Number of Frames is absent then there's only 1 frame. Commented Apr 12, 2022 at 1:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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