Search code examples
pythonimage-processingdicompydicom

Processing Xray Images (.dcm files) using pydicom (could not find SliceThickness Attribute)


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'

Solution

  • 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