Hi, I’m trying to get a 3D binary image from a mesh stl file. I found an example in c++ using the TriangleMeshToBinaryImageFilter function. I was wondering if this function is available from the Python interface?
Thanks!
Hi Roger,
This patch adds Python wrapping for TriangleMeshToBinaryImageFilter
:
https://github.com/InsightSoftwareConsortium/ITK/pull/516
Since you are interested in STL files, it is worth noting that Python packages for ITKIOMeshSTL were recently released. Install with:
python -m pip install itk-iomeshstl
Then, read a mesh file, e.g.:
import itk
MeshType = itk.Mesh[itk.F, 3]
reader = itk.MeshFileReader[MeshType].New()
meshIO = itk.STLMeshIO.New()
reader.SetMeshIO(meshIO)
reader.SetFileName("/path/to/mymesh.stl")
reader.Update()
mesh = reader.GetOutput()
Hi Matt, thanks for thi addition! Could you please explain to me how can I install it? usually I would just use pip
python -m pip install --upgrade --pre itk
but I am not sure if I have to install from source?
Hi Roger,
This will be available in the upcoming ITK 5.0 Release Candidate 2 – in the meantime, if you have Linux, you could try the nightly Python package builds:
python -m pip install --upgrade pip numpy
python -m pip install itk --upgrade --no-index \
-f https://github.com/InsightSoftwareConsortium/ITKPythonPackage/releases/tag/latest
I used the above code, but get the error like Segmentation fault (core dumped).
Hi @buaaduke,
Thanks for the note.
We an issue we are resolving with remote module Python packages in ITK 5.0 RC 2 that causes this segmentation fault. We aim to have these resolved with the 5.0.0 release.
The itk-iomeshstl
package will encounter this issue, but the itk.TriangleMeshToBinaryImageFilter
should be available in the 5.0 RC 2 packages. It is possible to write the result with one of the other mesh file formats supported by ITK in the meantime.
CC: @fbudin
Hi @matt.mccormick,
Thank you for your reply. I also tried to use ITKIOMeshOBJ to load the mesh file (.obj), code as follows,
import itk
meshType = itk.Mesh[itk.F, 3]
meshReader = itk.MeshFileReader[meshType].New()
meshIO = itk.OBJMeshIO.New()
meshReader.SetMeshIO(meshIO)
meshReader.SetFileName(‘bunny.obj’)
meshReader.Update()
but got error like ‘Bus error (core dumped)’. I used itk-5.0rc2.
@buaaduke thanks for the follow-up - this issue was created to track progress on the updated reading.
@buaaduke: I tried to reproduce your error using ITK Python v5.0rc2 and it actually worked on my machine with the model that I provided. Would you be able to share the model that you used that creates the error?
ok, see the attachment.
bunny.obj (386.3 KB)
I was able to reproduce the error on my machine. Thanks for sharing the data.
For some reason this obj
file has more vertex normals (4968) than vertices (2503) which makes ITK crash. I fixed ITK so it does not crash, but I don’t know what the correct behavior should be: Error message, ignore,… My Work-In-Progress pull request is here.
Is the number of vertex normals equal to number of faces, or triple number of faces?
I have a really dumb question, but I have imported a model using numpy-stl into python. How do I go baout using the TriangleMeshToBinaryIMageFilter now? I would like to use the native dimensions of the 3D model to generate the binary image so I can then extract features with the pyradiomics python package.
The minimum code effort is to write it to disk, and then load it using itk. You could also do in-memory conversion, but that will require more code. If you want to go that route, you could take some inspiration from:
https://examples.itk.org/src/core/quadedgemesh/createtriangularquadedgemesh/documentation
Ok, so is there any code examples for me to be able to import the mesh into python, and use the TriangleMeshToBinaryImageFilter in order to use the pyradiomics package? I don’t have to currently write it to disc, I already have the stl files written, I just need to know how to load it into itk and then run the image filtering on it to get the image data for use with the pyradiomics package.
Does the code from the beginning of this topic not work? What is the error? The filter’s documentation and the C++ example linked there should be enough to get you started on how to use it.
It does not seem to work. Every time I run it, it actually causes my jupyter notebook kernel to die. So considering I am at my reply limit currently, I will just keep editing these previous posts. This is what happens everytime I try to import the stl file using the inbuilt methods for importing them. Just to be safe, I also tried imnporting it through vtk, and was successful.
Also, I am trying to work in python as I do not know C++ and cannot decipher the C++ code you keep directing me to. Is there maybe a clear way to convert a vtkPolyData mesh into an ITK mesh in order to use this image filter?
This works for me:
C:\Users\Dzenan>python
Python 3.9.13 (tags/v3.9.13:6de2ca5, May 17 2022, 16:36:42) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import itk
>>> MeshType = itk.Mesh[itk.F, 3]
>>> reader = itk.MeshFileReader[MeshType].New()
>>> reader.SetFileName(r"M:\Work\spine\L1.vtk")
>>> reader.Update()
>>> mesh = reader.GetOutput()
>>> mesh
<itk.itkMeshBasePython.itkMeshF3; proxy of <Swig Object of type 'itkMeshF3 *' at 0x00000213E268EFC0> >
>>> mesh.GetNumberOfPoints()
82416
>>> itk.__version__
'5.3.0'
>>>