Hello , I am new to VTK. I have a problem with display 3d image data.
the color display is not my want.
I have a nifti image data with only four scala values: 0 ,1, 2, 3.
And I want 0 map to black, and 1 map to red, 2 map to green, 3 map to blue.
Could anyone help me?
# nifti image data with only four scala values: 0 ,1, 2, 3.
# I want 0 map to black, and 1 map to red, 2 map to green, 3 map to blue.
_file = "/xxx/work/IAS/debug_out/mvh_thy_nodule.nii" # u8 data, single channel
reader = vtk.vtkNIFTIImageReader()
reader.SetFileName(_file)
reader.Update()
image_data = reader.GetOutput()
# Create a lookup table to map scalar values to colors
lookup_table = vtk.vtkLookupTable()
lookup_table.SetNumberOfTableValues(4)
lookup_table.SetTableValue(0, 0, 0, 0, 1.0)
lookup_table.SetTableValue(1, 1, 0, 0, 1.0)
lookup_table.SetTableValue(2, 0, 1, 0, 1.0)
lookup_table.SetTableValue(3, 0, 0, 1, 1.0)
lookup_table.Build()
# Create a contour filter to create a surface from the image data
contour_filter = vtk.vtkDiscreteMarchingCubes()
contour_filter.SetInputData(image_data)
contour_filter.Update()
# Create a mapper for the contour filter output
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(contour_filter.GetOutputPort())
mapper.SetLookupTable(lookup_table)
mapper.SetColorModeToMapScalars()
mapper.ScalarVisibilityOn()
mapper.SetScalarRange(0, 3)
# Create an actor for the surface and set the mapper
actor = vtk.vtkActor()
actor.SetMapper(mapper)
# Create a renderer, add the actor to it, and set the background color
renderer = vtk.vtkRenderer()
renderer.AddActor(actor)
renderer.SetBackground(0.1, 0.2, 0.4)
# Create a render window and add the renderer to it
render_window = vtk.vtkRenderWindow()
render_window.AddRenderer(renderer)
# Create an interactor style and set it on the render window interactor
interactor_style = vtk.vtkInteractorStyleTrackballCamera()
interactor = vtk.vtkRenderWindowInteractor()
interactor.SetRenderWindow(render_window)
interactor.SetInteractorStyle(interactor_style)
# Render the scene and start the interactor
render_window.Render()
interactor.Start()