Using vtk, I overlaid a mesh on a CT slice, as shown in the image below. However, I noticed a red edge appear in the vtkImagePlaneWidget’s outer line. I tried to fix it by changing the colour to white, but it didn’t work.
Could someone please help in resolving this issue ?
Code:
import os
import nibabel
import vtk
import numpy as np
from vtk.util import numpy_support
import glob
def getMeshData(mesh_path):
meshReader = vtk.vtkUnstructuredGridReader()
meshReader.SetFileName(mesh_path)
meshReader.ReadAllVectorsOn()
meshReader.ReadAllScalarsOn()
meshReader.Update()
return meshReader.GetOutput()
def saveRefMeshSaggitalAlignment(nifti_image_file=None, ref_mesh_path=None, slice_loc=0):
reader = vtk.vtkNIFTIImageReader()
reader.SetFileName(nifti_image_file)
reader.Update()
imageMatrix = reader.GetSFormMatrix()
if imageMatrix is None:
imageMatrix = reader.GetQFormMatrix()
meshDataForRefMesh = getMeshData(ref_mesh_path)
gtMeshPoints = meshDataForRefMesh.GetPoints()
pointsToImage = vtk.vtkTransform()
pointsToImage.Concatenate(imageMatrix)
pointsToImage.Inverse()
# the newSourcePoints and newTargetPoints are in the same coordinate space as the vtkImageData,
# so they can be used to build the reslice transform.
newGTMeshPoints = vtk.vtkPoints()
pointsToImage.TransformPoints(gtMeshPoints, newGTMeshPoints)
calculator = vtk.vtkArrayCalculator()
meshDataForRefMesh.SetPoints(newGTMeshPoints)
vtkImage = reader.GetOutput()
#outline
outline=vtk.vtkOutlineFilter()
outline.SetInputData(vtkImage)
outlineMapper=vtk.vtkPolyDataMapper()
outlineMapper.SetInputConnection(outline.GetOutputPort())
outlineActor = vtk.vtkActor()
outlineActor.SetMapper(outlineMapper)
datasetMapper = vtk.vtkDataSetMapper()
datasetMapper.SetInputData(meshDataForRefMesh)
datasetActor = vtk.vtkActor()
datasetActor.SetMapper(datasetMapper)
#Picker
picker = vtk.vtkCellPicker()
picker.SetTolerance(0.005)
#PlaneWidget
planeWidgetX = vtk.vtkImagePlaneWidget()
planeWidgetX.DisplayTextOn()
planeWidgetX.SetInputData(vtkImage)
planeWidgetX.SetPlaneOrientationToXAxes()
planeWidgetX.SetSliceIndex(slice_loc)
planeWidgetX.SetPicker(picker)
planeWidgetX.SetKeyPressActivationValue("x")
prop2 = planeWidgetX.GetPlaneProperty()
prop2.SetColor(1, 1, 1)
#Renderer
renderer = vtk.vtkRenderer()
renderer.SetBackground(1, 1, 1)
#RenderWindow
renwin = vtk.vtkRenderWindow()
renwin.AddRenderer(renderer)
#Add outlineactor
renderer.AddActor(outlineActor)
renderer.AddActor(datasetActor)
renwin.SetSize(400,400)
# Generate an interesting view XCAT
renderer.ResetCamera()
renderer.GetActiveCamera().Azimuth(90)
renderer.GetActiveCamera().Roll(-90)
renderer.GetActiveCamera().Elevation(0)
renderer.GetActiveCamera().Dolly(2)
renderer.GetActiveCamera().Zoom(1)
renderer.ResetCameraClippingRange()
interactor = vtk.vtkRenderWindowInteractor()
interactor.SetRenderWindow(renwin)
#Load widget interactors and enable
planeWidgetX.SetInteractor(interactor)
planeWidgetX.On()
interactor.Initialize()
renwin.Render()
interactor.Start()
nifti_image_file='./sample_CT.nii.gz'
ref_mesh_path = './sample_mesh.vtk'
saveRefMeshSaggitalAlignment(nifti_image_file=nifti_image_file, ref_mesh_path=ref_mesh_path, slice_loc=120)