Writing dicom file through using ImageSeriesReader

Hi, please help.

I cannot write correctly dicom.
I did following step,

# load
L40t1 = "path/to/dcm image dir"
series_IDsT1 = sitk.ImageSeriesReader.GetGDCMSeriesIDs(L40t1)
if not series_IDsT1:
    print("ERROR: given directory does not contain a DICOM series.")
series_file_namesT1 = sitk.ImageSeriesReader.GetGDCMSeriesFileNames(L40t1, series_IDsT1[0])
series_readerT1 = sitk.ImageSeriesReader()
series_readerT1.SetFileNames(series_file_namesT1)
series_readerT1.MetaDataDictionaryArrayUpdateOn()
series_readerT1.LoadPrivateTagsOn()
t1volume = series_readerT1.Execute()

then save it,

writer = sitk.ImageFileWriter()
writer.KeepOriginalImageUIDOn()
print("save images...", t1volume.GetDepth())
for i in range(t1volume.GetDepth()):
    image_slice = t1volume[:,:,i]
    # Tags shared by the series.
    for j,key in enumerate(series_readerT1.GetMetaDataKeys(i)):
        image_slice.SetMetaData(key, series_readerT1.GetMetaData(i,key))
    # Write to the output directory and add the extension dcm, to force writing in DICOM format.
    writer.SetFileName(os.path.join(outdir,str(i)+'.dcm'))
    writer.Execute(image_slice)
print(("~" * 50) + "  Done.")

then, check pixel spacing of saved image

# check pixel spacing
print("original",t1volume.GetSpacing())

# data test using 0.dcm
image = sitk.ReadImage(os.path.join(outdir,str(0)+'.dcm')) 
print("saved image",image.GetSpacing())

I got result of pixel spacing are,
original (0.78125, 0.78125, 7.000051021575928)
saved image (0.78125, 0.78125, 1.0) # Odd…

Why I cannot save SpacingBetweenSlices correctly ??
Where is wrong/lack of my code ??

But, this behavior occurred by only using ImageSeriesReader. (in my case).
When I use ReadImage(path2dcm-instance), I can save SpacingBetweenSlices correctly.

tatsuaki

2 Likes

Hello @tatsuaki,

Please provide the following details so that we can recreate your issue:

  1. SimpleITK version.
  2. Python version.
  3. Operating system.
  4. SimpleITK, binary distribution (conda or pip installed).

Please be aware that the latest SimpleITK release is 1.2.4 . Often, just upgrading to the newest release solves issues as these are continually addressed.

Hi, @zivy

Thank you for your reply.

My environment are,

SimpleITK version = 1.2.4
libitk = 4.13.2
Python version = 3.6.7
Operating system = win10
SimpleITK, binary distribution (conda or pip installed) = conda, I am using AnacondaNavigator 1.9.7.

Hello @tatsuaki,

Generally speaking your code is fine (not sure why you have an enumerate whose value isn’t used when copying the tags).

Your issue is with the understanding of how a DICOM series is stored as a set of slices. Each slice is independently positioned in space (image position patient, 0020|0032). The spacing in the z axis is determined when reading all slices and is the difference between the z coordinates of consecutive slices. When you read a single slice there is no way to know what is the z-spacing, we only know that this slice is positioned in 3D so we arbitrarily set the spacing in Z to 1.

If you read the whole output series using a series reader you should see the correct spacing.

2 Likes

Hello, @zivy

If you read the whole output series using a series reader you should see the correct spacing.

Thank you inform nice trick. This way is seems nice to processing 3D spacing.

Finally, I decided to use the combination of the sitk(for resampling, filtering) and the pydicom(dcm in/out).
Path way is 1.read series-sitk, 2.resampling/filtering-sitk, 3.to numpy array-sitk, 4.read original dcm(to use as holder) and edit metadata, replace pixel_value, and then output -pydicom.

Thanks a lot.

tatsuaki

Hi @tatsuaki

I do the same two pieces of code as above, the problem is that when I open the output folder in a DICOM viewer, it shows as a number of separate images and not as a single multi-slice image.
Do you also have the same?