[vtkusers] mapping floating image to color image using vtkImageMapToColors
Richard Frank
rickfrank at me.com
Sat Jul 20 15:23:04 EDT 2013
Hi Bill,
The code below will illustrate the issue.
If you enable the color mapping code, by setting the #if 0 to #if 1, you will find that the color is a washed out white.
My desire is to have a ramp on a single hue, based on gray levels.
My data is typically from 0 - 0.69 which is what I set it to. The actual range is 0.0 - 1.0. but it pretty much never gets above
0.69.
I think the problem is perhaps not understanding the setup of the color table,,,but I'm unclear on how I should set it up other than
what I have.
The callback was used to interactively change the color table, but it wasn't helpful.
This is on Windows 7, using Visual Studio, and VTK 5.10.1
Thanks
Rick
----------------CODE---------------------------------------------------
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkFloatArray.h>
#include <vtkImageMapper.h>
#include <vtkImageData.h>
#include <vtkPointData.h>
#include <vtkActor2D.h>
#include <vtkImageMapToColors.h>
#include <vtkLookupTable.h>
#include <vtkMinimalStandardRandomSequence.h>
#include <vtkInteractorStyle.h>
#include "vtkInteractorStyleImage.h"
#include <vtkCommand.h>
#include <vtkSmartPointer.h>
class vtkImageInteractionCallback : public vtkCommand
{
public:
static vtkImageInteractionCallback *New()
{
return new vtkImageInteractionCallback;
};
vtkImageInteractionCallback()
{
this->Interactor = 0;
};
void SetInteractor(vtkRenderWindowInteractor *interactor)
{
this->Interactor = interactor;
};
vtkRenderWindowInteractor *GetInteractor()
{
return this->Interactor;
};
void SetColorTable(vtkLookupTable* colorTable)
{
ColorTable = colorTable;
}
virtual void Execute(vtkObject *, unsigned long event, void *)
{
vtkRenderWindowInteractor *interactor = this->GetInteractor();
int currPos[2];
interactor->GetEventPosition(currPos);
if (event == vtkCommand::LeftButtonPressEvent)
{
clickPos[0] = currPos[0];
clickPos[1] = currPos[1];
}
else if (event == vtkCommand::LeftButtonReleaseEvent)
{
}
else if (event == vtkCommand::MouseWheelForwardEvent)
{
}
else if (event == vtkCommand::MouseWheelBackwardEvent)
{
}
else if (event == vtkCommand::MouseMoveEvent)
{
}
else
{
vtkInteractorStyle *style = vtkInteractorStyle::SafeDownCast(
interactor->GetInteractorStyle());
if (style)
{
style->OnMouseMove();
}
}
}
vtkRenderWindowInteractor *Interactor;
vtkLookupTable * ColorTable;
int clickPos[2];
};
int main (int argc, char *argv[])
{
vtkSmartPointer<vtkRenderer> aRenderer =
vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renWin =
vtkSmartPointer<vtkRenderWindow>::New();
renWin->AddRenderer(aRenderer);
vtkSmartPointer<vtkRenderWindowInteractor> iren =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
iren->SetRenderWindow(renWin);
vtkSmartPointer<vtkFloatArray> floatData = vtkSmartPointer<vtkFloatArray>::New();
vtkIdType width = 500;
vtkIdType height = 500;
vtkIdType nt = width * height;
floatData->SetNumberOfTuples(nt);
vtkSmartPointer<vtkMinimalStandardRandomSequence> rg = vtkSmartPointer<vtkMinimalStandardRandomSequence>::New();
for(vtkIdType i = 0; i < nt; i++)
{
float rn = rg->GetRangeValue(0,0.69);
rg->Next();
floatData->SetTuple(i,&rn);
}
vtkSmartPointer<vtkImageData> image = vtkSmartPointer<vtkImageData>::New();
image->SetExtent(0,width-1,0,height-1,0,0);
image->SetNumberOfScalarComponents(1);
image->SetScalarTypeToFloat();
image->SetSpacing(0.1,0.1,1.0);
image->GetPointData()->SetScalars(floatData);
vtkSmartPointer<vtkImageMapper> mapper = vtkSmartPointer<vtkImageMapper>::New();
mapper->SetInputConnection(image->GetProducerPort());
mapper->SetColorWindow(1.0);
mapper->SetColorLevel(0.5);
vtkSmartPointer<vtkActor2D> actor = vtkSmartPointer<vtkActor2D>::New();
actor->SetPosition(0,0);
actor->SetMapper(mapper);
aRenderer->AddActor(actor);
// turn this on to map to colors.
#if 0
vtkSmartPointer<vtkImageInteractionCallback> callback = vtkSmartPointer<vtkImageInteractionCallback>::New();
vtkSmartPointer<vtkInteractorStyleImage> imageStyle = vtkSmartPointer<vtkInteractorStyleImage>::New();
iren->SetInteractorStyle(imageStyle);
callback->SetInteractor(iren);
renWin->SetInteractor(iren);
imageStyle->AddObserver(vtkCommand::MouseMoveEvent, callback);
imageStyle->AddObserver(vtkCommand::LeftButtonPressEvent, callback);
imageStyle->AddObserver(vtkCommand::LeftButtonReleaseEvent, callback);
imageStyle->AddObserver(vtkCommand::MouseWheelForwardEvent, callback);
imageStyle->AddObserver(vtkCommand::MouseWheelBackwardEvent, callback);
vtkSmartPointer<vtkImageMapToColors> filter = vtkSmartPointer<vtkImageMapToColors>::New();
vtkSmartPointer<vtkLookupTable> colorTable = vtkSmartPointer<vtkLookupTable>::New();
callback->SetColorTable(colorTable);
filter->SetOutputFormatToRGBA();
colorTable->SetTableRange(0,1);
colorTable->SetHueRange(0.3,0.3);
colorTable->SetSaturationRange(0,1);
colorTable->SetValueRange(1,1);
colorTable->SetRampToLinear();
colorTable->ForceBuild();
filter->SetLookupTable(colorTable);
filter->SetNumberOfThreads(1);
filter->SetInputConnection(image->GetProducerPort());
mapper->SetInputConnection(filter->GetOutputPort());
#endif
aRenderer->SetBackground(.2, .3, .4);
renWin->SetSize(640, 480);
// Initialize the event loop and then start it.
iren->Initialize();
iren->Start();
return EXIT_SUCCESS;
}
On Jul 19, 2013, at 09:32 PM, Bill Lorensen <bill.lorensen at gmail.com> wrote:
Can you post a small, compilable example that illustrates the problem?
On Fri, Jul 19, 2013 at 9:20 PM, Rick Frank <rickfrank at me.com> wrote:
Hi,
I'm getting some behavior I don't understand when trying to map gray floating point image to a color image
What I'm seeing is that the output is completely white.
my code is roughly as follows
filter->SetOutputFormatToRGBA()
colorTable->SetTableRange(0,1)
colorTable->SetHueRange(0,1);
colorTable->SetSaturationRange(0,1);
colorTable->SetValueRange(0,1);
colorTable->SetRampToLinear();
colorTable->ForceBuild();
Now, when I trace through the code, the values set in the RGBA image look correct. But the ultimate rendered image is completely white.
If I modify the code so that all the RGBA values are 1,0,0,255 the image is solid red. If I change the alpha to 64 nothing changes.
So, I suspect it's the mapper - I use the same mapper - vtkImageMapper to display the color image by resetting the input connection:
mapper->SetInputConnection(filter->GetOutputPort());
imageActor->SetMapper(mapper);
Do I need to make a change to the mapper to get the proper RGBA display?
Thanks
Rick
_______________________________________________
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
--
Unpaid intern in BillsBasement at noware dot com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20130720/c6f05073/attachment.htm>
More information about the vtkusers
mailing list