I have code that utilizes vtp and nibabel to convert read nifti file into vtp then I check how the geometry was made for vasculature through GeometryViewer of vtp examples.
import nibabel as nib
import vtk
from vtkmodules.util import numpy_support
import numpy as np
def nifti_to_vtp(nifti_file, vtp_file, contour_value, time_index=0):
img = nib.load(nifti_file)
array = img.get_fdata()
# Check if the array has four dimensions
if array.ndim == 4:
array = array[..., time_index]
spacing = img.header.get_zooms()[:3]
origin = img.affine[:3, 3]
print(array.shape)
# Create vtkImageData from NumPy array
vtk_image_data = vtk.vtkImageData()
vtk_image_data.SetDimensions(array.shape)
vtk_image_data.SetSpacing(spacing)
vtk_image_data.SetOrigin(origin)
vtk_array = numpy_support.numpy_to_vtk(np.ravel(array, order='F'), deep=True, array_type=vtk.VTK_FLOAT)
vtk_image_data.GetPointData().SetScalars(vtk_array)
# Use vtkFlyingEdges3D to extract iso-surface
flying_edges = vtk.vtkFlyingEdges3D()
flying_edges.SetInputData(vtk_image_data)
flying_edges.SetValue(0, contour_value)
flying_edges.Update()
poly_data = flying_edges.GetOutput()
# Write the vtkPolyData to a VTP file
writer = vtk.vtkXMLPolyDataWriter()
writer.SetFileName(vtp_file)
writer.SetInputData(poly_data)
writer.Write()
# Example usage
nifti_to_vtp(
nifti_file="example.nii.gz",
vtp_file="output.vtp",
contour_value=150,
time_index=0
)
The code above is what I’m using now the problem is that for nifti image with four dimensions array.shape = (256, 256, 64, 1) It gives me vasculature where vessels get stretched from the highest slice to the lowest slice of the brain, while another nifti image with (512, 512, 100) dimensions results in a proper vasculature