python3dvtkimagingmedical-imaging

vtkplotter - How to set a colormap


I'm working with labeled DICOMs, and I'm using vtkplotter to visualize the 3D model. Each voxel has a label that identifies the recognized part of bone, ranging from -1024 to a value that varies according to the DICOM scan. How can I assign a specific color to each scalar value? I tried with color(), but from what I understand I can only insert in an orderly way the colors corresponding to the value starting from the minimum. I have to specify for a certain scalar value x the color y.

Code snippet that I use to show the 3D model:

from vtkplotter import *

volume = load('Letter:/dicom_folder')
volume.color(['white', 'red', 'yellow'])
volume.alpha([0.0, 0.5, 0.5])

volume.show()

EDIT:

I solved it like this:

from vtkmodules.vtkCommonDataModel import vtkPiecewiseFunction
from vtkmodules.vtkRenderingCore import vtkColorTransferFunction, vtkVolumeProperty
from vtkplotter import *

volume = load('Letter:/dicom_folder')

volumeColor = vtkColorTransferFunction()
r, g, b = getColor('white')
volumeColor.AddRGBPoint(-1024, r, g, b)
r, g, b = getColor('yellow')
volumeColor.AddRGBPoint(-1023, r, g, b)
r, g, b = getColor('blue')
volumeColor.AddRGBPoint(-1022, r, g, b)


volumeScalarOpacity = vtkPiecewiseFunction()
volumeScalarOpacity.AddPoint(-1024, 0.0)
volumeScalarOpacity.AddPoint(-1023, 0.5)
volumeScalarOpacity.AddPoint(-1022, 0.5)


volumeProperty = vtkVolumeProperty()
volumeProperty.SetColor(volumeColor)
volumeProperty.SetScalarOpacity(volumeScalarOpacity)
volumeProperty.SetInterpolationTypeToLinear()
volumeProperty.ShadeOn()
volumeProperty.SetAmbient(0.4)
volumeProperty.SetDiffuse(0.6)
volumeProperty.SetSpecular(0.2)

volume.SetProperty(volumeProperty)

volume.show()

If you have better or more efficient solutions please let me know.


Solution

  • from vtkmodules.vtkCommonDataModel import vtkPiecewiseFunction
    from vtkmodules.vtkRenderingCore import vtkColorTransferFunction, vtkVolumeProperty
    from vtkplotter import *
    
    volume = load('Letter:/dicom_folder')
    
    volumeColor = vtkColorTransferFunction()
    r, g, b = getColor('white')
    volumeColor.AddRGBPoint(-1024, r, g, b)
    r, g, b = getColor('yellow')
    volumeColor.AddRGBPoint(-1023, r, g, b)
    r, g, b = getColor('blue')
    volumeColor.AddRGBPoint(-1022, r, g, b)
    
    
    volumeScalarOpacity = vtkPiecewiseFunction()
    volumeScalarOpacity.AddPoint(-1024, 0.0)
    volumeScalarOpacity.AddPoint(-1023, 0.5)
    volumeScalarOpacity.AddPoint(-1022, 0.5)
    
    
    volumeProperty = vtkVolumeProperty()
    volumeProperty.SetColor(volumeColor)
    volumeProperty.SetScalarOpacity(volumeScalarOpacity)
    volumeProperty.SetInterpolationTypeToLinear()
    volumeProperty.ShadeOn()
    volumeProperty.SetAmbient(0.4)
    volumeProperty.SetDiffuse(0.6)
    volumeProperty.SetSpecular(0.2)
    
    volume.SetProperty(volumeProperty)
    
    volume.show()