0

I am new to Image Processing and want to know how can I pre-process dicom images using python. I am following the below tutorial:dicom in python I do not have the SliceThickness attribute in my data. How can I calculate it?

This is a sample dataset which I have:

Dataset

Here is my code:

import pydicom
import os
import numpy
from matplotlib import pyplot, cm

PathDicom = "xyz\images"
lstFilesDCM = []  # creating an empty list
for dirName, subdirList, fileList in os.walk(PathDicom):
    for filename in fileList:
        if ".dcm" in filename.lower():  # checking whether the file's DICOM
            lstFilesDCM.append(os.path.join(dirName,filename))

RefDs = pydicom.read_file(lstFilesDCM[0])
ConstPixelDims = (int(RefDs.Rows), int(RefDs.Columns), len(lstFilesDCM)) #Load dimensions based on the number of rows, columns, and slices (along the Z axis)
ConstPixelSpacing = (float(RefDs.PixelSpacing[0]), float(RefDs.PixelSpacing[1]), float(RefDs.SliceThickness)) # Load spacing values (in mm)

This is the error I got:

'FileDataset' object has no attribute 'SliceThickness'
E_net4
  • 27,810
  • 13
  • 101
  • 139
Trisa Biswas
  • 555
  • 1
  • 3
  • 17
  • `pydicom` looks to be able to convert between dicom format and numpy arrays, and my answer here https://stackoverflow.com/a/52113846/2836621 shows how to convery back and forth between PIL Image and numpy arrays so you should be able to use dicom and numpy and PIL interchangeably. I guess you'll want scikit-image for any serious processing. – Mark Setchell Sep 03 '18 at 07:39
  • I have modified my question. Actually I followed some tutorials and blogs where the image has been converted to a numpy array. For that, slice thickness attribute is required which I do not have in my data. Is there any other way to do it? – Trisa Biswas Sep 04 '18 at 06:05
  • 3
    Slice thickness only applies to image data where there actually are slices, that is computed tomography (CT), magnetic resonance imaging (MR) etc. If you are looking at plain single x-ray images (CR or DX modality) , then slice thickness is not relevant to this type of data anyway. You have not pasted your entire dataset, have you? There is no information about the type of image we are dealing with. Or if this is all, then it's not legal DICOM, because it's missing quite a bit of mandatory fields. – Tarmo R Sep 04 '18 at 08:18
  • I have edited the question & added an image of the entire dataset I have. How can I convert this image to an array? Which data points are relevant in this case? – Trisa Biswas Sep 04 '18 at 09:00
  • The question is still unclear as it stands, because there is still no clear explanation of what exactly you are trying to do with the object (_pre-processing_ can mean many things), nor is there a [MCVE] that shows what you have attempted and why it failed. – E_net4 Sep 04 '18 at 13:05
  • I have added my code with the error where I need help. – Trisa Biswas Sep 05 '18 at 05:01

2 Answers2

3

Tarmo R's comment is right. Looking at your data it has CR (Computed Radiography) as modality, which is a single picture because it basically is a digital x-ray. Therefore it will never have a SliceThickness attribute.

In this case you can immediately get a numpy array containing the pixel data:

image_pixel_data = RefDs.pixel_array
g_uint
  • 1,903
  • 3
  • 17
  • 30
0

This worked for me:

RefDs.pixel_array

It converted the image directly to a numpy array.

Trisa Biswas
  • 555
  • 1
  • 3
  • 17