How to extract 2D patches from 3D medical image


I have two different modality dicom data set ,CT :512 X 512 X 568 and PET:200 X 200 X 426
Both images are preprocessed to get a volumetric data of equal size and pixel spacing .
The dimensions after resampling are 143 X 143 X 284 for both with slice thickness of 3mm.
Now I want to extract patch of 64 X 64 X 64 from my preprocessed data by sliding the window with
overlap size of 18 × 18 × 18 to its neighbouring patches.
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.
Can anyone explain me how can I acheive this in pytorch

Hi @Surbhi_Khushu,

You can achieve that using TorchIO:

from pathlib import Path
import torchio as tio

out_dir = Path('/tmp/patches')

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 =

for i, patch in enumerate(sampler): / f'ct_{i}.nii.gz') / f'mr_{i}.nii.gz')

Full volumes:

One of the extracted patches:

Take a look at the documentation for patch-based pipelines.

By the way, in the title you say “2D patches” but then you mention 3D shapes in your post.

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?

I guess I already replied to you here: Creating non overlapping patches and reconstructing image back from the patches