# Create image patches and reconstruct it back to the image

**URL:** https://discourse.itk.org/t/create-image-patches-and-reconstruct-it-back-to-the-image/3274
**Category:** Beginner Questions
**Tags:** python, dicom, simpleitk
**Created:** [July 9, 2020, 12:03pm UTC](https://discourse.itk.org/t/create-image-patches-and-reconstruct-it-back-to-the-image/3274 "2020-07-09T12:03:21Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![DL\_Begin](https://discourse.itk.org/letter_avatar_proxy/v4/letter/d/b5a626/32.png) [@DL\_Begin](https://discourse.itk.org/u/DL_Begin)
#### Post date: [July 9, 2020, 12:03pm UTC](https://discourse.itk.org/t/create-image-patches-and-reconstruct-it-back-to-the-image/3274/1 "2020-07-09T12:03:22Z")

</div>

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

---

<div class="post-metadata">

### Author: ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)
#### Post date: [July 9, 2020, 1:23pm UTC](https://discourse.itk.org/t/create-image-patches-and-reconstruct-it-back-to-the-image/3274/2 "2020-07-09T13:23:11Z")

</div>

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:

```auto
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](https://discourse.itk.org/t/resampleimagefilter-4d-images/2172/4) solution provided by @blowekamp and then resampling as above.

---

<div class="post-metadata">

### Author: ![DL\_Begin](https://discourse.itk.org/letter_avatar_proxy/v4/letter/d/b5a626/32.png) [@DL\_Begin](https://discourse.itk.org/u/DL_Begin)
#### Post date: [July 9, 2020, 1:32pm UTC](https://discourse.itk.org/t/create-image-patches-and-reconstruct-it-back-to-the-image/3274/3 "2020-07-09T13:32:18Z")

</div>

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

---

<div class="post-metadata">

### Author: ![DL\_Begin](https://discourse.itk.org/letter_avatar_proxy/v4/letter/d/b5a626/32.png) [@DL\_Begin](https://discourse.itk.org/u/DL_Begin)
#### Post date: [July 9, 2020, 1:34pm UTC](https://discourse.itk.org/t/create-image-patches-and-reconstruct-it-back-to-the-image/3274/4 "2020-07-09T13:34:31Z")

</div>

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

---

<div class="post-metadata">

### Author: ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)
#### Post date: [July 9, 2020, 1:42pm UTC](https://discourse.itk.org/t/create-image-patches-and-reconstruct-it-back-to-the-image/3274/5 "2020-07-09T13:42:19Z")

</div>

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:

```auto
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)

```

---

<div class="post-metadata">

### Author: ![DL\_Begin](https://discourse.itk.org/letter_avatar_proxy/v4/letter/d/b5a626/32.png) [@DL\_Begin](https://discourse.itk.org/u/DL_Begin)
#### Post date: [July 10, 2020, 8:56am UTC](https://discourse.itk.org/t/create-image-patches-and-reconstruct-it-back-to-the-image/3274/6 "2020-07-10T08:56:27Z")

</div>

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](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

---

<div class="post-metadata">

### Author: ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)
#### Post date: [July 10, 2020, 12:54pm UTC](https://discourse.itk.org/t/create-image-patches-and-reconstruct-it-back-to-the-image/3274/7 "2020-07-10T12:54:28Z")

</div>

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

```auto
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.
