如何在使用 SimpleITK 保存 DICOM 文件时保留 UID?

How to preserve UID while saving DICOM file using SimpleITK?

我正在尝试使用 SimpleITK 读取一个 DICOM 文件并将其保存在其他地方。我注意到 Series Instance UID 会发生变化,而不管我是否明确设置了它。

如何保留原始 UID?

from pathlib import Path
import SimpleITK as sitk

dicom_path = '......'
p = Path(dicom_path)

reader = sitk.ImageFileReader()
reader.SetFileName(str(p))
reader.LoadPrivateTagsOn()

image = reader.Execute()
print('Series Instance UID', image.GetMetaData('0020|000e'))
print('SOP Instance UID', image.GetMetaData('0008|0018'))
image.SetMetaData('0020|000e', image.GetMetaData('0020|000e'))

writer = sitk.ImageFileWriter()
writer.SetFileName(out_folder+case+p.name)
writer.SetUseCompression(False)
writer.Execute(image)

reader = sitk.ImageFileReader()
reader.SetFileName(out_folder+case+p.name)
reader.LoadPrivateTagsOn()

image = reader.Execute()
print('Series Instance UID', image.GetMetaData('0020|000e'))
print('SOP Instance UID', image.GetMetaData('0008|0018'))

为系列 UID 提供两个不同的字符串。 SOP UID 保持不变:

Series Instance UID 1.3.12.2.1107.5.1.4.74141.30000017013107011409700014483
SOP Instance UID 1.3.12.2.1107.5.1.4.74141.30000017013107011409700014570

Series Instance UID 1.2.826.0.1.3680043.2.1125.1.65790274925978549485969544082687134
SOP Instance UID 1.3.12.2.1107.5.1.4.74141.30000017013107011409700014570

虽然我从未使用过工具包,但工具包的行为很奇怪。如果修改像素数据,则应更改某些属性;在这种情况下,SOP 实例 UID 最为重要。

但是,在您的情况下,像素数据没有被修改。同样,只修改了Series Instance UID; SOP 实例 UID 不变。

无论如何,工具包提供了一种在编写 DICOM 数据集时保留 UID 的方法。详情请参考KeepOriginalImageUIDOn会员

Self& itk::simple::ImageFileWriter::KeepOriginalImageUIDOn (void)
Use the original study/series/frame of reference.

These methods Set/Get/Toggle the KeepOriginalImageUID flag which get's passed to image file's itk::ImageIO object. This is relevant only for the DICOM file format, configuring the writer to use the information in the image's meta-data dictionary or to create new study/series/frame of reference values.

Definition at line 134 of file sitkImageFileWriter.h.

这将指示工具包在写入数据集时保留原始 UID。