我正在使用pydicom
(pip3
在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 值吗?不要犹豫,给我一些建议/备注/...
谢谢!