# Creating 3D objects and slice them to obtain 2D numpy array

**URL:** https://discourse.vtk.org/t/creating-3d-objects-and-slice-them-to-obtain-2d-numpy-array/14768
**Category:** Support
**Tags:** python
**Created:** [October 25, 2024, 10:14am UTC](https://discourse.vtk.org/t/creating-3d-objects-and-slice-them-to-obtain-2d-numpy-array/14768 "2024-10-25T10:14:55Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Sah](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/s/ecc23a/32.png) [@Sah](https://discourse.vtk.org/u/Sah)
#### Post date: [October 25, 2024, 10:14am UTC](https://discourse.vtk.org/t/creating-3d-objects-and-slice-them-to-obtain-2d-numpy-array/14768/1 "2024-10-25T10:14:55Z")

</div>

Dear all,

this might be a stupid question, but I am completely new in the topic and I need some help in understanding the best approach. I am creating an anatomical model, and the idea is that when I’m done with it, I need to be able to slice it with planes and convert the slice to a numpy object to do further analysis with python (that have nothing to do with vtk). I have done good progress in creating this model using “vtk.PolyData” but it’s really hard to find any documentation regarding the slicing. Please note, that I want surfaces to be displayed in my slice, not just points. Today I have done some research and looks like vtkImageData is more helpful for what I have to do. The problem is that with vtkPolyData I have found interesting tools helping me designing the geometries i’m interested into (e.g., vtkTubeFilter, vtkSphere, etc…) which I can not find in vtkImageData. I also couldn’t find a way to convert my vtkPolyData in vtkImageData.

So I’m here asking some guidance from you. what is the best tool that could help me in realizing what I want to do? (slicing surfaces from a 3D object). Is there any guidance/documentation online that I could follow?

Thank you.

---

<div class="post-metadata">

### Author: ![gnikit](https://discourse.vtk.org/user_avatar/discourse.vtk.org/gnikit/32/2624_2.png) [@gnikit](https://discourse.vtk.org/u/gnikit)
#### Post date: [October 25, 2024, 3:35pm UTC](https://discourse.vtk.org/t/creating-3d-objects-and-slice-them-to-obtain-2d-numpy-array/14768/2 "2024-10-25T15:35:19Z")

</div>

For the slices might be worth looking at the filter `slice_along_axis` in pyvista see [docs](https://docs.pyvista.org/api/core/_autosummary/pyvista.datasetfilters.slice_along_axis#pyvista.DataSetFilters.slice_along_axis) and some general slicing examples can be found here, [Slicing — PyVista 0.44.1 documentation](https://docs.pyvista.org/examples/01-filter/slicing#slice-example).

For every slice you should then have access to the cell and point data, already in numpy arrays, which you can use to do your further processing.

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [October 26, 2024, 3:15am UTC](https://discourse.vtk.org/t/creating-3d-objects-and-slice-them-to-obtain-2d-numpy-array/14768/3 "2024-10-26T03:15:40Z")

</div>

All these operations are really easy to do in 3D Slicer, either using the GUI or by Python scripting. For example, you can create detailed, anatomically very accurate models directly from DICOM images by a few clicks and get the voxels as a 3D numpy array. Or, you can import a 3D mesh file that you created elsewhere (using VTK, pyvista, or any modeling software) and get a series of TIFF files by slicing them by copy-pasting [this code snippet](https://discourse.slicer.org/t/convert-stl-file-to-high-resolution-tiff-stack/15949/8?u=lassoan) into Slicer’s Python console.

What are you going to use the slices for? Bitmap 3D printing? Dose computation? Further processing, analysis, segmentation, registration,…? There are tools for these and a lot more in 3D Slicer. If you describe what your overall goal is then we may be able to give you more specific advice.

---

<div class="post-metadata">

### Author: ![Sah](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/s/ecc23a/32.png) [@Sah](https://discourse.vtk.org/u/Sah)
#### Post date: [October 28, 2024, 9:38am UTC](https://discourse.vtk.org/t/creating-3d-objects-and-slice-them-to-obtain-2d-numpy-array/14768/4 "2024-10-28T09:38:52Z")

</div>

Thank you Andras. I have tried to use 3D slicer already, but did not work out with the “.vti” file I obtain out of vtkPolyData object. Moreover, the computation that I need to do after obtaining a slice need to be automatized, and that’s why I was originally thinking to do it programmatically with python.

---

<div class="post-metadata">

### Author: ![Sah](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/s/ecc23a/32.png) [@Sah](https://discourse.vtk.org/u/Sah)
#### Post date: [October 28, 2024, 9:42am UTC](https://discourse.vtk.org/t/creating-3d-objects-and-slice-them-to-obtain-2d-numpy-array/14768/5 "2024-10-28T09:42:11Z")

</div>

Thank you Giannis, this looks like what I want. However, I think that with vtkPolyData I do not have volumes, but just points in the space. I have tried to convert my PolyData to ImageData, but without success. For example, I have tried to convert a TubeFilter in ImageData using ImageStencil, but what I have as output is just a rectangular shape and not a tube.  
The alternative would be of course trying to do the tube shapes with ImageData directly, rather than converting from PolyData, but I am not finding so many examples to follow. Maybe you know some more documentation that I don’t? Thank you.

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [October 28, 2024, 11:36am UTC](https://discourse.vtk.org/t/creating-3d-objects-and-slice-them-to-obtain-2d-numpy-array/14768/6 "2024-10-28T11:36:06Z")

</div>

You can convert between vtkPolyData and vtkImageData using VTK. It is not a single command, because you need to specify the desired resolution, extents, axis directions when converting to image, and you need to make the mesh smooth when converting to mesh.

We have not enabled loading of .vti files in Slicer using GUI (but you can use the Python API - see [here](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#load-volume-from-vti-file)), because we did not want to encourage users to use it, as vti could not store image axis directions, it is only supported by a few VTK-based software, it is not particularly simple or efficient, and it is very limited compared to any other widely used research file formats, such as nrrd or nifti.

Both pyvista and Slicer wrap VTK and offers higher-level API, making the Python code a little shorter and simpler. Slicer also provides GUI, but you don’t have to use it. The deciding factor for choosing between Slicer and pyvista is what else you need to do.

---

<div class="post-metadata">

### Author: ![Sah](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/s/ecc23a/32.png) [@Sah](https://discourse.vtk.org/u/Sah)
#### Post date: [December 13, 2024, 3:54pm UTC](https://discourse.vtk.org/t/creating-3d-objects-and-slice-them-to-obtain-2d-numpy-array/14768/7 "2024-12-13T15:54:37Z")

</div>

Dear Andras,

thank you for you reply. Do you know where I can find documentation / examples for the conversion between vtkPolyData and vtkImageData in **python**? I wasn’t able to find anything that could help me.

Thanks a lot.
