1

我正在使用pydicompip3在python 3.7上安装,使用Idle),我需要访问pixel_array值。

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

  • 首先是关于get_testdata_files操作,它不起作用,因为

    Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license()" for more information.
    >>> 
    ==================== RESTART: D:\OneDrive\Desktop\test.py ====================
    None
    Traceback (most recent call last):
      File "D:\OneDrive\Desktop\test.py", line 8, in <module>
        filename = get_testdata_files("bmode.dcm")[0]
    IndexError: list index out of range
    

我已经解决了这个问题,而不是使用这个操作。

  • 第二个是关于的pixel_array,我无法解码出了什么问题,但似乎pixel_array没有填充。但是,我可以访问数据集中的其他字段,并且可以显示文件(ImageJ例如使用)。

    ==================== RESTART: D:\OneDrive\Desktop\test.py ====================
    None
    
    Filename.........: bmode.dcm
    Storage type.....: 1.2.840.10008.5.1.4.1.1.3.1
    
    Patient's name...: Femoral trombenarterectomy, Case Report:
    Patient id.......: Case Report 1
    Modality.........: US
    Study Date.......: 20110824
    Image size.......: 768 x 1024, 27472108 bytes
    Slice location...: (missing)
    Traceback (most recent call last):
    File "D:\OneDrive\Desktop\test.py", line 38, in <module>
         plt.imshow(dataset.pixel_array, cmap=plt.cm.bone)
    File "C:\Users\marcl\AppData\Local\Programs\Python\Python37\lib\site-packages\pydicom\dataset.py", line 949, in pixel_array
         self.convert_pixel_data()
    File "C:\Users\marcl\AppData\Local\Programs\Python\Python37\lib\site-packages\pydicom\dataset.py", line 895, in convert_pixel_data
         raise last_exception
    File "C:\Users\marcl\AppData\Local\Programs\Python\Python37\lib\site-packages\pydicom\dataset.py", line 863, in convert_pixel_data
    arr = handler.get_pixeldata(self)
     File "C:\Users\marcl\AppData\Local\Programs\Python\Python37\lib\site-packages\pydicom\pixel_data_handlers\pillow_handler.py", line 188, in get_pixeldata
    UncompressedPixelData.extend(decompressed_image.tobytes())
    File "C:\Users\marcl\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\Image.py", line 746, in tobytes
    self.load()
    File "C:\Users\marcl\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\ImageFile.py", line 261, in load
    raise_ioerror(err_code)
    File "C:\Users\marcl\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\ImageFile.py", line 58, in raise_ioerror
    raise IOError(message + " when reading image file")
    

    OSError:读取图像文件时数据流损坏

这是我的代码:

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 值吗?不要犹豫,给我一些建议/备注/...

谢谢!

4

1 回答 1

1

嗨,马克欢迎来到 SO!

您的第一个错误意味着get_testdata_files返回一个空列表,因此找不到您的文件。查看pydicom 源代码,它显示在[DATA_ROOT]/test_files. 您的文件是否位于该路径中?

您的第二个错误与 PIL 有关,并且很难调试和修复。首先尝试从提供的测试文件pixel_array之一创建的数据集中读取。如果可行,您的问题可能是 PIL 无法处理图像数据的特定编码。您想安装和使用 GDCM而不是 PIL 以查看是否可以解决问题。另一个用户遇到了和你类似的问题,GDCM 解决了这个问题。不幸的是,开始工作可能有点让人头疼。或者看看这个页面,它显示了查看图像数据的其他一些选择。

于 2019-04-03T09:46:21.370 回答