I am trying to read one single DICOM file using SimpleITK and save it somewhere else. I noticed that Series Instance UID
changes regardless of the fact that I am explicitly setting it.
How do I preserve the original 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'))
Gives two different strings for Series UIDs. SOP UIDs stay the same though:
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
Though I never used the toolkit, the behavior of toolkit is strange. Some of the attributes should be changed if pixel data is modified; SOP Instance UID is most important in that case.
But, in your case, pixel data is not modified. Also, only Series Instance UID is modified; SOP Instance UID is unchanged.
Anyway, toolkit provides a way to preserve the UIDs while writing DICOM dataset. Please refer to KeepOriginalImageUIDOn
member for more details.
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.
This will instruct toolkit to keep the original UIDs while writing dataset.