Segmentation Node AddSegmentFromClosedSurfaceRepresentation's color only effective on 2d viewer

Slicer version:4.8.1
Platform: Windows 10
Pic: image

Code:

import SampleData

sampleDataLogic = SampleData.SampleDataLogic()
masterVolumeNode = sampleDataLogic.downloadCTChest()

segmentationNode = slicer.vtkMRMLSegmentationNode()
slicer.mrmlScene.AddNode(segmentationNode)
segmentationNode.CreateDefaultDisplayNodes() # only needed for display
segmentationNode.SetReferenceImageGeometryParameterFromVolumeNode(masterVolumeNode)
masterVolumeNode.UpdateDisplayNodeImageData()

marchingCubes = vtk.vtkMarchingCubes()
marchingCubes.SetInputData(masterVolumeNode.GetImageData())
marchingCubes.SetValue(0, 1500)
marchingCubes.Update()

ijk2rasmat = vtk.vtkMatrix4x4()
masterVolumeNode.GetIJKToRASMatrix(ijk2rasmat)

transform = vtk.vtkTransform()
transform.Concatenate(ijk2rasmat)

applyTransform = vtk.vtkTransformPolyDataFilter()
applyTransform.SetTransform(transform)

applyTransform.SetInputData(marchingCubes.GetOutput())
applyTransform.Update()

segmentName=“segment”
segmentationNode.AddSegmentFromClosedSurfaceRepresentation(applyTransform.GetOutput(), segmentName, [0.0,1.0,0.0])

Any suggestions are greatly appreciated!

YingLi

Marching cubes created a scalar and set it active, which overwrote the color set by segmentation.

We’ll change the segmentation display in to ignore any scalar values that may be present in the input mesh. For now, you have to remove the scalar values from the mesh if you want to see correct color:

import SampleData
masterVolumeNode = SampleData.SampleDataLogic().downloadCTChest()

segmentationNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentationNode")
segmentationNode.CreateDefaultDisplayNodes()
segmentationNode.SetReferenceImageGeometryParameterFromVolumeNode(masterVolumeNode)

marchingCubes = vtk.vtkMarchingCubes()
marchingCubes.SetInputData(masterVolumeNode.GetImageData())
marchingCubes.SetValue(0, 1000)
marchingCubes.Update()
ijk2rasmat = vtk.vtkMatrix4x4()
masterVolumeNode.GetIJKToRASMatrix(ijk2rasmat)
transform = vtk.vtkTransform()
transform.SetMatrix(ijk2rasmat)
applyTransform = vtk.vtkTransformPolyDataFilter()
applyTransform.SetTransform(transform)
applyTransform.SetInputData(marchingCubes.GetOutput())
applyTransform.Update()
surface = applyTransform.GetOutput()

# Remove point arrays to make sure they don't affect display color
while surface.GetPointData().GetNumberOfArrays()>0:
  surface.GetPointData().RemoveArray(0)

segmentationNode.AddSegmentFromClosedSurfaceRepresentation(surface, "segment", [0.0,1.0,0.0])

Got it. Thanks Andras!