0

I am trying to sort DICOM files in correct order, and I not able to use Image Position Patient descriptor as it's giving me an error "AttributeError: 'FileDataset' object has no attribute 'sort'".

code:

for paths in a:
    for root, dirs, files in os.walk(paths):
        for file in files:
            if file.endswith('.dcm'):
                k = dcm.read_file(os.path.join(root, file))
                k.sort
                k = dcm.read_file(os.path.join(root, file)).pixel_array
                PixelArrays.append(k)

Other than sort attribute pydicom lib works fine.

Mike
  • 51
  • 1
  • 6

1 Answers1

1

This example shows how to sort slices.

https://github.com/pydicom/pydicom/blob/master/examples/image_processing/reslice.py

import pydicom
import numpy as np
import matplotlib.pyplot as plt
import sys
import glob

# load the DICOM files
files = []
print('glob: {}'.format(sys.argv[1]))
for fname in glob.glob(sys.argv[1], recursive=False):
    print("loading: {}".format(fname))
    files.append(pydicom.dcmread(fname))

print("file count: {}".format(len(files)))

# skip files with no SliceLocation (eg scout views)
slices = []
skipcount = 0
for f in files:
    if hasattr(f, 'SliceLocation'):
        slices.append(f)
    else:
        skipcount = skipcount + 1

print("skipped, no SliceLocation: {}".format(skipcount))

# ensure they are in the correct order
slices = sorted(slices, key=lambda s: s.SliceLocation)