pydicom读取头文件,如何使用pydicom替换同一dicom文件上的像素数据,以使用任何dicom查看器再次读取它?...

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?

# authors : Guillaume Lemaitre

# 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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值