Computer Vision News - December 2022
31 Visualizing Data with Mayavi # Import libraries from mayavi import mlab import SimpleITK as sitk import numpy as np import vtk from vtk.util import numpy_support # Reads the image using SimpleITK, and returns origin and spacing to plot the image in world coordinates def load_itk(path): itkimage = sitk.ReadImage(path) scan = sitk.GetArrayFromImage(itkimage) origin = np.array(list(reversed(itkimage.GetOrigin()))) spacing = np.array(list(reversed(itkimage.GetSpacing()))) return scan, origin, spacing # Define image and mesh paths image_path = "HealthP/image/cinefrm02.mhd" mesh_path = image_path.replace('image','mesh').replace('frm','mesh'). replace('.mhd','.vtk') # Read scan scan, origin, spacing = load_itk(image_path) # Create Mayavi figure mlab.figure() # Add pipeline to visualise the image oriented on the x-axis src = mlab.pipeline.scalar_field(scan) src.origin = origin src.spacing = spacing plane = mlab.pipeline.image_plane_widget(src,plane_orientation='x_axes', colormap='black-white') # Read mesh reader = vtk.vtkDataSetReader() reader.SetFileName(mesh_path) reader.ReadAllScalarsOn() reader.Update() header = reader.GetHeader() mesh=reader.GetOutput() # Read coordinates as x,y,z points point_coordinates = reader.GetOutput().GetPoints().GetData() numpy_coordinates = numpy_support.vtk_to_numpy(point_coordinates) x = numpy_coordinates[:,0] y = numpy_coordinates[:,1] z = numpy_coordinates[:,2] # Visualise 3D coordinates mlab.points3d(z,y,x, scale_factor=1) mlab.show()
Made with FlippingBook
RkJQdWJsaXNoZXIy NTc3NzU=