[vtkusers] Image blending using a lookup table
Anka Kochanowska
pluszcz at gmail.com
Wed Mar 3 11:44:39 EST 2010
Hi!
I have found somewhere (at Kitware site?) the following example -
TestThinPlateWarp:
#include "vtkImageGridSource.h"
#include "vtkLookupTable.h"
#include "vtkImageMapToColors.h"
#include "vtkBMPReader.h"
#include "vtkImageBlend.h"
#include "vtkPoints.h"
#include "vtkThinPlateSplineTransform.h"
#include "vtkImageReslice.h"
#include "vtkSmartPointer.h"
#include "vtkDataObject.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkInteractorStyleTerrain.h"
#include "vtkImageMapper.h"
#include "vtkActor2D.h"
// warp an image with a thin plate spline
int main(int argc, char** argv)
{
char filename[256];
const char *temp=getenv("VTK_DATA_ROOT");
if ( ! temp )
{
cerr << "ERROR: Please define environment variable VTK_DATA_ROOT\n";
return 0;
}
strcpy(filename,temp);
strcat(filename,"Data/masonry.bmp");
// first, create an image to warp
vtkSmartPointer< vtkImageGridSource > imageGrid = vtkSmartPointer<
vtkImageGridSource >::New();
imageGrid->SetGridSpacing( 16, 16, 0 );
imageGrid->SetGridOrigin( 0, 0, 0 );
imageGrid->SetDataExtent( 0, 255, 0, 255, 0, 0 );
imageGrid->SetDataScalarTypeToUnsignedChar();
vtkSmartPointer< vtkLookupTable > table = vtkSmartPointer<
vtkLookupTable >::New();
table->SetTableRange( 0, 1 );
table->SetAlphaRange( 0.0, 1.0 );
table->SetHueRange( 0.15, 0.15 );
table->SetSaturationRange( 1, 1 );
table->SetValueRange( 0, 1 );
table->Build();
vtkSmartPointer< vtkImageMapToColors > alpha = vtkSmartPointer<
vtkImageMapToColors >::New();
alpha->SetInputConnection( imageGrid->GetOutputPort() );
alpha->SetLookupTable( table );
vtkSmartPointer< vtkBMPReader > reader = vtkSmartPointer<
vtkBMPReader >::New();
reader->SetFileName(filename);
reader->Update();
vtkSmartPointer< vtkImageBlend > blend = vtkSmartPointer<
vtkImageBlend >::New();
blend->AddInput( 0, (vtkDataObject*)reader->GetOutput() );
blend->AddInput( 0, (vtkDataObject*)alpha->GetOutput() );
// next, create a ThinPlateSpline transform
vtkSmartPointer< vtkPoints > p1 = vtkSmartPointer< vtkPoints >::New();
p1->SetNumberOfPoints( 8 );
p1->SetPoint( 0, 0, 0, 0 );
p1->SetPoint( 1, 0, 255, 0);
p1->SetPoint( 2, 255, 0, 0 );
p1->SetPoint( 3, 255, 255, 0 );
p1->SetPoint( 4, 96, 96, 0 );
p1->SetPoint( 5, 96, 159, 0 );
p1->SetPoint( 6, 159, 159, 0 );
p1->SetPoint( 7, 159, 96, 0 );
vtkSmartPointer< vtkPoints > p2 = vtkSmartPointer< vtkPoints >::New();
p2->SetNumberOfPoints( 8 );
p2->SetPoint( 0, 0, 0, 0 );
p2->SetPoint( 1, 0, 255, 0 );
p2->SetPoint( 2, 255, 0, 0 );
p2->SetPoint( 3, 255, 255, 0);
p2->SetPoint( 4, 96, 159, 0 );
p2->SetPoint( 5, 159, 159, 0 );
p2->SetPoint( 6, 159, 96, 0 );
p2->SetPoint( 7, 96, 96, 0 );
vtkSmartPointer< vtkThinPlateSplineTransform > transform =
vtkSmartPointer< vtkThinPlateSplineTransform >::New();
transform->SetSourceLandmarks( p2 );
transform->SetTargetLandmarks( p1 );
transform->SetBasisToR2LogR();
// you must invert the transform before passing it to vtkImageReslice
transform->Inverse();
vtkSmartPointer< vtkImageReslice > reslice = vtkSmartPointer<
vtkImageReslice >::New();
reslice->SetInputConnection( blend->GetOutputPort() );
// reslice->SetResliceTransform( transform );
reslice->SetInterpolationModeToLinear();
vtkSmartPointer< vtkImageMapper > map = vtkSmartPointer<
vtkImageMapper >::New();
map->SetInput( reslice->GetOutput() );
map->SetColorWindow( 255.0);
map->SetColorLevel( 127.5 );
map->SetZSlice( 0 );
vtkSmartPointer< vtkActor2D > act = vtkSmartPointer< vtkActor2D >::New();
act->SetMapper( map );
act->SetPosition( 0.0, 0.0 );
vtkSmartPointer< vtkRenderer > renderer = vtkSmartPointer<
vtkRenderer >::New();
renderer->AddActor( act );
vtkSmartPointer< vtkRenderWindow > window = vtkSmartPointer<
vtkRenderWindow >::New();
window->SetSize( 640, 480 );
window->AddRenderer(renderer);
vtkSmartPointer< vtkRenderWindowInteractor > interactor =
vtkSmartPointer< vtkRenderWindowInteractor >::New();
interactor->SetRenderWindow(window);
vtkSmartPointer< vtkInteractorStyleTerrain > style =
vtkSmartPointer< vtkInteractorStyleTerrain >::New();
interactor->SetInteractorStyle( style );
interactor->Initialize();
interactor->Start();
return 0;
}
_____________________________
Anka
On Wed, Mar 3, 2010 at 11:08 AM, "Tim Hütz (VTK)" <vtk at huetz.biz> wrote:
> Hey David,
>
> of course I can do this. I'll will extract the corresponding code
> parts out of my project and will build a simple example for using
> the vtkImageMapToColors class. Should I submit this sample to this
> list (with all required files)?
>
> Best regards,
> Tim
>
> David Doria schrieb:
>> On Wed, Mar 3, 2010 at 10:52 AM, "Tim Hütz (VTK)" <vtk at huetz.biz
>> <mailto:vtk at huetz.biz>> wrote:
>>
>> Hey David,
>>
>> thank you for your fast reply. With the vtkImageMapToColors class,
>> it works
>> perfectly. Thanks a lot.
>>
>>
>> Best regards,
>> Tim
>>
>>
>> Tim,
>>
>> There is no example that uses vtkImageMapToColors. Would you be
>> interested in adding one? At the very least, could you send a small
>> sample code to the mailing list and I'll make one?
>>
>> Thanks,
>>
>> David
> _______________________________________________
> 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
>
More information about the vtkusers
mailing list