# TriangleMeshToBinaryImageFilter In Python

**URL:** https://discourse.itk.org/t/trianglemeshtobinaryimagefilter-in-python/1604
**Category:** Beginner Questions
**Tags:** python, itkv5
**Created:** [February 15, 2019, 9:01pm UTC](https://discourse.itk.org/t/trianglemeshtobinaryimagefilter-in-python/1604 "2019-02-15T21:01:05Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![rogertrullo](https://discourse.itk.org/user_avatar/discourse.itk.org/rogertrullo/32/1953_2.png) [@rogertrullo](https://discourse.itk.org/u/rogertrullo)
#### Post date: [February 15, 2019, 9:01pm UTC](https://discourse.itk.org/t/trianglemeshtobinaryimagefilter-in-python/1604/1 "2019-02-15T21:01:05Z")

</div>

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!

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [February 17, 2019, 1:33pm UTC](https://discourse.itk.org/t/trianglemeshtobinaryimagefilter-in-python/1604/2 "2019-02-17T13:33:59Z")

</div>

Hi Roger,

This patch adds Python wrapping for `TriangleMeshToBinaryImageFilter`:

[https://github.com/InsightSoftwareConsortium/ITK/pull/516](https://github.com/InsightSoftwareConsortium/ITK/pull/516)

Since you are interested in STL files, it is worth noting that Python packages for [ITKIOMeshSTL](https://github.com/InsightSoftwareConsortium/ITKIOMeshSTL) were recently released. Install with:

```auto
python -m pip install itk-iomeshstl

```

Then, read a mesh file, e.g.:

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

```

---

<div class="post-metadata">

### Author: ![rogertrullo](https://discourse.itk.org/user_avatar/discourse.itk.org/rogertrullo/32/1953_2.png) [@rogertrullo](https://discourse.itk.org/u/rogertrullo)
#### Post date: [March 4, 2019, 5:10pm UTC](https://discourse.itk.org/t/trianglemeshtobinaryimagefilter-in-python/1604/3 "2019-03-04T17:10:34Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [March 11, 2019, 2:27am UTC](https://discourse.itk.org/t/trianglemeshtobinaryimagefilter-in-python/1604/4 "2019-03-11T02:27:26Z")

</div>

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:

```auto
python -m pip install --upgrade pip numpy
python -m pip install itk --upgrade --no-index \
  -f https://github.com/InsightSoftwareConsortium/ITKPythonPackage/releases/tag/latest

```

---

<div class="post-metadata">

### Author: ![buaaduke](https://discourse.itk.org/user_avatar/discourse.itk.org/buaaduke/32/759_2.png) [@buaaduke](https://discourse.itk.org/u/buaaduke)
#### Post date: [May 13, 2019, 1:37pm UTC](https://discourse.itk.org/t/trianglemeshtobinaryimagefilter-in-python/1604/5 "2019-05-13T13:37:12Z")

</div>

I used the above code, but get the error like Segmentation fault (core dumped).

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [May 13, 2019, 4:43pm UTC](https://discourse.itk.org/t/trianglemeshtobinaryimagefilter-in-python/1604/6 "2019-05-13T16:43:57Z")

</div>

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](https://itk.org/Doxygen/html/group__ITKIOMesh.html) in the meantime.

CC: @fbudin

---

<div class="post-metadata">

### Author: ![buaaduke](https://discourse.itk.org/user_avatar/discourse.itk.org/buaaduke/32/759_2.png) [@buaaduke](https://discourse.itk.org/u/buaaduke)
#### Post date: [May 14, 2019, 3:07am UTC](https://discourse.itk.org/t/trianglemeshtobinaryimagefilter-in-python/1604/7 "2019-05-14T03:07:37Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [May 14, 2019, 12:00pm UTC](https://discourse.itk.org/t/trianglemeshtobinaryimagefilter-in-python/1604/8 "2019-05-14T12:00:51Z")

</div>

@buaaduke thanks for the follow-up - [this issue](https://github.com/InsightSoftwareConsortium/ITK/issues/866) was created to track progress on the updated reading.

---

<div class="post-metadata">

### Author: ![fbudin](https://discourse.itk.org/user_avatar/discourse.itk.org/fbudin/32/14_2.png) [@fbudin](https://discourse.itk.org/u/fbudin)
#### Post date: [May 14, 2019, 3:31pm UTC](https://discourse.itk.org/t/trianglemeshtobinaryimagefilter-in-python/1604/9 "2019-05-14T15:31:29Z")

</div>

@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?

---

<div class="post-metadata">

### Author: ![buaaduke](https://discourse.itk.org/user_avatar/discourse.itk.org/buaaduke/32/759_2.png) [@buaaduke](https://discourse.itk.org/u/buaaduke)
#### Post date: [May 15, 2019, 3:00am UTC](https://discourse.itk.org/t/trianglemeshtobinaryimagefilter-in-python/1604/10 "2019-05-15T03:00:26Z")

</div>

ok, see the attachment.  
[bunny.obj](https://discourse.itk.org/uploads/default/original/1X/ced4efe8c1eb78342afe474f53ad4409ad5dfc0c.obj) (386.3 KB)

---

<div class="post-metadata">

### Author: ![fbudin](https://discourse.itk.org/user_avatar/discourse.itk.org/fbudin/32/14_2.png) [@fbudin](https://discourse.itk.org/u/fbudin)
#### Post date: [May 15, 2019, 7:29pm UTC](https://discourse.itk.org/t/trianglemeshtobinaryimagefilter-in-python/1604/11 "2019-05-15T19:29:06Z")

</div>

I was able to reproduce the error on my machine. Thanks for sharing the data.

---

<div class="post-metadata">

### Author: ![fbudin](https://discourse.itk.org/user_avatar/discourse.itk.org/fbudin/32/14_2.png) [@fbudin](https://discourse.itk.org/u/fbudin)
#### Post date: [May 16, 2019, 2:49pm UTC](https://discourse.itk.org/t/trianglemeshtobinaryimagefilter-in-python/1604/12 "2019-05-16T14:49:35Z")

</div>

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](https://github.com/InsightSoftwareConsortium/ITK/pull/883).

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [May 16, 2019, 3:13pm UTC](https://discourse.itk.org/t/trianglemeshtobinaryimagefilter-in-python/1604/13 "2019-05-16T15:13:11Z")

</div>

Is the number of vertex normals equal to number of faces, or triple number of faces?

---

<div class="post-metadata">

### Author: ![greglahmer2021](https://discourse.itk.org/letter_avatar_proxy/v4/letter/g/ac8455/32.png) [@greglahmer2021](https://discourse.itk.org/u/greglahmer2021)
#### Post date: [July 1, 2024, 1:54pm UTC](https://discourse.itk.org/t/trianglemeshtobinaryimagefilter-in-python/1604/14 "2024-07-01T13:54:27Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [July 1, 2024, 2:34pm UTC](https://discourse.itk.org/t/trianglemeshtobinaryimagefilter-in-python/1604/15 "2024-07-01T14:34:00Z")

</div>

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](https://examples.itk.org/src/core/quadedgemesh/createtriangularquadedgemesh/documentation)

---

<div class="post-metadata">

### Author: ![Gregori](https://discourse.itk.org/letter_avatar_proxy/v4/letter/g/ba8739/32.png) [@Gregori](https://discourse.itk.org/u/Gregori)
#### Post date: [July 9, 2024, 2:31pm UTC](https://discourse.itk.org/t/trianglemeshtobinaryimagefilter-in-python/1604/16 "2024-07-09T14:31:44Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [July 9, 2024, 2:49pm UTC](https://discourse.itk.org/t/trianglemeshtobinaryimagefilter-in-python/1604/17 "2024-07-09T14:49:44Z")

</div>

Does the [code from the beginning of this topic](https://discourse.itk.org/t/trianglemeshtobinaryimagefilter-in-python/1604/2) not work? What is the error? The [filter’s documentation](https://itk.org/Doxygen/html/classitk_1_1TriangleMeshToBinaryImageFilter.html) and the C++ example linked there should be enough to get you started on how to use it.

---

<div class="post-metadata">

### Author: ![Gregori](https://discourse.itk.org/letter_avatar_proxy/v4/letter/g/ba8739/32.png) [@Gregori](https://discourse.itk.org/u/Gregori)
#### Post date: [July 9, 2024, 2:56pm UTC](https://discourse.itk.org/t/trianglemeshtobinaryimagefilter-in-python/1604/18 "2024-07-09T14:56:23Z")

</div>

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.

 ![jupyter_notebook_screenshot](https://discourse.itk.org/uploads/default/original/2X/6/665c8cd3290018192f8ee3944a24e040c09fe545.png)

---

<div class="post-metadata">

### Author: ![Gregori](https://discourse.itk.org/letter_avatar_proxy/v4/letter/g/ba8739/32.png) [@Gregori](https://discourse.itk.org/u/Gregori)
#### Post date: [July 9, 2024, 3:04pm UTC](https://discourse.itk.org/t/trianglemeshtobinaryimagefilter-in-python/1604/19 "2024-07-09T15:04:02Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [July 9, 2024, 3:28pm UTC](https://discourse.itk.org/t/trianglemeshtobinaryimagefilter-in-python/1604/20 "2024-07-09T15:28:45Z")

</div>

This works for me:

```log
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'
>>> 

```

[Next page](https://discourse.itk.org/t/trianglemeshtobinaryimagefilter-in-python/1604.md?page=2)
