【问题标题】:How to replace pixel data in same DICOM file using pyDicom to read it again with any DICOM viewer?如何使用 pyDicom 替换同一 DICOM 文件中的像素数据以使用任何 DICOM 查看器再次读取它?
【发布时间】:2018-02-19 06:51:55
【问题描述】:

我想阅读一些 DICOM 文件,所以我正在为我的工作测试 pydicom,我认为这非常有用。

现在我想加载现有的 DICOM 文件,用另一个像素数组(例如预处理或另一个 DICOM 像素数组)替换像素数据数组,最重要的是,我想用任何 DICOM 查看器应用程序再次读取它。

对于这个测试,我使用了下面的教程代码。此代码加载一个测试数据文件。图片大小为64*64。下面的代码对原始数据进行子采样。之后图片大小为8*8,结果保存到after.dcm

但是当我使用 DICOM 查看器应用程序(我使用“Dicompass”)读取文件时,DICOM 图像的大小仍然是64*64。我错过了什么?

我参考了pydicom 文档(http://pydicom.readthedocs.io/en/stable/getting_started.htmlhttps://pydicom.github.io/pydicom/stable/index.html)来解决我的问题。

# authors : Guillaume Lemaitre <g.lemaitre58@gmail.com>
# license : MIT

import pydicom
from pydicom.data import get_testdata_files

print(__doc__)

# FIXME: add a full-sized MR image in the testing data
filename = get_testdata_files('MR_small.dcm')[0]
ds = pydicom.dcmread(filename)

# get the pixel information into a numpy array
data = ds.pixel_array
print(data)

print('The image has {} x {} voxels'.format(data.shape[0],
                                        data.shape[1]))
data_downsampling = data[::8, ::8]
print('The downsampled image has {} x {} voxels'.format(
    data_downsampling.shape[0], data_downsampling.shape[1]))

# copy the data back to the original data set
ds.PixelData = data_downsampling.tostring()
# update the information regarding the shape of the data array
ds.Rows, ds.Columns = data_downsampling.shape

# print the image information given in the dataset
print('The information of the data set after downsampling: \n')
print(ds)
print(ds.pixel_array)
print(len(ds.PixelData))
ds.save_as("after.dcm")

【问题讨论】:

  • 适用于我的 python 3.6 和 pydicom v1.0.1rc1。我可以在 Dicompass 中查看它。它显示了 8x8 像素,并且 dicom 标签查看器将行和列显示为 8。您确定您没有尝试查看文件的以前版本或类似的东西吗?
  • 谢谢你,达西马森。我不太了解Dicompass 工具。你有 Dicompass 的许可证吗?我的 Dicompass 工具不允许我连续显示两个患者信息相同的 DICOM 图像。

标签: python dicom pydicom


【解决方案1】:

代码看起来没问题。但是,您并没有覆盖原始文件。

您使用以下方式加载文件:

filename = get_testdata_files('MR_small.dcm')[0]
ds = pydicom.dcmread(filename)

原始文件名为“MR_small.dcm”。

然后你保存文件:

ds.save_as("after.dcm")

目标文件名不同的地方。这意味着,原始文件仍然没有改变。

您应该在 DICOM 查看器中加载“after.dcm”进行测试

您应该在保存文件时覆盖文件 (pydicom.filewriter.dcmwrite)。


不是您的问题的一部分,但如果您要创建像素数据更改的原始图像副本,建议您还修改数据集中的实例特定信息,例如 InstanceNumber (0020,0013)、SOPInstanceUID (0008,0018 ) 等。

【讨论】:

  • 感谢您的友好回答。你是对的,代码没有你说的问题。问题出在我身上。我知道我对 DICOM 工具的了解不够。使用 Dicompass 显示 dicom 文件时,没有问题。但之后,如果尝试显示患者信息与第一个相同的其他 DICOM 文件,则忽略第二个命令。当我尝试分别打开两个DICOM文件时,终于确认像素阵列已经更改好了..不好意思,谢谢..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多