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

时间:2019-12-23 16:53:50

标签: dicom pydicom

我试图按正确的顺序对DICOM文件进行排序,但由于无法使用“图像位置病人”描述符,因此出现错误“ AttributeError:'FileDataset'对象没有属性'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)

除sort属性之外,pydicom lib可以正常工作。

1 个答案:

答案 0 :(得分:2)

此示例显示了如何对切片进行排序。

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)