Create image patches and reconstruct it back to the image

Hello,

I have 3D medical image of size [284,143,143] , 284 are the number of slices and I want to extract 2D image patches out of it and reconstruct the image back to patches. I want to have patches of shape 128*128 .Is there any way to do it using simple itk.

Thanks in advance

Hello @DL_Begin,

Yes, this is easily done, though not sure exactly about your goal, the exact code/approach depends on that. The most trivial solution below:

import SimpleITK as sitk
import numpy as np

# Create random image, note that numpy (z,y,x) and ITK/SimpleITK (x,y,z) index order are reveresed 
arr = np.random.random([284,143,143])
original_volume = sitk.GetImageFromArray(arr)

# Resample to the new desired size
new_size = [128, 128, original_volume.GetSize()[-1]]
resampled_volume = sitk.Resample(original_volume,new_size)

#Extract the slices into a list
slice_list = [resampled_volume[:,:,i] for i in range(resampled_volume.GetSize()[-1])]

If you want to perform some operation on a slice by slice fashion and then resample the volume to a new size I recommend using the Pythonic slicing_decorator solution provided by @blowekamp and then resampling as above.

I want to get non overlapping slices and then fed it to my cycle gans model and in the end reconstruct the image back from these patches

If I am not wrong this will give me axial slices instead of patch right?

What is the difference between your desired patch and an axial slice? Please define what you mean by patch (e.g. 2D image that is extracted from the 3D volume along the z axis).

To reconstruct the volume from the output of your GAN, use the JoinSeries function it is the inverse of the slice separation in the code above:

volume_from_slice_list = sitk.JoinSeries(slice_list)
#Copy the metadata information back (origin, spacing, direction cosine matrix)
volume_from_slice_list.CopyInformation(resampled_volume)

sorry for the confusion. I just re-read my question and framed it wrong.

So I am trying to implement this paper https://www.ncbi.nlm.nih.gov/pmc/articles/PMC6398343/pdf/nihms-1014364.pdf Medical Image Synthesis with Deep Convolutional Adversarial Networks

I have resampled my medical image dataset into desired size already. After that I get a volumetric image of size 284,143,143 . I want to extract 2D patches /small windows of my axial slices

Still not well defined. If you want a 2D axial patch from a 3D image:

x_size = 32
y_size = 16
z_index = 0
x_start_index=10
y_start_index = 20
try:
   patch = volume[x_start_index:x_start_index+x_size, y_start_index:y_start_index+y_size, z_index]
except:
  pass

The degrees of freedom that remain are the values for z_index, x_start_index, y_start_index, which is why I said that the task is still not well defined. Also inside try-except because we don’t check that the indexes are inside the original image.

1 Like