[vtkusers] VTK RGB rendering Volume
abenchaaben
abenchaaben at histalim.com
Tue Apr 12 11:26:34 EDT 2016
Hello,
I have a 2D RGB serie that i want to render it with VTK. I created the
volume with the help of ITK and i have now a vti file.
I used an example of rendering that i found online:
double opacityWindow = 4096;
double opacityLevel = 2048;
int blendType = 6;
int clip = 0;
double reductionFactor = 1.0;
double frameRate = 10.0;
char *fileName = (char*)VolumePath.c_str();
bool independentComponents=true;
// Create the renderer, render window and interactor
vtkRenderer *renderer = vtkRenderer::New();
uinw->qvtkWidget->GetRenderWindow()->AddRenderer(renderer);
QVTKInteractor *iren=uinw->qvtkWidget->GetInteractor();
vtkInteractorStyle *s= static_cast<vtkInteractorStyle
*>(iren->GetInteractorStyle());
vtkTDxInteractorStyleCamera *t=
static_cast<vtkTDxInteractorStyleCamera *>(s->GetTDxStyle());
const double angleSensitivity=0.02;
const double translationSensitivity=0.001;
t->GetSettings()->SetAngleSensitivity(angleSensitivity);
t->GetSettings()->SetTranslationXSensitivity(translationSensitivity);
t->GetSettings()->SetTranslationYSensitivity(translationSensitivity);
t->GetSettings()->SetTranslationZSensitivity(translationSensitivity);
iren->SetRenderWindow(uinw->qvtkWidget->GetRenderWindow());
iren->SetDesiredUpdateRate(frameRate / (1+clip) );
iren->GetInteractorStyle()->SetDefaultRenderer(renderer);
// Read the data
vtkAlgorithm *reader3=0;
vtkImageData *input2=0;
vtkXMLImageDataReader *xmlReader = vtkXMLImageDataReader::New();
xmlReader->SetFileName(fileName);
xmlReader->Update();
input2=xmlReader->GetOutput();
reader3=xmlReader;
// Verify that we actually have a volume
int dim[3];
input2->GetDimensions(dim);
if ( dim[0] < 2 ||
dim[1] < 2 ||
dim[2] < 2 )
{
cout << "Error loading data!" << endl;
exit(EXIT_FAILURE);
}
vtkImageResample *resample = vtkImageResample::New();
if ( reductionFactor < 1.0 )
{
resample->SetInputConnection( reader3->GetOutputPort() );
resample->SetAxisMagnificationFactor(0, reductionFactor);
resample->SetAxisMagnificationFactor(1, reductionFactor);
resample->SetAxisMagnificationFactor(2, reductionFactor);
}
// Create our volume and mapper
vtkVolume *volume = vtkVolume::New();
vtkFixedPointVolumeRayCastMapper *mapper =
vtkFixedPointVolumeRayCastMapper::New();
if ( reductionFactor < 1.0 )
{
mapper->SetInputConnection( resample->GetOutputPort() );
}
else
{
mapper->SetInputConnection( reader3->GetOutputPort() );
}
// Set the sample distance on the ray to be 1/2 the average spacing
double spacing[3];
if ( reductionFactor < 1.0 )
{
resample->GetOutput()->GetSpacing(spacing);
}
else
{
input2->GetSpacing(spacing);
}
// mapper->SetSampleDistance(
(spacing[0]+spacing[1]+spacing[2])/6.0 );
// mapper->SetMaximumImageSampleDistance(10.0);
// Create our transfer function
vtkColorTransferFunction *colorFun =
vtkColorTransferFunction::New();
vtkPiecewiseFunction *opacityFun = vtkPiecewiseFunction::New();
// Create the property and attach the transfer functions
vtkVolumeProperty *property = vtkVolumeProperty::New();
property->SetIndependentComponents(independentComponents);
property->SetColor( colorFun );
property->SetScalarOpacity( opacityFun );
property->SetInterpolationTypeToLinear();
// connect up the volume to the property and the mapper
volume->SetProperty( property );
volume->SetMapper( mapper );
// Depending on the blend type selected as a command line option,
// adjust the transfer function
switch ( blendType )
{
// MIP
// Create an opacity ramp from the window and level values.
// Color is white. Blending is MIP.
case 0:
colorFun->AddRGBSegment(0.0, 1.0, 1.0, 1.0, 255.0, 1.0, 1.0, 1.0 );
opacityFun->AddSegment( opacityLevel - 0.5*opacityWindow, 0.0,
opacityLevel + 0.5*opacityWindow, 1.0 );
mapper->SetBlendModeToMaximumIntensity();
break;
// CompositeRamp
// Create a ramp from the window and level values. Use compositing
// without shading. Color is a ramp from black to white.
case 1:
colorFun->AddRGBSegment(opacityLevel - 0.5*opacityWindow, 0.0, 0.0,
0.0,
opacityLevel + 0.5*opacityWindow, 1.0, 1.0, 1.0 );
opacityFun->AddSegment( opacityLevel - 0.5*opacityWindow, 0.0,
opacityLevel + 0.5*opacityWindow, 1.0 );
mapper->SetBlendModeToComposite();
property->ShadeOff();
break;
// CompositeShadeRamp
// Create a ramp from the window and level values. Use compositing
// with shading. Color is white.
case 2:
colorFun->AddRGBSegment(0.0, 1.0, 1.0, 1.0, 255.0, 1.0, 1.0, 1.0 );
opacityFun->AddSegment( opacityLevel - 0.5*opacityWindow, 0.0,
opacityLevel + 0.5*opacityWindow, 1.0 );
mapper->SetBlendModeToComposite();
property->ShadeOn();
break;
// CT_Skin
// Use compositing and functions set to highlight skin in CT data
// Not for use on RGB data
case 3:
colorFun->AddRGBPoint( -3024, 0, 0, 0, 0.5, 0.0 );
colorFun->AddRGBPoint( -1000, .62, .36, .18, 0.5, 0.0 );
colorFun->AddRGBPoint( -500, .88, .60, .29, 0.33, 0.45 );
colorFun->AddRGBPoint( 3071, .83, .66, 1, 0.5, 0.0 );
opacityFun->AddPoint(-3024, 0, 0.5, 0.0 );
opacityFun->AddPoint(-1000, 0, 0.5, 0.0 );
opacityFun->AddPoint(-500, 1.0, 0.33, 0.45 );
opacityFun->AddPoint(3071, 1.0, 0.5, 0.0);
mapper->SetBlendModeToComposite();
property->ShadeOn();
property->SetAmbient(0.1);
property->SetDiffuse(0.9);
property->SetSpecular(0.2);
property->SetSpecularPower(10.0);
property->SetScalarOpacityUnitDistance(0.8919);
break;
// CT_Bone
// Use compositing and functions set to highlight bone in CT data
// Not for use on RGB data
case 4:
colorFun->AddRGBPoint( -3024, 0, 0, 0, 0.5, 0.0 );
colorFun->AddRGBPoint( -16, 0.73, 0.25, 0.30, 0.49, .61 );
colorFun->AddRGBPoint( 641, .90, .82, .56, .5, 0.0 );
colorFun->AddRGBPoint( 3071, 1, 1, 1, .5, 0.0 );
opacityFun->AddPoint(-3024, 0, 0.5, 0.0 );
opacityFun->AddPoint(-16, 0, .49, .61 );
opacityFun->AddPoint(641, .72, .5, 0.0 );
opacityFun->AddPoint(3071, .71, 0.5, 0.0);
mapper->SetBlendModeToComposite();
property->ShadeOn();
property->SetAmbient(0.1);
property->SetDiffuse(0.9);
property->SetSpecular(0.2);
property->SetSpecularPower(10.0);
property->SetScalarOpacityUnitDistance(0.8919);
break;
// CT_Muscle
// Use compositing and functions set to highlight muscle in CT data
// Not for use on RGB data
case 5:
colorFun->AddRGBPoint( -3024, 0, 0, 0, 0.5, 0.0 );
colorFun->AddRGBPoint( -155, .55, .25, .15, 0.5, .92 );
colorFun->AddRGBPoint( 217, .88, .60, .29, 0.33, 0.45 );
colorFun->AddRGBPoint( 420, 1, .94, .95, 0.5, 0.0 );
colorFun->AddRGBPoint( 3071, .83, .66, 1, 0.5, 0.0 );
opacityFun->AddPoint(-3024, 0, 0.5, 0.0 );
opacityFun->AddPoint(-155, 0, 0.5, 0.92 );
opacityFun->AddPoint(217, .68, 0.33, 0.45 );
opacityFun->AddPoint(420,.83, 0.5, 0.0);
opacityFun->AddPoint(3071, .80, 0.5, 0.0);
mapper->SetBlendModeToComposite();
property->ShadeOn();
property->SetAmbient(0.1);
property->SetDiffuse(0.9);
property->SetSpecular(0.2);
property->SetSpecularPower(10.0);
property->SetScalarOpacityUnitDistance(0.8919);
break;
// RGB_Composite
// Use compositing and functions set to highlight red/green/blue
regions
// in RGB data. Not for use on single component data
case 6:
opacityFun->AddPoint(0, 0.0);
opacityFun->AddPoint(5.0, 0.0);
opacityFun->AddPoint(30.0, 0.05);
opacityFun->AddPoint(31.0, 0.0);
opacityFun->AddPoint(90.0, 0.0);
opacityFun->AddPoint(100.0, 0.3);
opacityFun->AddPoint(110.0, 0.0);
opacityFun->AddPoint(190.0, 0.0);
opacityFun->AddPoint(200.0, 0.4);
opacityFun->AddPoint(210.0, 0.0);
opacityFun->AddPoint(245.0, 0.0);
opacityFun->AddPoint(255.0, 0.5);
mapper->SetBlendModeToComposite();
property->ShadeOff();
property->SetScalarOpacityUnitDistance(1.0);
break;
default:
vtkGenericWarningMacro("Unknown blend type.");
break;
}
// Set the default window size
uinw->qvtkWidget->GetRenderWindow()->Render();
// Add the volume to the scene
renderer->AddVolume( volume );
renderer->ResetCamera();
// interact with data
uinw->qvtkWidget->GetRenderWindow()->Render();
opacityFun->Delete();
colorFun->Delete();
property->Delete();
volume->Delete();
mapper->Delete();
reader3->Delete();
resample->Delete();
renderer->Delete();
The problem is that nwith this example it give me the volume with color
transformation and i want to display the volume with the true RGB color from
the images serie that i use when i start.
Does VTK support such operation??
--
View this message in context: http://vtk.1045678.n5.nabble.com/VTK-RGB-rendering-Volume-tp5737641.html
Sent from the VTK - Users mailing list archive at Nabble.com.
More information about the vtkusers
mailing list