如何使用pyDicom替换同一DICOM文件中的像素数据,以便再次使用任何DICOM查看器阅读?

8
我想读取一些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.html, https://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。您确定您不是在尝试查看文件的早期版本或类似的东西吗? - darcymason
谢谢你,darcymason。我对Dicompass工具的理解还不够。你有Dicompass的许可证吗?我的Dicompass工具不允许我连续显示两个患者信息相同的DICOM图像。 - user8697183
1个回答

5
代码看起来不错。但是你没有覆盖原始文件。
你使用以下命令加载文件:
filename = get_testdata_files('MR_small.dcm')[0]
ds = pydicom.dcmread(filename)

原始文件名为"MR_small.dcm"。

然后您可以使用以下命令保存文件:

ds.save_as("after.dcm")

目标文件名不同,这意味着原始文件仍然未更改。

您应该加载“after.dcm”以测试您的DICOM查看器

或者

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


虽然不是您问题的一部分,但如果您创建了具有像素数据更改的原始图像副本,则建议您还修改数据集中的实例特定信息,例如某些UIDInstanceNumber(0020,0013),SOPInstanceUID(0008,0018)等。


谢谢您的友善回答。您说得对,代码没有问题。问题出在我身上。我意识到我对DICOM工具的理解还不够。使用Dicompass显示DICOM文件时没有问题。但是在那之后,如果尝试显示其他患者信息与第一个相同的DICOM文件,则第二个命令将被忽略。当我尝试分别打开两个DICOM文件时,最终确认像素阵列已经改变了...我很尴尬,谢谢您的帮助。 - user8697183

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接