# Patch extraction

**URL:** https://discourse.itk.org/t/patch-extraction/3630
**Category:** Algorithms
**Tags:** python, simpleitk
**Created:** [November 17, 2020, 11:24pm UTC](https://discourse.itk.org/t/patch-extraction/3630 "2020-11-17T23:24:00Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![a10227](https://discourse.itk.org/user_avatar/discourse.itk.org/a10227/32/1690_2.png) [@a10227](https://discourse.itk.org/u/a10227)
#### Post date: [November 17, 2020, 11:24pm UTC](https://discourse.itk.org/t/patch-extraction/3630/1 "2020-11-17T23:24:00Z")

</div>

Good day,  
Could you please tell me, how I can save extracted patched from 3D image, so their original coordinates are preserved? My goal is to select some patches, so those, being combined together, form the original image. I understand that some properties e.g. origin should be changed, but I don’t understand which exactly properties should be changed (I’m just afraid to forget smth) and is there a fast way to do this without changing properties manually?

I have an image with size [512,512,512]. I am extracting patches from it using skimage function:

```
from skimage.util.shape import view_as_blocks
img = sitk.ReadImage(name)
arr = sitk.GetArrayFromImage(img)
windows = view_as_blocks(arr, windows_shape)
w = windows[i,j,z]
w_i = sitk.GetImageFromArray(w)
### add some translation so the voxel is on its original place
sitk.WriteImage(w_i,"res.nii")
```

---

<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: [November 18, 2020, 7:48pm UTC](https://discourse.itk.org/t/patch-extraction/3630/2 "2020-11-18T19:48:50Z")

</div>

[RegionOfInterestImageFilter](https://itk.org/Doxygen/html/classitk_1_1RegionOfInterestImageFilter.html) should do what you want. Here is an [example](https://itk.org/ITKExamples/src/Filtering/ImageGrid/ExtractRegionOfInterestInOneImage/Documentation.html) of how to use it. Of course, image format you are writing should support spatial position metadata. Your choice (nifti) does support it.

---

<div class="post-metadata">

### Author: ![a10227](https://discourse.itk.org/user_avatar/discourse.itk.org/a10227/32/1690_2.png) [@a10227](https://discourse.itk.org/u/a10227)
#### Post date: [November 19, 2020, 11:32am UTC](https://discourse.itk.org/t/patch-extraction/3630/3 "2020-11-19T11:32:41Z")

</div>

I will try that, thank you for the reply
