pixel_array 在 python 3.x 上使用 pydicom 的问题

Issue with pixel_array using pydicom on python 3.x

我正在使用 pydicom(与 pip3 一起安装,在 python 3.7 上,使用 Idle)并且我需要访问 pixel_array 值。

我只是将提供的示例复制粘贴到 the documentation 中,这会导致两个错误:

我已经解决了这个问题,没有使用这个操作。

这是我的代码:

import matplotlib.pyplot as plt
import sys
import pydicom
import numpy
from pydicom.data import get_testdata_files

print(__doc__)
#filename = get_testdata_files("bmode.dcm")[0]
filename = "bmode.dcm"
dataset = pydicom.dcmread(filename)

# Normal mode:
print()
print("Filename.........:", filename)
print("Storage type.....:", dataset.SOPClassUID)
print()

pat_name = dataset.PatientName
display_name = pat_name.family_name + ", " + pat_name.given_name
print("Patient's name...:", display_name)
print("Patient id.......:", dataset.PatientID)
print("Modality.........:", dataset.Modality)
print("Study Date.......:", dataset.StudyDate)

if 'PixelData' in dataset:
    rows = int(dataset.Rows)
    cols = int(dataset.Columns)
    print("Image size.......: {rows:d} x {cols:d}, {size:d} bytes".format(
        rows=rows, cols=cols, size=len(dataset.PixelData)))
    if 'PixelSpacing' in dataset:
        print("Pixel spacing....:", dataset.PixelSpacing)


# use .get() if not sure the item exists, and want a default value if missing
print("Slice location...:", dataset.get('SliceLocation', "(missing)"))

# plot the image using matplotlib
plt.imshow(dataset.pixel_array, cmap=plt.cm.bone)
plt.show()

你能帮我解决这两个错误并获取 pixel_array 值吗? 不要犹豫,给我一些建议/备注/...

谢谢!

嗨,马克,欢迎来到 SO!

您的第一个错误意味着 get_testdata_files returns 是一个空列表,因此找不到您的文件。查看 the pydicom source,它显示在 [DATA_ROOT]/test_files 中执行了搜索。您的文件位于该路径中吗?

你的第二个错误与 PIL 相关,这可能很难调试和修复。首先尝试从 supplied test files. If that works, your problem is probably that PIL cannot handle the specific encoding of your image data. You want to install and use GDCM instead of PIL to see if that solves the problem. Another user has had a similar issue as you, GDCM solved the problem. It can be a bit of a headache to get working unfortunately. Or have a look at this page 之一创建的数据集中读取 pixel_array,它显示了查看图像数据的一些其他选择。