# Extract Overlapping Patches from a 3D Medical image and reconstruct the patches back to an Image

**URL:** https://discourse.itk.org/t/extract-overlapping-patches-from-a-3d-medical-image-and-reconstruct-the-patches-back-to-an-image/3368
**Category:** Beginner Questions
**Tags:** dicom, ct, simpleitk, python
**Created:** [August 7, 2020, 8:42am UTC](https://discourse.itk.org/t/extract-overlapping-patches-from-a-3d-medical-image-and-reconstruct-the-patches-back-to-an-image/3368 "2020-08-07T08:42:54Z")
**Posts on this page:** 4
**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: [August 7, 2020, 8:42am UTC](https://discourse.itk.org/t/extract-overlapping-patches-from-a-3d-medical-image-and-reconstruct-the-patches-back-to-an-image/3368/1 "2020-08-07T08:42:54Z")

</div>

Hello,  
I have a 3D CT image size of shape 284,143,143 and I want to extract 3D patches of size 64x64x64.  
Is there a way I can extract the patches from this volumetric image and reconstruct patches back. I have been using unfold function from pytorch so far but havent acheived the desired goal . But is there any way to do it using ITK.  
The aim is to send these image patches as input to my 3D GANs model. And reconstruct the generated image patches back to an image

Thank you

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [August 7, 2020, 1:02pm UTC](https://discourse.itk.org/t/extract-overlapping-patches-from-a-3d-medical-image-and-reconstruct-the-patches-back-to-an-image/3368/2 "2020-08-07T13:02:20Z")

</div>

You can use [region of interest](https://itk.org/Doxygen/html/classitk_1_1RegionOfInterestImageFilter.html) filter ([example](https://itk.org/ITKExamples/src/Filtering/ImageGrid/ExtractRegionOfInterestInOneImage/Documentation.html)) to extract your patches. You will need to be careful in choosing positions (origins) of the patches to have uniform overlap. You will probably want to record region indices (patch origins) so you can reconstruct your generated image later. Maybe use [paste image filter](https://itk.org/ITKExamples/src/Filtering/ImageGrid/PasteImageIntoAnotherOne/Documentation.html), maybe [iterators](https://itk.org/ITKExamples/src/Core/Common/IterateRegionWithAccessToIndexWithWriteAccess/Documentation.html).

---

<div class="post-metadata">

### Author: ![ibro45](https://discourse.itk.org/user_avatar/discourse.itk.org/ibro45/32/1639_2.png) [@ibro45](https://discourse.itk.org/u/ibro45)
#### Post date: [August 7, 2020, 6:28pm UTC](https://discourse.itk.org/t/extract-overlapping-patches-from-a-3d-medical-image-and-reconstruct-the-patches-back-to-an-image/3368/3 "2020-08-07T18:28:33Z")

</div>

I’ve used @fepegar’s [TorchIO](https://github.com/fepegar/torchio) package for exactly the same purpose. Check its `GridSampler` and `GridAggregator`, [here’s](https://torchio.readthedocs.io/data/patch_inference.html#grid-sampler) the documentation for them.

---

<div class="post-metadata">

### Author: ![fepegar](https://discourse.itk.org/user_avatar/discourse.itk.org/fepegar/32/1664_2.png) [@fepegar](https://discourse.itk.org/u/fepegar)
#### Post date: [August 7, 2020, 6:33pm UTC](https://discourse.itk.org/t/extract-overlapping-patches-from-a-3d-medical-image-and-reconstruct-the-patches-back-to-an-image/3368/4 "2020-08-07T18:33:30Z")

</div>

Thanks, @ibro45! @DL_Begin you can also check e.g. the [`WeightedSampler`](https://torchio.readthedocs.io/data/patch_based.html) for that. TorchIO uses ITK a lot under the hood, so the libraries are very compatible.
