TriangleMeshToBinaryImageFilter In Python

These are the key parts from this example:

using FilterType = itk::TriangleMeshToBinaryImageFilter<MeshType, OutputImageType>;
auto filter = FilterType::New();
filter->SetInput(meshReader->GetOutput());
filter->SetInfoImage(cast->GetOutput());
filter->SetInsideValue(itk::NumericTraits<OutputPixelType>::max());
filter->Update();

This is attempt at translating it into python:

UCType = itk.image[itk.UC, 3]
ConverterType = itk.TriangleMeshToBinaryImageFilter[MeshType, UCType]
filter = ConverterType.New()
filter.SetInput(mesh)  # mesh is read from file
# referenceImage is read from file or constructed by setting size, origin, and spacing
filter.SetInfoImage(referenceImage)
filter.SetInput(255)
filter.Update()
itk.imwrite(filter.GetOutput(), "mask.nrrd")

I am currently unable to still import the stl file into ITK at all to be able to do anything. This is why I was hoping for an option involving vtk, as the jupyter notebook fails every time I try to load the stl file in with ITK.

If you’re using VTK, you can use the vtkPolyDataToImageStencil to scan convert a mesh into a volume image.

1 Like

Example for that (also in C++) is here.

To load an STL file:

pip install itk-iomeshstl
import itk
mesh = itk.meshread('mesh.stl')
1 Like