【发布时间】:2021-09-16 16:35:24
【问题描述】:
我正在尝试替换给定示例图像中的像素数据。我的代码:
import matplotlib.pyplot as plt
from pydicom import dcmread
from pydicom.data import get_testdata_file
fpath = get_testdata_file('CT_small.dcm')
ds = dcmread(fpath)
# replacing rows and columens with new values
ds.Rows = 200
ds.Columns = 200
# replacing pixel data with new image (type: numpy.ndarray)
ds.pixel_array = data[layer].tobytes()
...
我收到此错误:
AttributeError: can't set attribute
更新
好的,如果我使用
ds.PixelData = data[layer].tobytes()
而不是
ds.pixel_array = data[layer].tobytes()
我得到一张图像,但它看起来与原始图像(数据[层])完全不同。我错过了什么?
【问题讨论】:
-
错误在哪一行?
-
对不起。第 10 行:ds.pixel_array = data[layer].tobytes()
-
数据很可能不是预期的格式。如果使用 CT 数据,则数据应为单色,每个像素 2 个字节。您可能必须相应地转换或重塑您的 numpy 数组。由于我们没有关于您尝试设置的数据的信息,这只是一个猜测。
-
是的,我想你可能就在那儿。新数据是 200x200 的 float32。我确实调整了行和列。添加 .astype(np.float16) 完成了这项工作。非常感谢!