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 中,这会导致两个错误:
首先是关于 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
我已经解决了这个问题,没有使用这个操作。
second 是关于 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 值吗?
不要犹豫,给我一些建议/备注/...
谢谢!
嗨,马克,欢迎来到 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
,它显示了查看图像数据的一些其他选择。
我正在使用 pydicom
(与 pip3
一起安装,在 python 3.7 上,使用 Idle
)并且我需要访问 pixel_array
值。
我只是将提供的示例复制粘贴到 the documentation 中,这会导致两个错误:
首先是关于
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
我已经解决了这个问题,没有使用这个操作。
second 是关于
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 值吗? 不要犹豫,给我一些建议/备注/...
谢谢!
嗨,马克,欢迎来到 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
,它显示了查看图像数据的一些其他选择。