[vtkusers] Convert grayscale image to rgb

David Cole dlrdave at aol.com
Tue Jun 3 08:19:15 EDT 2014


> These examples do work on my machine, but they start with already
> valid rgb images and do a bit of LUT modifying. No conversions
> from grayscale or use of vtkScalarsToColors().


Yes, but both examples use vtkImageMapToColors, which is the filter you
are asking for: it is a filter which converts a scalar-based
vtkImageData into an RGB vtkImageData via a lookup table which maps the
scalar values to color values. In your case, I'm assuming you have a
vtkImageData with a single scalar component which you are calling "gray
value".

Following is a more concise example, which does exactly what I think
you are asking for. I will attempt to get it added properly to the VTK
wiki examples as well.


Hope it helps,
David C.


// ImageMapToColors example
//
// Displays a "grayscale" image as a full color image via the
// vtkImageMapToColors filter, which uses a lookup table to
// map scalar values to colors
//
#include <vtkImageActor.h>
#include <vtkImageData.h>
#include <vtkImageMapper3D.h>
#include <vtkImageMapToColors.h>
#include <vtkInteractorStyleImage.h>
#include <vtkLookupTable.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSmartPointer.h>
#include <vtkVersion.h>

int main(int argc, char* argv[])
{
  // Create a "grayscale" 16x16 image, 1-component pixels of type
  // "double"
  vtkSmartPointer<vtkImageData> image =
    vtkSmartPointer<vtkImageData>::New();
  int imageExtent[6] = { 0, 15, 0, 15, 0, 0 };
  image->SetExtent(imageExtent);
#if VTK_MAJOR_VERSION <= 5
  image->SetNumberOfScalarComponents(1);
  image->SetScalarTypeToDouble();
#else
  image->AllocateScalars(VTK_DOUBLE, 1);
#endif

  double scalarvalue = 0.0;

  for (int y = imageExtent[2]; y <= imageExtent[3]; y++)
    {
    for (int x = imageExtent[0]; x <= imageExtent[1]; x++)
      {
       double* pixel = static_cast<double*>(image->GetScalarPointer(x,
y, 0));
      pixel[0] = scalarvalue;
      scalarvalue += 1.0;
      }
    }

  // Map the scalar values in the image to colors with a lookup table:
  vtkSmartPointer<vtkLookupTable> lookupTable =
    vtkSmartPointer<vtkLookupTable>::New();
  lookupTable->SetNumberOfTableValues(256);
  lookupTable->SetRange(0.0, 255.0);
  lookupTable->Build();

  // Pass the original image and the lookup table to a filter to create
  // a color image:
  vtkSmartPointer<vtkImageMapToColors> scalarValuesToColors =
    vtkSmartPointer<vtkImageMapToColors>::New();
  scalarValuesToColors->SetLookupTable(lookupTable);
  scalarValuesToColors->PassAlphaToOutputOn();
#if VTK_MAJOR_VERSION <= 5
  scalarValuesToColors->SetInput(image);
#else
  scalarValuesToColors->SetInputData(image);
#endif

  // Create an image actor
  vtkSmartPointer<vtkImageActor> imageActor =
    vtkSmartPointer<vtkImageActor>::New();
  imageActor->GetMapper()->SetInputConnection(
    scalarValuesToColors->GetOutputPort());

  // Visualize
  vtkSmartPointer<vtkRenderer> renderer =
    vtkSmartPointer<vtkRenderer>::New();
  renderer->AddActor(imageActor);
  renderer->ResetCamera();

  vtkSmartPointer<vtkRenderWindow> renderWindow =
    vtkSmartPointer<vtkRenderWindow>::New();
  renderWindow->AddRenderer(renderer);

  vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
  vtkSmartPointer<vtkInteractorStyleImage> style =
    vtkSmartPointer<vtkInteractorStyleImage>::New();

  renderWindowInteractor->SetInteractorStyle(style);

  renderWindowInteractor->SetRenderWindow(renderWindow);
  renderWindowInteractor->Initialize();
  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}




More information about the vtkusers mailing list