Search code examples
pythonvtkmedical-imaging

How to create mesh surface from segmentation MRI file using Python and VTK


I have a binary mask of 3D medical images in nifti format, "mask.nii.gz" which I would like to extract surface mesh from.

I can load binary mask data into a numpy.ndarray as following

import numpy as np
import nibabel as nib
filePath = "mask.nii.gz"
image = nib.load(filePath)
image_data = image.get_data()

but not sure how to render the surface with vtkDiscreteMarchingCubes() using the image_data above and output the vertices from rendered surface.

Could someone shed a light on this issue? Sorry I am very new to VTK library here. Many Thanks in advance.


Solution

  • You can create mesh with skimage.measure, using marching_cubes In your case, try transform nib.image to numpy ndarray

    image_ndarray = np.array(imgage.dataobj).astype(np.float64)
    verts, faces, norm, val = measure.marching_cubes_lewiner(image, threshold, step_size=step_size, allow_degenerate=True)