I am using vtkImageMapToColors
to map gray image to color image. My image has only 0/1 value:
img = vtk.vtkImageData()
img.SetDimensions(256, 256, 1)
img.AllocateScalars(vtk.VTK_INT, 1)
for i in range(256):
for j in range(256):
if i > 128 and i<192 and j>128 and j<192:
img.SetScalarComponentFromFloat(i, j, 0, 0, 1)
else:
img.SetScalarComponentFromFloat(i, j, 0, 0, 0)
If the color table is given as:
colorTable = vtk.vtkWindowLevelLookupTable()
colorTable.SetNumberOfTableValues(2)
colorTable.SetTableValue(0, 1, 1, 1, 1)
colorTable.SetTableValue(1, 1, 0, 0, 1)
colorTable.Build()
The shown result is what I expect:
But, if the color table is given as:
colorTable = vtk.vtkWindowLevelLookupTable()
colorTable.SetNumberOfTableValues(3)
colorTable.SetTableValue(0, 1, 1, 1, 1)
colorTable.SetTableValue(1, 1, 0, 0, 1)
colorTable.SetTableValue(2, 1, 1, 0, 1)
colorTable.Build()
The shown result is:
I hope the color part is red, because the value in image is 1, and colorTable.SetTableValue(1, 1, 0, 0, 1)
. I don’t know why the color part is yellow.
The complete code to reproduce my problem is:
import vtkmodules.all as vtk
img = vtk.vtkImageData()
img.SetDimensions(256, 256, 1)
img.AllocateScalars(vtk.VTK_INT, 1)
for i in range(256):
for j in range(256):
if i > 128 and i<192 and j>128 and j<192:
img.SetScalarComponentFromFloat(i, j, 0, 0, 1)
else:
img.SetScalarComponentFromFloat(i, j, 0, 0, 0)
colorTable = vtk.vtkWindowLevelLookupTable()
colorTable.SetNumberOfTableValues(3)
colorTable.SetTableValue(0, 1, 1, 1, 1)
colorTable.SetTableValue(1, 1, 0, 0, 1)
colorTable.SetTableValue(2, 1, 1, 0, 1)
colorTable.Build()
mapToColor = vtk.vtkImageMapToColors()
mapToColor.SetInputData(img)
mapToColor.SetLookupTable(colorTable)
imgMapper = vtk.vtkImageSliceMapper()
imgMapper.SetInputConnection(mapToColor.GetOutputPort())
imgSlice = vtk.vtkImageSlice()
imgSlice.SetMapper(imgMapper)
render = vtk.vtkRenderer()
render.AddActor(imgSlice)
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(render)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
iren.Initialize()
iren.Start()