我指的不是帧,而是分段写入数据数组。 我通常以这种方式将 numpy 数组保存在文件中:
ds.PixelData = array.tobytes()
ds.save_as(dcmpath)
但我无法创建足够大的数组并遇到 numpy 限制。
好的,现在我对您的问题有了更好的理解,这是使用 SimpleITK 解决您的问题的方法。
import numpy as np
import SimpleITK as sitk
# Read a Dicom series from a directory
dicom_dir = './DCMData'
reader = sitk.ImageSeriesReader()
dicom_names = reader.GetGDCMSeriesFileNames(dicom_dir)
reader.SetFileNames(dicom_names)
image = reader.Execute()
print("Image size:", image.GetSize())
# Extract out a sub-block of the image
subblock = image[50:100, 50:100, 50:100]
# Add 300 to all the intesities of the subblock pixels,
# just for fun.
subblock = subblock + 300
# Convert SimpleITK Image to Numpy array
my_array = sitk.GetArrayFromImage(subblock)
# Insert your Numpy stuff here
# Convert Numpy array back into SimpleITK Image
subblock = sitk.GetImageFromArray(my_array)
# Paste the subblock back into the original image
image[50:100, 50:100, 50:100] = subblock
# Display the image using the default viewer (usually Fiji)
sitk.Show(image)
sick.WriteImage(image, "output_image.nii.gz")
第一部分将 DICOM 系列从目录读取到 SimpleITK 图像中。
之后,我提取出图像的一个子块(X、Y 和 Z 范围从 50 到 100,仅用于说明)。
然后我将子块中所有像素的强度添加到 300,这样我就可以看到它们。您可能想省略该行。
然后我将该子块图像转换为 Numpy 数组。之后,您可以插入您想要的任何 Numpy 像素修改。
之后,我将 Numpy 数组转换回 SimpleITK 图像,并将新的子块粘贴到我们的原始图像中。
最后我们显示图像并写出它的 Nifti 版本。
如果你想写出一个新的 DICOM 系列,那就有点复杂了,因为你写的是一系列图像,而不是单个 Nifti 文件。这里有示例代码:https://simpleitk.readthedocs.io/en/master/link_DicomSeriesReadModifyWrite_docs.html