[vtkusers] Transparent values of vtkImageData
Alex Southern
mrapsouthern at gmail.com
Tue Jul 10 12:09:31 EDT 2012
Hi,
Below is the code that demonstrates how I currently make vtkImageData
that is not transparent, also attached is an image of the window showing
this for this example code.
Right now I dont see how to modify this to make the zero values
transparent. Any help would be greatly appreciated.
Thanks
Alex
#include <iostream>
#include <vtkSmartPointer.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkLookupTable.h>
#include <vtkFloatArray.h>
#include <vtkPointData.h>
#include <vtkImageMapToColors.h>
#include <vtkImageActor.h>
#include <vtkScalarBarActor.h>
#include <vtkImageData.h>
#include <vtkCubeSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
using namespace std;
vtkSmartPointer<vtkLookupTable> setupLookupTable(void)
{
vtkSmartPointer<vtkLookupTable> table =
vtkSmartPointer<vtkLookupTable>::New();
double r = 1.0; double g = 1.0; double b = 0.0;
for (int x = 0; x < 64 ; x++)
{
if (x > 32) { r = r + 0.0258; g = g - 0.0129; b = b -
0.0258; }
if (x == 32){ r = 0.2; g = 0.4; b = 0.8; }
if (x < 32) { r = r - 0.025; g = g - 0.0188; b = b
+ 0.025; }
table->SetTableValue(x,r,g,b,1.0);
}
table->SetTableValue(32,0.2,0.4,0.8,1.0); // make sure middle value
matches background
return table;
}
void generateArrayData( float *p, int L, int W )
{
for (int i = 0; i < L*W; i++ ) // Generate random numbers between
-1.0 and 1.0
{ p[i] = 2.0*(((double)(rand() / (RAND_MAX + 1.0)))-0.5); }
for (int x = 20; x < 50; x++ ) // Add some zeros (these will be
transparent)
{
for (int y = 40; y < 70; y++ )
{
p[x*W + y] = 0.0;
}
}
}
int main(int argc, char **argw)
{
int L = 100; int W = 100; int H = 50; int len = L*W;
float *myArray;
myArray = new float [len];
vtkSmartPointer<vtkCubeSource> cube =
vtkSmartPointer<vtkCubeSource>::New();
cube->SetXLength(L); cube->SetYLength(W); cube->SetZLength(H);
cube->SetCenter(L/2,W/2,-H);
vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInput(cube->GetOutput());
vtkSmartPointer<vtkActor> cubeActor = vtkSmartPointer<vtkActor>::New();
cubeActor->SetMapper( mapper );
cubeActor->GetProperty()->SetColor(0.0,1.0,0.0); // make green
vtkSmartPointer<vtkFloatArray> data =
vtkSmartPointer<vtkFloatArray>::New();
generateArrayData( myArray, L, W );
data->SetArray(myArray,L*W,1);
vtkSmartPointer<vtkImageData> imgData2DVw =
vtkSmartPointer<vtkImageData>::New();
imgData2DVw->SetOrigin(0,0,0);
imgData2DVw->SetNumberOfScalarComponents(1);
imgData2DVw->SetScalarTypeToFloat();
imgData2DVw->SetExtent(0,L,0,W,0,0);
imgData2DVw->SetSpacing(1.0,1.0,1.0);
imgData2DVw->AllocateScalars();
imgData2DVw->GetPointData()->SetScalars(data);
vtkSmartPointer<vtkLookupTable> table = setupLookupTable();
table->SetNumberOfTableValues(64);
table->Build();
table->SetScaleToLinear();
table->SetRange(-1.0,1.0);
vtkSmartPointer<vtkImageMapToColors> color =
vtkSmartPointer<vtkImageMapToColors>::New();
color->SetLookupTable(table);
color->SetInput(imgData2DVw);
vtkSmartPointer<vtkImageActor> imActor =
vtkSmartPointer<vtkImageActor>::New();
imActor->SetInput(color->GetOutput());
vtkSmartPointer<vtkScalarBarActor> colorBar =
vtkSmartPointer<vtkScalarBarActor>::New();
colorBar->SetLookupTable(table);
colorBar->GetPositionCoordinate()->SetCoordinateSystemToNormalizedViewport();
colorBar->GetPositionCoordinate()->SetValue(0.1, 0.01);
colorBar->SetOrientationToHorizontal();
colorBar->SetWidth(0.8);
colorBar->SetHeight(0.12);
vtkSmartPointer<vtkRenderer> ren1 =
vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renWin =
vtkSmartPointer<vtkRenderWindow>::New();
vtkSmartPointer<vtkRenderWindowInteractor> iren =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renWin->AddRenderer( ren1 );
iren->SetRenderWindow( renWin );
ren1->AddActor(colorBar); // Add Actors
ren1->AddActor(imActor);
ren1->AddActor(cubeActor);
ren1->SetBackground(0.2, 0.4, 0.8); // window background colour
renWin->Render();
iren->Start();
delete []myArray;
return 0;
}
On 7/10/2012 8:56 AM, Najzero wrote:
> Hi Alex,
>
> you might wanna have a look into vtkLookupTable
>
> We use it for example to have "black" values to be transparent.
>
> First build the lookuptable, then apply to the texture.
>
> <snippet we use>
> body->_volume.reset( new dicom::modDicomVolume );
> body->_lookupTable = vtkSmartPointer<vtkLookupTable>::New();
> body->_bmpPixelData = vtkSmartPointer<modImage>::New(); // Pixeldaten
> body->_bmpPolyData = vtkSmartPointer<vtkPolyData>::New(); //
> Polygondaten
> body->_texture = vtkSmartPointer<vtkTexture>::New(); // Textur
>
> //=== set lookup table so it shows black values transparent
> body->_lookupTable->SetNumberOfColors( 255 );
> body->_lookupTable->SetTableRange( 0.0, 256 * 256 - 1 );
> body->_lookupTable->SetHueRange( 0.0, 0.0 );
> body->_lookupTable->SetSaturationRange( 0.0, 0.0 );
> body->_lookupTable->SetAlphaRange( 0.0, 1.0 );
> body->_lookupTable->Build();
>
> body->_texture->SetInput( body->_bmpPixelData ); // Der Textur die
> Pixeldaten zuweisen
> body->_texture->MapColorScalarsThroughLookupTableOn();
> body->_texture->SetLookupTable( body->_lookupTable ); // Der Textur die
> Lookup-Table zuweisen
>
> body->_texture->InterpolateOn();
>
>
>
> KR Adrian
>
> --
> View this message in context: http://vtk.1045678.n5.nabble.com/Transparent-values-of-vtkImageData-tp5714562p5714583.html
> Sent from the VTK - Users mailing list archive at Nabble.com.
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtkusers
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20120710/a5e47441/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ghddgegb.png
Type: image/png
Size: 63337 bytes
Desc: not available
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20120710/a5e47441/attachment.png>
More information about the vtkusers
mailing list