2

I am trying to read and display DICOM(.dcm) images using below code:-

import pydicom as dicom
import numpy as np
from PIL import Image, ImageEnhance, ImageOps
from PIL.ImageQt import ImageQt

def display_dicom_images(self, folder_Path):
    try:
        # Image parameters
        image_width = 382
        image_height = 382

        image_depth = 3
        self.total_images_in_folder = len(glob.glob1(folder_Path,"*"))

        # Select the center image for display
        self.current_image_number = round(self.total_images_in_folder / 2)
        self.display_number = self.current_image_number

        image_dtype = np.uint8
        pixel_array = np.ndarray([self.total_images_in_folder, image_height, image_width, image_depth]).astype(image_dtype)
        # load images here, once better MR images are acquired
        for image_index in range(0, self.total_images_in_folder):
            # for DICOM
            image_path = folder_Path + "/" + str(image_index) + ".dcm"

            scan_image = dicom.dcmread(image_path)
            scan_image = scan_image.pixel_array.astype(image_dtype)

            pixel_array[image_index, :scan_image.shape[0], :scan_image.shape[1], :scan_image.shape[2]] = scan_image

        return pixel_array

But getting error:-

IndexError('tuple index out of range',)

i am using pillow python library for image.

Sanjiv
  • 980
  • 2
  • 11
  • 29
  • Can you post the full traceback? It'll show what line is causing the issue. Also you have a `try` block with no `except`... – scaramallion Feb 10 '20 at 11:19
  • @scaramallion this line throughing error:- `pixel_array[image_index, :scan_image.shape[0], :scan_image.shape[1], :scan_image.shape[2]] = scan_image` – Sanjiv Feb 10 '20 at 11:50
  • @scaramallion:- Second Last line throughting error. – Sanjiv Feb 10 '20 at 11:50

1 Answers1

0

How do you know scan_image.shape is of length 3? MR images should only be monochrome, which would make image_depth = 1 and the length of scan_image.shape equal to 2.

C.8.3.1.1.3 Photometric Interpretation

Enumerated Values:

  • MONOCHROME1
  • MONOCHROME2
Amit Joshi
  • 15,448
  • 21
  • 77
  • 141
scaramallion
  • 1,251
  • 1
  • 6
  • 14
  • Can you please tell me what will be mode for dicom image. e.g- L, I. RGB, 1 or etc..? – Sanjiv Feb 10 '20 at 11:52
  • 2
    It depends on the value of `dataset.BitsAllocated`, if its 8 then you can use `L`, if its 16 you'll have to use `I;16` – scaramallion Feb 10 '20 at 11:58
  • @scarmallion:- what about `mode = 'RGB'` – Sanjiv Feb 10 '20 at 12:02
  • 2
    That would be for *Samples Per Pixel* = 3, which it isn't for MR images. This is also getting off track, is my answer correct in diagnosing your original question or not? – scaramallion Feb 10 '20 at 12:04
  • So you are saying i have to use `pixel_array[image_index, :scan_image.shape[0], :scan_image.shape[1]] = scan_image` instead of `pixel_array[image_index, :scan_image.shape[0], :scan_image.shape[1], :scan_image.shape[2]] = scan_image` and image_depth not required.? – Sanjiv Feb 10 '20 at 12:08
  • 1
    It depends, if you're just processing *MR Image* datasets then yes, that should be fine. But in general its better to minimise your assumptions when it comes to working with DICOM data (because eventually there'll be something you don't expect) – scaramallion Feb 11 '20 at 23:43