TriangleMeshToBinaryImageFilter In Python

itkv5
python

(Roger) #1

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!


(Matt McCormick) #2

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()

(Roger) #3

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?


(Matt McCormick) #4

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