首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用pyDicom替换同一DICOM文件中的像素数据,并使用任何DICOM查看器重新读取?

如何使用pyDicom替换同一DICOM文件中的像素数据,并使用任何DICOM查看器重新读取?
EN

Stack Overflow用户
提问于 2018-02-19 06:51:55
回答 1查看 11.3K关注 0票数 6

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

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

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

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

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

代码语言:javascript
复制
# 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")
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-02-20 06:11:13

密码看上去没问题。但是,您没有覆盖原始文件。

加载该文件时:

代码语言:javascript
复制
filename = get_testdata_files('MR_small.dcm')[0]
ds = pydicom.dcmread(filename)

其中原始文件名为"MR_small.dcm“。

然后用以下方式保存该文件:

代码语言:javascript
复制
ds.save_as("after.dcm")

目标文件名不同的地方。这意味着原始文件仍然保持不变。

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

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

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

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48860881

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档