reading stack of images and writing as single image

Hello,

I am trying to perform a simple task where I read in images with for loop, join them together and write it as a single image. But somehow sitk.JoinSeriesImageFilter does not join the images into a single stack, instead images are processed individually according to for loop.

I tried to make a list of images and then pass it to sitk.JoinSeriesImageFilter but it would not work.
However if I don’t use the for loop and read in individual image manually, it gives the desired outcome.
Can someone tell me what I am missing out?
Here is my code:

for file in sorted(glob.glob(filepath)):
   #read
   image_all = sitk.ReadImage(file, imageIO="TIFFImageIO")
   join = sitk.JoinSeriesImageFilter()
   joined_image = join.Execute(image_all)

   #write
   writer = sitk.ImageFileWriter()
   writer.SetFileName("test_write.dcm")
   writer.Execute(joined_image)

thank you

Hello @Won_Chul_Chung,

In the for loop you are reading each image on its own and joining it with no other image. Code for reading and creating the volume:

sitk.JoinSeries([sitk.ReadImage(file, imageIO="TIFFImageIO") for file in sorted(glob.glob(filepath))])

Code for writing a DICOM series as a set of files is more complicated, please see this example for full details.

2 Likes

Hello @zivy

thank you,
I was able to get the desired output.
perhaps sitk.JoinSeriesImageFilter() and sitk.JoinSeries() are two different functions.

by the way, would it be easier to apply any image transformation on the single volume I just created or to write a DICOM series and then apply image transformation to individual instance (“slice”)?

I only have experience with reading individual images as arrays (using openCV, pydicom, etc) and apply transformation to the arrays. Of course, this would not work since individual images lack any metadata to qualify as DICOM series.

Hello @Won_Chul_Chung,

The JoinSeries function is the procedural interface to the object oriented JoinSeriesImageFilter . In SimpleITK the object oriented interface has a corresponding procedural counterpart which under the hood creates the objects and calls the relevant functions.

Not sure I clearly understand the question with respect to applying transformations.

In SimpleITK you can do 2D transformations with 2D images and 3D transformations with 3D images. If the question is in the context of data augmentation, then this jupyter notebook is likely relevant. It shows how to perform transformations in 2D or 3D.

For detailed examples of SimpleITK transformations and resampling, please see the relevant notebooks found in the toolkit’s notebook repository.

1 Like