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

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.

For the slices might be worth looking at the filter slice_along_axis in pyvista see docs and some general slicing examples can be found here, Slicing — PyVista 0.44.1 documentation.

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.

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 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.

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.

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.

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), 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.