如何以正确的顺序对 DICOM 切片进行排序?

How to sort DICOM Slices in correct order?

我正在尝试以正确的顺序对 DICOM 文件进行排序,但我无法使用图像位置患者描述符,因为它给我一个错误 "AttributeError: 'FileDataset' object has no attribute 'sort'"。

代码:

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)

除了排序属性之外,pydicom lib 工作正常。

此示例说明如何对切片进行排序。

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)