Apply presets to certain tissue segment of a volume rendering

Hi, I am working on writing a visualization function that applies a different color to a 3D rendering of a volume to only an area masked with tissue segment.

I am using split volume function to split segments into scalar volume nodes. The idea I am using so far is to use one of the split volume, apply a different preset to that split volume and use the color and component from split volume to set color on original volume scan. In example of screenshot below, I have the original volume and bone tissue split volume from the original scan with a different preset and I want to apply this preset to only bone covered areas of original volume.

Below is the code I have written so far and it is crashing as it calls the “setColor” to set different color transfer function to bone segment of original volume components. I wanted to ask if this is right way to do it or if there is any other efficient method to do something like this.

// -----------------------------------------------------------------------------------
void qSegmenterAppModuleWidget::testFunction() {
    Q_D(qTestApp);

    //volume nodes
    vtkMRMLScalarVolumeNode* vnodeOriginal = vtkMRMLScalarVolumeNode::SafeDownCast(qSlicerApplication::application()->mrmlScene()->GetNodeByID("vtkMRMLScalarVolumeNode1"));
    vtkMRMLScalarVolumeNode* vnodeSplit = vtkMRMLScalarVolumeNode::SafeDownCast(qSlicerApplication::application()->mrmlScene()->GetNodeByID("vtkMRMLScalarVolumeNode2"));

    vtkSlicerVolumeRenderingLogic* rlo = vtkSlicerVolumeRenderingLogic::SafeDownCast(qSlicerApplication::application()->moduleLogic("VolumeRendering"));
    
    //volume rendering display nodes
    vtkMRMLVolumeRenderingDisplayNode* displayNode3DSplit = rlo->GetFirstVolumeRenderingDisplayNode(vnodeSplit);
    vtkMRMLVolumeRenderingDisplayNode* displayNode3DOriginal = rlo->GetFirstVolumeRenderingDisplayNode(vnodeOriginal);

    //volume property nodes
    vtkMRMLVolumePropertyNode* volPropertyNodeSplit = displayNode3DSplit->GetVolumePropertyNode();
    vtkMRMLVolumePropertyNode* volPropertyNodeOriginal = displayNode3DOriginal->GetVolumePropertyNode();

    vtkDataArray* volArraySplit = vnodeSplit->GetImageData()->GetPointData()->GetScalars();

    for (int i = 0; i < volArraySplit->GetNumberOfTuples(); i++)
    {
        if (volArraySplit->GetComponent(i, 0) > 0)
        {
            vtkColorTransferFunction* function = volPropertyNodeSplit->GetColor(volArraySplit->GetComponent(i, 0));
            volPropertyNodeOriginal->SetColor(function, i);
        }
    }
}

You can have a look at the ColorizeVolume module in the Sandbox extension. You can completely control the volume rendering using segments.

1 Like

Thank you @pieper for the quick response, I will take a look at Colorize Volume module.