# convert .nii to a mesh

**URL:** https://discourse.vtk.org/t/convert-nii-to-a-mesh/10677
**Category:** Support
**Tags:** python, code, file-formats
**Created:** [February 14, 2023, 3:36pm UTC](https://discourse.vtk.org/t/convert-nii-to-a-mesh/10677 "2023-02-14T15:36:54Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Hamid\_Fsian](https://discourse.vtk.org/user_avatar/discourse.vtk.org/hamid_fsian/32/6114_2.png) [@Hamid\_Fsian](https://discourse.vtk.org/u/Hamid_Fsian)
#### Post date: [February 14, 2023, 3:36pm UTC](https://discourse.vtk.org/t/convert-nii-to-a-mesh/10677/1 "2023-02-14T15:36:54Z")

</div>

Dear Community,

I am trying to convert a niftii file into a mesh ( and later save it as .obj) and I want to color this latter using the pixel intesity from the nifti volume. Is it possible using vtk and python ?

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [February 14, 2023, 10:08pm UTC](https://discourse.vtk.org/t/convert-nii-to-a-mesh/10677/2 "2023-02-14T22:08:40Z")

</div>

Hello,

VTK has the `vtkOBJExporter` class ([https://vtk.org/doc/nightly/html/classvtkOBJExporter.html](https://vtk.org/doc/nightly/html/classvtkOBJExporter.html)) that saves the entire **scene** contained in a `vtkRenderWindow` as a `.OBJ` file. As far as I know, there is no data object-to-obj direct conversion. In other words, it exports the scene as rendered and not as data. I mean, if your volume is being rendered as a slice, there will be only a flat panel with a texture applied to it in the OBJ file (`vtkOBJExporter` also generates a complimentary `.mtl` file with material description when necessary). There is a small Python example on how to use `vtkOBJExporter` here: [https://discourse.vtk.org/t/how-can-i-export-tubes-to-obj/6015/12](https://discourse.vtk.org/t/how-can-i-export-tubes-to-obj/6015/12)

Now, if you aim at converting all the voxels of the input volume into grid cells of a mesh described as OBJ format, I believe you need to study the OBJ file format and write the code to generate the file explicitly.

take care,

Paulo

---

<div class="post-metadata">

### Author: ![Hamid\_Fsian](https://discourse.vtk.org/user_avatar/discourse.vtk.org/hamid_fsian/32/6114_2.png) [@Hamid\_Fsian](https://discourse.vtk.org/u/Hamid_Fsian)
#### Post date: [February 15, 2023, 10:23am UTC](https://discourse.vtk.org/t/convert-nii-to-a-mesh/10677/3 "2023-02-15T10:23:05Z")

</div>

Dear Paulo,

Thank you for your answer. It really helped.

My main problem is to color my mesh using the pixel intesity from my niftii volume. Is there a way to do it ?

thank you

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [February 15, 2023, 11:49am UTC](https://discourse.vtk.org/t/convert-nii-to-a-mesh/10677/4 "2023-02-15T11:49:51Z")

</div>

What class are you using to represent the image? `vtkImage`?

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [February 15, 2023, 12:48pm UTC](https://discourse.vtk.org/t/convert-nii-to-a-mesh/10677/5 "2023-02-15T12:48:00Z")

</div>

> [@Hamid\_Fsian](#):
>
> My main problem is to color my mesh using the pixel intesity from my niftii volume

You can use vtkProbeFilter for this.

Usually this coloring does not lead to meaningful result, as the mesh points are on the boundary of the segmented object. You end up just amplifying any segmentation imperfections.

What do you expect from this visualization? Would you like to display the volume similarly to volume rendering (raycasting), but with using just surface rendering, so that you can use simple web or mixed reality viewers for volume visualization?

---

<div class="post-metadata">

### Author: ![Hamid\_Fsian](https://discourse.vtk.org/user_avatar/discourse.vtk.org/hamid_fsian/32/6114_2.png) [@Hamid\_Fsian](https://discourse.vtk.org/u/Hamid_Fsian)
#### Post date: [February 16, 2023, 11:05am UTC](https://discourse.vtk.org/t/convert-nii-to-a-mesh/10677/6 "2023-02-16T11:05:28Z")

</div>

Hello,  
I did some investigations, and I know that my nifti volume have a range from 0 to 4 and each range is an index for colorification. I think it will be easier for me to color my mesh using thouse indexes.

Here is the code that I used, but it is displaying only white color during the visualization.

```auto
import vtk
import nibabel as nib
nifti_path = '/path/to/niftivolume'

reader = vtk.vtkNIFTIImageReader()
reader.SetFileName(nifti_path)
reader.Update()

image_data = reader.GetOutput()

# Load the Nifti file
img = nib.load('/path/to/niftivolume')
# Get the data array
data = img.get_fdata()

# Extract iso-surface mesh using vtkFlyingEdges3D
iso_value = 0.5
flying_edges = vtk.vtkFlyingEdges3D()
flying_edges.SetInputData(image_data)
flying_edges.SetValue(0, iso_value)
flying_edges.Update()

mesh = flying_edges.GetOutput()

# Create a color array to store the index values for each vertex
color_array = vtk.vtkUnsignedCharArray()
color_array.SetName("Colors")
color_array.SetNumberOfComponents(4)
color_array.SetNumberOfTuples(mesh.GetNumberOfPoints())

# Create a lookup table to map index values to colors
lookup_table = vtk.vtkLookupTable()
lookup_table.SetNumberOfTableValues(4)
lookup_table.SetTableValue(0, 1, 0, 0, 1) # Red for index 0
lookup_table.SetTableValue(1, 0, 1, 0, 1) # Green for index 2
lookup_table.SetTableValue(2, 0, 0, 1, 1) # Blue for index 3
lookup_table.SetTableValue(3, 1, 1, 0, 1) # Yellow for index 4
# Loop through the voxels in the Nifti volume and extract the index values
# Map the index values to colors using the lookup table
for i in range(image_data.GetDimensions()[0]):
    for j in range(image_data.GetDimensions()[1]):
        for k in range(image_data.GetDimensions()[2]):
            index_value = image_data.GetScalarComponentAsDouble(i, j, k, 0)
            # color = [0, 0, 0, 255]
            if index_value == 0.0 : 
                color = [255, 255, 255, 255]
            if index_value == 2.0 : 
                color = [255, 0, 0, 255]
            if index_value == 3.0 : 
                color = [0, 255, 0, 255]
            if index_value == 4.0 : 
                color = [0, 0, 255, 255]
            
            # lookup_table.GetColor(index_value, color)
            color_array.InsertTuple(image_data.ComputePointId((i, j, k)), color)

# Set the color array as the point data for the mesh
mesh.GetPointData().SetScalars(color_array)

```

I also double checked the indexes contained in my nifti volume :

```auto
import nibabel as nib
import numpy as np

# Load the Nifti file
img = nib.load('/path/to/nifti')

# Get the data array
data = img.get_fdata()

# Print the unique values in the data array
print(np.unique(data))

```

the output was :

```auto
[0. 2. 3. 4.]

```

Do you know what I got wrong ?  
Thanks

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [February 16, 2023, 7:46pm UTC](https://discourse.vtk.org/t/convert-nii-to-a-mesh/10677/7 "2023-02-16T19:46:06Z")

</div>

There a couple of potential issues:

1. You have only specified to extract a single isosurface, you need to extract 4
2. I’m not sure if [ComputeScalars](https://vtk.org/doc/nightly/html/classvtkFlyingEdges3D.html#a50a2cc967ef7c32643ca2e3a47a31645) is enabled by default
3. Consider using discrete flying edges for label image
