How to extract 2D patches from 3D medical image

username: Surbhi_Khushu
cooked: <p>Hi,</p>
<p>I have two different modality dicom data set ,CT :512 X 512 X 568 and PET:200 X 200 X 426<br>
Both images are preprocessed to get a volumetric data of equal size and pixel spacing .<br>
The dimensions after resampling are 143 X 143 X 284 for both with slice thickness of 3mm.<br>
Now I want to extract patch of 64 X 64 X 64 from my preprocessed data  by sliding the window with<br>
overlap size of 18 × 18 × 18 to its neighbouring patches.<br>
My aim is to extract these patches from my volume data and pass it to the U Net based generator model of my cycle GANs.<br>
Can anyone explain me how can I acheive this in pytorch</p>

username: fepegar
cooked: <p>Hi <a class="mention" href="/u/surbhi_khushu">@Surbhi_Khushu</a>,</p>
<p>You can achieve that using <a href="https://torchio.readthedocs.io/" rel="noopener nofollow ugc">TorchIO</a>:</p>
<pre data-code-wrap="python"><code class="lang-python">from pathlib import Path
import torchio as tio

out_dir = Path('/tmp/patches')
out_dir.mkdir(exist_ok=True)

patch_size = 64
patch_overlap = 18

subject = tio.Subject(
    ct=tio.Image('/tmp/ct.nii.gz', type=tio.INTENSITY),
    mr=tio.Image('/tmp/mr.nii.gz', type=tio.INTENSITY),
)

sampler = tio.data.GridSampler(
    subject,
    patch_size,
    patch_overlap,
)

for i, patch in enumerate(sampler):
    patch.ct.save(out_dir / f'ct_{i}.nii.gz')
    patch.mr.save(out_dir / f'mr_{i}.nii.gz')
</code></pre>
<p>Full volumes:</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/2/d/2d9932175857e3224bdfff0381ae32782f5d068d.png" data-download-href="https://discuss.pytorch.org/uploads/default/2d9932175857e3224bdfff0381ae32782f5d068d" title="Screenshot"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/2/d/2d9932175857e3224bdfff0381ae32782f5d068d_2_543x500.png" alt="Screenshot" data-base62-sha1="6vnHNjlsAP23u5O2tkuo6BgGKu1" width="543" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/2/d/2d9932175857e3224bdfff0381ae32782f5d068d_2_543x500.png, https://discuss.pytorch.org/uploads/default/optimized/3X/2/d/2d9932175857e3224bdfff0381ae32782f5d068d_2_814x750.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/2/d/2d9932175857e3224bdfff0381ae32782f5d068d_2_1086x1000.png 2x" data-dominant-color="262624"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot</span><span class="informations">1410×1298 211 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>One of the extracted patches:</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/b/3/b330633d6fabbde9bb5106f5a13f865b595ce6b1.png" data-download-href="https://discuss.pytorch.org/uploads/default/b330633d6fabbde9bb5106f5a13f865b595ce6b1" title="Screenshot"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/b/3/b330633d6fabbde9bb5106f5a13f865b595ce6b1_2_538x500.png" alt="Screenshot" data-base62-sha1="pzb8vJ0Uj3P1C6S0rz9uFItlHgZ" width="538" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/b/3/b330633d6fabbde9bb5106f5a13f865b595ce6b1_2_538x500.png, https://discuss.pytorch.org/uploads/default/optimized/3X/b/3/b330633d6fabbde9bb5106f5a13f865b595ce6b1_2_807x750.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/b/3/b330633d6fabbde9bb5106f5a13f865b595ce6b1_2_1076x1000.png 2x" data-dominant-color="343332"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot</span><span class="informations">1398×1298 162 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>Take a look at the documentation for <a href="https://torchio.readthedocs.io/data/patch_based.html" rel="noopener nofollow ugc">patch-based pipelines</a>.</p>

username: fepegar
cooked: <p>By the way, in the title you say “2D patches” but then you mention 3D shapes in your post.</p>

username: CS.Enthu
cooked: <p>Actually I wanted to create 2d patches from 3D data(my mistake in the question). But since from  143 X 143 X 284 I want a patch of 64 x 64 , padding would be required here right?</p>

username: fepegar
cooked: <p>I guess I already replied to you here: <a href="https://discuss.pytorch.org/t/creating-non-overlapping-patches-and-reconstructing-image-back-from-the-patches/88417/6" class="inline-onebox">Creating non overlapping patches and reconstructing image back from the patches</a></p>