Как создать новый образ dicom с аннотациями от других? - PullRequest
0 голосов
/ 26 марта 2019

Я хотел бы создать два файла 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

1 Ответ

0 голосов
/ 27 марта 2019

Информация об этом может быть найдена в документации pydicom .
Замечание по коду "your";): data = ds.pixel_array[:, :half_x] присваивает представление numpy.ndarray, то есть ds.pixel_array, 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")   
...