【问题标题】:How to create a new dicom image with annotations from other?如何使用其他注释创建新的 dicom 图像?
【发布时间】:2019-03-25 23:38:59
【问题描述】:

我想从一个创建两个 pydicom 文件。但我无法使用带注释的 *.dcm 格式保存文件。

import pydicom
from pydicom.data import get_testdata_files
# read the dicom file
ds = pydicom.dcmread(test_image_fps[0])
# find the shape of your pixel data
shape = ds.pixel_array.shape
# get the half of the x dimension. For the y dimension use shape[0]
half_x = int(shape[1] / 2)
# slice the halves
# [first_axis, second_axis] so [:,:half_x] means slice all from first axis, slice 0 to half_x from second axis
data  = ds.pixel_array[:, :half_x]
print('The image has {} x {}'.format(data.shape[0],
                                        data.shape[1]))

# print the image information given in the dataset
print(data)
data.save_as("/my/path/after.dcm")
'numpy.ndarray' object has no attribute 'save_as

【问题讨论】:

    标签: python pydicom


    【解决方案1】:

    这方面的信息可以在pydicom documentation找到。
    备注“你的”;) 代码:data = ds.pixel_array[:, :half_x] 将 ds.pixel_array 的 numpy.ndarray 视图分配给data。调用data.save_as() 预计会失败,因为这是ds 的属性而不是data。根据文档,您需要像这样写入ds.PixelData 属性:

    ds.PixelData = data.tobytes() # where data is a numpy.ndarray or a view of an numpy.ndarray
    
    # if the shape of your pixel data changes ds.Rows and ds.Columns must be updated,
    # otherwise calls to ds.pixel_array.shape will fail
    ds.Rows = 512 # update with correct number of rows
    ds.Columns = 512 # update with the correct number of columns
    ds.save_as("/my/path/after.dcm")   
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多