如何使用pydicom替换同一dicom文件上的像素数据,以使用任何dicom查看器再次读取它? [英] how to replace pixel data on same dicom file using pydicom to read it again with any dicom viewer?

查看:800
本文介绍了如何使用pydicom替换同一dicom文件上的像素数据,以使用任何dicom查看器再次读取它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想处理一些DICOM文件,所以我正在为自己的工作测试pydicom,我认为这很有用.

现在我要加载现有的DICOM文件,用另一个像素阵列(例如预处理或字面意义上的另一个DICOM像素阵列)替换像素数据阵列,最重要的是,我想使用任何DICOM查看器应用程序再次对其进行处理./p>

对于此测试,我使用了下面的教程代码.此代码加载测试数据文件.图片尺寸为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")

解决方案

代码看起来不错.但是,您不会覆盖原始文件.

您通过以下方式加载文件:

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

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

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

ds.save_as("after.dcm")

目标文件名不同.也就是说,原始文件仍保持不变.

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

OR

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


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

I want to treat some DICOM files, so I'm testing pydicom for my work, which I think is considerably useful.

And now I want to load existing DICOM files, replace the pixel data array with another pixel array (e.g. preprocessing or literally another DICOM pixel array) and most of all, I want to treat it again with any DICOM viewer application.

For this test, I used the tutorial code below. This code loads a test data file. The size of image is 64*64. The code below does subsampling from the original data. After that, the size of image is 8*8, and the result is saved to "after.dcm".

But when I read the file using a DICOM viewer app (I used 'Dicompass'), the size of dicom image is still 64*64. What is it that i'm missing?

I referred to the pydicom documentation (http://pydicom.readthedocs.io/en/stable/getting_started.html, https://pydicom.github.io/pydicom/stable/index.html) to solve my problem.

# 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")

解决方案

The code looks OK. But, you are not overwriting original file.

You load the file with:

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

where original file name is "MR_small.dcm".

Then you save the file with:

ds.save_as("after.dcm")

where destination file name is different. That means, original file is still unchanged.

You should either load "after.dcm" in your DICOM viewer to test

OR

You should overwrite the file (pydicom.filewriter.dcmwrite) while saving it.


Not a part of your problem, but if you are creating copy of original image with change in pixel data, it is recommended that you also modify instance specific information in dataset like InstanceNumber (0020,0013), SOPInstanceUID (0008,0018) etc.

这篇关于如何使用pydicom替换同一dicom文件上的像素数据,以使用任何dicom查看器再次读取它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆