# Slice 3D volume along a specific axis using python ITK

**URL:** https://discourse.itk.org/t/slice-3d-volume-along-a-specific-axis-using-python-itk/6005
**Category:** Beginner Questions
**Tags:** itk, python, dicom
**Created:** [June 21, 2023, 8:38am UTC](https://discourse.itk.org/t/slice-3d-volume-along-a-specific-axis-using-python-itk/6005 "2023-06-21T08:38:47Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![zemmyang](https://discourse.itk.org/user_avatar/discourse.itk.org/zemmyang/32/3501_2.png) [@zemmyang](https://discourse.itk.org/u/zemmyang)
#### Post date: [June 21, 2023, 8:38am UTC](https://discourse.itk.org/t/slice-3d-volume-along-a-specific-axis-using-python-itk/6005/1 "2023-06-21T08:38:47Z")

</div>

I found [Read DICOM Series and Write 3D Image — v5.3.0](https://examples.itk.org/src/io/gdcm/readdicomseriesandwrite3dimage/documentation) and it works, but I can only get slices from one axis (along z in my case). Is there a way to specify which axis I’m slicing along (I need x and y as well).

I saw a thread ([Extract an oblique 2D slice from a 3D volume](https://discourse.itk.org/t/extract-an-oblique-2d-slice-from-a-3d-volume/4137)) that suggested that I can use resample image filter, but I couldn’t find any examples for it.

ps: I used to do it by reading the DICOM files using SimpleITK, converting to VTK, and then doing the slicing in VTK. But now I need to use ITK-specific functions on the sliced images, so it would be better (at least, I think) if the only thing that VTK was doing is to display the image, with everything else being done by ITK.

---

<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: [June 21, 2023, 12:58pm UTC](https://discourse.itk.org/t/slice-3d-volume-along-a-specific-axis-using-python-itk/6005/2 "2023-06-21T12:58:06Z")

</div>

You could take a look at this thread:

> [@RegionOfInterestImageFilter to extract image slice from volume](https://discourse.itk.org/t/regionofinterestimagefilter-to-extract-image-slice-from-volume/4787):
>
> Hello, I’d like to extract a 2D slice from a MRI NIfTI file. I believe the RegionOfInterestImageFilter function in itk will do the job: [https://itk.org/ITKExamples/src/Filtering/ImageGrid/ExtractRegionOfInterestInOneImage/Documentation.html](https://itk.org/ITKExamples/src/Filtering/ImageGrid/ExtractRegionOfInterestInOneImage/Documentation.html) However, I’m having difficulties using this function in Python. I’ve tried writing the code this way, without success: image\_slice11 = itk.region\_of\_interest\_filter(image, [512,512,1], [0,0,11]) Can you provide advice on how that should be implemented? B…

---

<div class="post-metadata">

### Author: ![dchen](https://discourse.itk.org/user_avatar/discourse.itk.org/dchen/32/34_2.png) [@dchen](https://discourse.itk.org/u/dchen)
#### Post date: [June 21, 2023, 1:50pm UTC](https://discourse.itk.org/t/slice-3d-volume-along-a-specific-axis-using-python-itk/6005/3 "2023-06-21T13:50:15Z")

</div>

With SimpleITK you can extract the slices like this:

```
import SimpleITK as sitk

vol = sitk.Image(100, 100, 100, sitk.sitkUInt8)

# extract the 42nd X, Y and Z slices
xslice = vol[42, :, :]
yslice = vol[:, 42, :]
zslice = vol[:, :, 42]

```

---

<div class="post-metadata">

### Author: ![jasq](https://discourse.itk.org/user_avatar/discourse.itk.org/jasq/32/3067_2.png) [@jasq](https://discourse.itk.org/u/jasq)
#### Post date: [June 27, 2023, 8:51am UTC](https://discourse.itk.org/t/slice-3d-volume-along-a-specific-axis-using-python-itk/6005/4 "2023-06-27T08:51:08Z")

</div>

You can also try FREDtools with uses SimpleITK. An example code:

`import fredtools as ft`  
`import matplotlib.pyplot as plt`  
` `  
`# read 3D image from mhd file (or create/read any other format as a SimpleITK object)`  
`img=ft.readMHD('file_name.mhd', displayInfo=True)`  
` `  
`# Get 2D SimpleITK from 3D SimpleITK`  
`# Any slice, for instance 'XY', 'ZX', or even something like 'X-Z' can be calculated`  
`# The slice will be interpolated from 3D, including 'linear' or 'nearest' interpolation methods`  
`# The 'point' describes (x,y,z) coordinates in the image units (e.g. mm) to get the slice through`  
` `  
`slice=ft.getSlice(img, point=[0,4,5], plane='XY', interpolation='linear') `  
` `  
`# display the 2D slice or use ft.showSlice(...) method`  
`plt.imshow(ft.arr(slice), extent=ft.getExtMpl(slice), cmap='jet')`

check [https://fredtools.ifj.edu.pl/](https://fredtools.ifj.edu.pl/) manual and tutorials on [github](https://github.com/jasqs/FREDtools/tree/main) for more details and find more functionalities of the FREDtools.

Disclaimer: I am one of the authors.

---

<div class="post-metadata">

### Author: ![zemmyang](https://discourse.itk.org/user_avatar/discourse.itk.org/zemmyang/32/3501_2.png) [@zemmyang](https://discourse.itk.org/u/zemmyang)
#### Post date: [June 28, 2023, 6:36am UTC](https://discourse.itk.org/t/slice-3d-volume-along-a-specific-axis-using-python-itk/6005/5 "2023-06-28T06:36:38Z")

</div>

Thanks, but I specifically need to use ITK to do the slicing, **not SimpleITK** , because I need to use the sliced images in ITK Elastix. As I understand, the only way to go from SimpleITK to ITK is by converting it to a numpy array, which is not ideal.

---

<div class="post-metadata">

### Author: ![jasq](https://discourse.itk.org/user_avatar/discourse.itk.org/jasq/32/3067_2.png) [@jasq](https://discourse.itk.org/u/jasq)
#### Post date: [June 28, 2023, 7:11am UTC](https://discourse.itk.org/t/slice-3d-volume-along-a-specific-axis-using-python-itk/6005/6 "2023-06-28T07:11:20Z")

</div>

As far as I know, the SimpleITK and ITK image objects are very similar. FREDtools provides two routines:

`fredtools.SITK2ITK(imgSITK)`  
and  
`fredtools.ITK2SITK(imgITK)`

to convert between them.

The approach proposed by @dchen is also very useful but allows getting a slice only through the centre of a voxel (equivalent to the `nearest` interpolation method). Moreover, you have to calculate the voxel from the real-world coordinates.

---

<div class="post-metadata">

### Author: ![dchen](https://discourse.itk.org/user_avatar/discourse.itk.org/dchen/32/34_2.png) [@dchen](https://discourse.itk.org/u/dchen)
#### Post date: [June 28, 2023, 12:11pm UTC](https://discourse.itk.org/t/slice-3d-volume-along-a-specific-axis-using-python-itk/6005/7 "2023-06-28T12:11:31Z")

</div>

You could use the PermuteAxesImageFilter to create different versions of your volume. Map the X axis to Z, and then slice that volume to get X slices, and similarly for Y to Z.

---

<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: [June 29, 2023, 2:00pm UTC](https://discourse.itk.org/t/slice-3d-volume-along-a-specific-axis-using-python-itk/6005/8 "2023-06-29T14:00:41Z")

</div>

Maybe apply this suggestion:

> [@RegionOfInterestImageFilter to extract image slice from volume](https://discourse.itk.org/t/regionofinterestimagefilter-to-extract-image-slice-from-volume/4787/6):
>
> Another option to be aware of: a) use itk.xarray\_from\_image, b) do the slicing with pandas/numpy syntax, c) then itk.image\_from\_xarray. This may be more convenient while preserving spatial metadata.
