1

I am trying to do a project. Unfortuantely I can read the dicom files, but when I try to access pixel_array attribute and plot it, it is throwing error.

def readDcm(self):
        #path of the decomFolder
        print("Path to the DICOM directory is: ",self.dcmPath)
        PathDicom = self.dcmPath
        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))


        print(len(lstFilesDCM))

        for filename in lstFilesDCM:
            currentDcm = pydicom.read_file(lstFilesDCM[0])
            dcm_data = currentDcm.PixelData
            pixeldata= currentDcm.pixel_array

the error is:

File "C:\Anaconda3\lib\site-packages\pydicom\pixel_data_handlers\pillow_handler.py", line 199, in get_pixeldata
    raise NotImplementedError(e.strerror)

NotImplementedError: None

Any suggestion would be nice. Thank you in advance.

Solved Solution:

def load_scan(path):
    slices = [dicom.read_file(path + '/' + s) for s in os.listdir(path)]
    slices.sort(key = lambda x: float(x.ImagePositionPatient[2]))
    pos1 = slices[int(len(slices)/2)].ImagePositionPatient[2]
    pos2 = slices[(int(len(slices)/2)) + 1].ImagePositionPatient[2]
    diff = pos2 - pos1
    if diff > 0:
        slices = np.flipud(slices)
    try:
        slice_thickness = np.abs(slices[0].ImagePositionPatient[2] - slices[1].ImagePositionPatient[2])
    except:
        slice_thickness = np.abs(slices[0].SliceLocation - slices[1].SliceLocation)

    for s in slices:
        s.SliceThickness = slice_thickness
    return slices 
Makau
  • 71
  • 2
  • 10

2 Answers2

1

I sorted the dicom files according to the axis which was in increasing order by using ImagePositionPatient attribute. Which solved my problem. I was able to access pixel_array attribute and plot. The code is added to original question. I add it below too.

def load_scan(path):
    slices = [dicom.read_file(path + '/' + s) for s in os.listdir(path)]
    slices.sort(key = lambda x: float(x.ImagePositionPatient[2]))
    pos1 = slices[int(len(slices)/2)].ImagePositionPatient[2]
    pos2 = slices[(int(len(slices)/2)) + 1].ImagePositionPatient[2]
    diff = pos2 - pos1
    if diff > 0:
        slices = np.flipud(slices)
    try:
        slice_thickness = np.abs(slices[0].ImagePositionPatient[2] - slices[1].ImagePositionPatient[2])
    except:
        slice_thickness = np.abs(slices[0].SliceLocation - slices[1].SliceLocation)

    for s in slices:
        s.SliceThickness = slice_thickness
    return slices 

code source:https://www.programcreek.com/python/example/97517/dicom.read_file

Makau
  • 71
  • 2
  • 10
0

Without any further context, it's difficult to assert anything. However, when I look at the way you collect the potential DICOM files, you are probably constructing not existing path (it looks like you skip one hierarchy level when joining path parts back; you may want to check that the constructed path actually points to an existing files using os.path.exists()).

You may want to change the iterating part:

    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)) # <-you cut one potential hierarchy level

with something like:

dicom_files = []
for d, dirs, files in os.walk('./data/'):
    for x in files:
        if x.endswith(".png"):
            dicom_path = os.path.join(d, x)
            if os.path.exists(dicom_path):
                dicom_files.append(dicom_path)
leoburgy
  • 356
  • 3
  • 9
  • Just to let you know, I can access other attributes but not pixel_array – Makau Nov 20 '18 at 09:38
  • Can you reproduce the error with the Matplotlib way of displaying the array? https://pydicom.github.io/pydicom/stable/viewing_images.html – leoburgy Nov 20 '18 at 09:42
  • Yes, I am using exactly the same syntex. Using the test data files, it is ok. But with my data. I get the error. – Makau Nov 20 '18 at 09:47
  • Looking at the source code of pillow_handler.py, it looks like your error is piping when checking the jpeg plugin. Did you check that the jpeg plugin is installed in your environment? Maybe useful: https://stackoverflow.com/a/10109941/5202571 – leoburgy Nov 20 '18 at 11:42
  • Thank you for your help. The jepg plugin is already installed. I can see the dcm files in Radiant or 3D slicer software. So why pydicom is not finding pixel-array is strange.. I even uninstalled and reinstalled pil. – Makau Nov 20 '18 at 12:29