# How to create vtkUnstructuredGrid from an itk::Image?

**URL:** <https://discourse.itk.org/t/how-to-create-vtkunstructuredgrid-from-an-itk-image/2364>\
**Category:** Beginner Questions\
**Created:** [October 30, 2019, 12:45pm UTC](https://discourse.itk.org/t/how-to-create-vtkunstructuredgrid-from-an-itk-image/2364 "2019-10-30T12:45:38Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sara\_Caffe](https://discourse.itk.org/user_avatar/discourse.itk.org/sara_caffe/32/974_2.png) [@Sara\_Caffe](https://discourse.itk.org/u/Sara_Caffe)\
**Post date:** [October 30, 2019, 12:45pm UTC](https://discourse.itk.org/t/how-to-create-vtkunstructuredgrid-from-an-itk-image/2364/1 "2019-10-30T12:45:38Z")

</div>

Hi,

I have binary label files in `itk::Image`. I have gone through a few examples in ITK and VTK. What I understood, we need to create the cell type object for generating a specific mesh in both ITK and Vtk. For example, I need to generate a tetrahedron mesh, and I need to choose `typedef MeshType::CellType CellType;` in which CellType can be various type of cells.

In VTK, there is [an example](https://lorensen.github.io/VTKExamples/site/Cxx/GeometricObjects/Cell3DDemonstration/) that they create the mesh cells. However, I do not how to connect the data types between itk and vtk.

How can I create the mesh from itk::Image and from that generate vtkUnstructuredGrid?  
Is there any example available to show some tips and tricks?

Thanks

---

<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:** [October 30, 2019, 1:02pm UTC](https://discourse.itk.org/t/how-to-create-vtkunstructuredgrid-from-an-itk-image/2364/2 "2019-10-30T13:02:18Z")

</div>

The answer mostly depends on what you are trying to accomplish. Generally, some classes from [ITKVtkGlue](https://itk.org/Doxygen/html/group__ITKVtkGlue.html) could help.

---

<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:** [October 30, 2019, 1:04pm UTC](https://discourse.itk.org/t/how-to-create-vtkunstructuredgrid-from-an-itk-image/2364/3 "2019-10-30T13:04:48Z")

</div>

The ITKVtkGlue external module in particular has a class with this functionality:

[https://github.com/InsightSoftwareConsortium/ITKVtkGlue](https://github.com/InsightSoftwareConsortium/ITKVtkGlue)

i.e. [`itk::MeshToVTKUnstructuredGridFilter`](https://github.com/InsightSoftwareConsortium/ITKVtkGlue/blob/master/include/itkMeshToVTKUnstructuredGridFilter.h).

---

<div class="post-metadata">

**Author:** ![Sara\_Caffe](https://discourse.itk.org/user_avatar/discourse.itk.org/sara_caffe/32/974_2.png) [@Sara\_Caffe](https://discourse.itk.org/u/Sara_Caffe)\
**Post date:** [October 31, 2019, 7:16am UTC](https://discourse.itk.org/t/how-to-create-vtkunstructuredgrid-from-an-itk-image/2364/4 "2019-10-31T07:16:24Z")

</div>

Is it like that, I need to follow the following transformation:

itk::Image ==\> create the point sets ([link](https://itk.org/Doxygen/html/Examples_2DataRepresentation_2Mesh_2ImageToPointSet_8cxx-example.html)) ==\> generate the tetrahedron mesh from the point sets and save as an `vtkunstructuredGrid` ( MakeTetrahedron() function in [this link](https://lorensen.github.io/VTKExamples/site/Cxx/GeometricObjects/Cell3DDemonstration/))

Is this procedure correct?

---

<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:** [October 31, 2019, 3:02pm UTC](https://discourse.itk.org/t/how-to-create-vtkunstructuredgrid-from-an-itk-image/2364/6 "2019-10-31T15:02:06Z")

</div>

You might need [BinaryMask3DMeshSource](https://itk.org/Doxygen/html/classitk_1_1BinaryMask3DMeshSource.html) as the first step.

---

<div class="post-metadata">

**Author:** ![Sara\_Caffe](https://discourse.itk.org/user_avatar/discourse.itk.org/sara_caffe/32/974_2.png) [@Sara\_Caffe](https://discourse.itk.org/u/Sara_Caffe)\
**Post date:** [November 17, 2019, 5:08am UTC](https://discourse.itk.org/t/how-to-create-vtkunstructuredgrid-from-an-itk-image/2364/7 "2019-11-17T05:08:41Z")

</div>

Hi again,

I have enabled ITKVtkGlue Module and recompiled and rebuilt ITK. Then, I added, `ITKVtkGlue` in the cmakeList find package list, however, once I am trying to include `#include "itkMeshToVTKUnstructuredGridFilter.h"` in the code , what could be the reason? Should I add other modules to the cmakeList?

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:** [November 19, 2019, 4:35pm UTC](https://discourse.itk.org/t/how-to-create-vtkunstructuredgrid-from-an-itk-image/2364/8 "2019-11-19T16:35:45Z")

</div>

Hi Sara,

The ITKVtkGlue repository is ahead of the version distributed in ITK and `"itkMeshToVTKUnstructuredGridFilter.h"` is not yet in ITK. One option is to build the module externally; that is, clone the repository:

[https://github.com/InsightSoftwareConsortium/ITKVtkGlue](https://github.com/InsightSoftwareConsortium/ITKVtkGlue)

Then create a build directory, and build against the ITK build tree like it is an application being built against ITK. Then, build your application.

Or, since they are templated headers, you could just copy the `*.h` and `*.hxx` files.

---

<div class="post-metadata">

**Author:** ![Sara\_Caffe](https://discourse.itk.org/user_avatar/discourse.itk.org/sara_caffe/32/974_2.png) [@Sara\_Caffe](https://discourse.itk.org/u/Sara_Caffe)\
**Post date:** [November 20, 2019, 11:51am UTC](https://discourse.itk.org/t/how-to-create-vtkunstructuredgrid-from-an-itk-image/2364/9 "2019-11-20T11:51:37Z")

</div>

@matt.mccormick Thank you, Sir, I am just checking with you if the installation procedure is correct?

I tried to follow [the link](https://discourse.itk.org/t/remote-itkmodule-build-on-top-of-another/1547) that you had guided a user. I set the `ITK_DIR` and `VTK_DIR` to the build directory of each application. Then, I set the following flags:

BUILD\_SHARED\_LIBS ON  
BUILD\_TESTING OFF  
ITK\_USE\_KWSTYLE OFF

ITK\_DIR /home/esara/software/vtk-itk/ITK-build  
VTK\_DIR /home/esara/software/vtk-itk/VTK-build

- I removed the ITK prefix in [this line](https://github.com/InsightSoftwareConsortium/ITKVtkGlue/blob/master/itk-module.cmake#L7)

After running cmake successfully, I could build the ITKVtkGlue

```
~/software/vtk-itk/ITKVtkGlue-build$ sudo make -j4
Scanning dependencies of target VtkGlue-all
Scanning dependencies of target ITKData
Built target VtkGlue-all
Built target ITKData

```

Thank you

---

<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:** [November 20, 2019, 4:14pm UTC](https://discourse.itk.org/t/how-to-create-vtkunstructuredgrid-from-an-itk-image/2364/10 "2019-11-20T16:14:27Z")

</div>

Perfect, Sara – great work!

---

<div class="post-metadata">

**Author:** ![Sara\_Caffe](https://discourse.itk.org/user_avatar/discourse.itk.org/sara_caffe/32/974_2.png) [@Sara\_Caffe](https://discourse.itk.org/u/Sara_Caffe)\
**Post date:** [November 21, 2019, 6:07am UTC](https://discourse.itk.org/t/how-to-create-vtkunstructuredgrid-from-an-itk-image/2364/11 "2019-11-21T06:07:46Z")

</div>

Thanks for replying the message.

> [@matt.mccormick](#):
>
> Or, since they are templated headers, you could just copy the `*.h` and `*.hxx` files.

Do you mean [these files](https://github.com/InsightSoftwareConsortium/ITKVtkGlue/tree/master/include)?  
Where should I copy the files?

Still the `#include "itkMeshToVTKUnstructuredGridFilter.h"` is shown with red underline indicating has not been included yet. Or should I include any module in the `CmakeList`file of my code?

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:** [November 24, 2019, 3:40am UTC](https://discourse.itk.org/t/how-to-create-vtkunstructuredgrid-from-an-itk-image/2364/12 "2019-11-24T03:40:02Z")

</div>

Include `ITKVtkGlue` in the `find_package(ITK [...]` `COMPONENTS` list. For example, in your _CMakeLists.txt_ file:

```auto
find_package(ITK REQUIRED
  COMPONENTS
    ITKVtkGlue
    ITKImageIO
    # Any other ITK modules your application requires. These can be generated with https://github.com/InsightSoftwareConsortium/ITK/blob/master/Utilities/Maintenance/WhatModulesITK.py
  )
# This line adds the include directories required to use the modules listed above to any executables that follow
include(${ITK_USE_FILE})

add_executable(MyExecutable MyExecutable.cxx)
target_link_libraries(MyExecutable ${ITK_LIBRARIES})

```

---

<div class="post-metadata">

**Author:** ![Sara\_Caffe](https://discourse.itk.org/user_avatar/discourse.itk.org/sara_caffe/32/974_2.png) [@Sara\_Caffe](https://discourse.itk.org/u/Sara_Caffe)\
**Post date:** [November 26, 2019, 12:53pm UTC](https://discourse.itk.org/t/how-to-create-vtkunstructuredgrid-from-an-itk-image/2364/13 "2019-11-26T12:53:47Z")

</div>

@matt.mccormick Hi again and sorry for inconveniences. I had already included `ITKVtkGlue` in the ITK package. That is why I asked the reason here. Could not find any reason!
