[vtkusers] Adding a transparency mask to an image
John Drescher
drescherjm at gmail.com
Thu Sep 23 10:27:00 EDT 2010
> Is there a way to set a transparency mask for a vtkImageData displayed
> with a vtkImageActor? For example, I want to make a specific region
> (in the example below, the top half) of the image transparent. This
> would be used to overlay the result of a binary segmentation onto the
> original image.
>
For a Lung Segmentation application I created a vtkLookupTable for the
mask image and set different opacities for different values in the
mask. In my case I made 0 totally transparent. Then I added the mask
image to the scene using a second image actor.
Here are bits of the code:
void someFunction()
{
vtkImageData* pMaskImage = InitializeMaskImage(pImage);
vtkLookupTable* lut = vtkLookupTable::New();
lut->SetNumberOfTableValues(7);
lut->SetRange(0.0,6.0);
lut->SetTableValue( 0, 0.0, 0.0, 0.0, 0.0 ); //label 0 is transparent
lut->SetTableValue( 1, 0.0, 1.0, 0.0, 0.8 ); //label 1 is green
lut->SetTableValue( 2, 1.0, 0.0, 0.0, 0.3 ); //label 2 is red
lut->SetTableValue( 3, 0.0, 0.0, 1.0, 0.3 ); //label 3 is blue
lut->SetTableValue( 4, 0.0, 1.0, 1.0, 0.3 ); //label 4 is cyan
lut->SetTableValue( 5, 1.0, 1.0, 0.0, 0.3 ); //label 5
lut->SetTableValue( 6, 1.0, 0.0, 1.0, 0.3 ); //label 6
lut->Build();
pImage->Update();
for(int i=0; i < 3;++i) {
#ifndef LIB_SETS_OFFSET
pViews[i]->SetFGImage(pMaskImage,lut);
#else
pViews[i]->SetFGImage(pMaskImage,lut,MASK_COL_OFFS,MASK_ROW_OFFS,MASK_SLICE_OFFS);
#endif //ndef LIB_SETS_OFFSET
}
}
void vtkViewImage2D::SetFGImage( vtkImageData* image, vtkLookupTable* lut,
int xoffs/*=0*/,int yoffs/*=0*/,int zoffs/*=0*/ )
{
if (FGImageActor != NULL) {
this->GetRenderer()->RemoveViewProp(FGImageActor);
}
vtkImageMapToColors* windowLevel = vtkImageMapToColors::New();
windowLevel->SetLookupTable(lut);
windowLevel->SetInput(image);
windowLevel->PassAlphaToOutputOn();
// set up the vtk pipeline
vtkImageReslice* relsice = vtkImageReslice::New();
relsice->SetOutputDimensionality(2);
relsice->InterpolateOff();
relsice->SetInput(windowLevel->GetOutput());
vtkImageActor* actor = vtkImageActor::New();
actor->SetInput(relsice->GetOutput());
FGImageActor = actor;
FGWindowLevel = windowLevel;
FGImageReslice = relsice;
this->FGImageActor->SetOpacity(0.95);
Update();
SetupAnnotations();
FGImage = image;
this->GetRenderer()->AddViewProp(this->FGImageActor);
this->GetRenderer()->Render();
}
John
More information about the vtkusers
mailing list