How to convert a gray image to rgb image with a specified colormap?

I need to convert a gray image to rgb color image. And I find that vtkLookupTable is useful to convert gray image to colored image. But, how can I assign the colormap? In matlab, there are some available colormaps, such as jet, cool, hot, hsv and so on. So, how can I convert the gray image to rgb color image with jet colormap in vtk?

Hello, Zhang,

You can make any colormap you want. All you have to do is to find the interpolation points that define that “jet” color table (maybe looking in the MATLAB docs). Unfortunately, there is no way to build that by doing something simple like vtkLookupTable::getLUT("jet").

Once you have the interpolation points relating the min (0) and max (255) values to RGB values you can write a function like this:

vtkSmartPointer<vtkLookupTable>
View3dColorTables::getClassicRainbow(double min, double max)
{
    size_t tableSize = 32;

    //create a color interpolator object
    vtkSmartPointer<vtkColorTransferFunction> ctf =
            vtkSmartPointer<vtkColorTransferFunction>::New();
    ctf->SetColorSpaceToRGB();
    ctf->AddRGBPoint(0.00, 0.000, 0.000, 1.000);
    ctf->AddRGBPoint(0.25, 0.000, 1.000, 1.000);
    ctf->AddRGBPoint(0.50, 0.000, 1.000, 0.000);
    ctf->AddRGBPoint(0.75, 1.000, 1.000, 0.000);
    ctf->AddRGBPoint(1.00, 1.000, 0.000, 0.000);

    //create the color table object
    vtkSmartPointer<vtkLookupTable> lut = 
            vtkSmartPointer<vtkLookupTable>::New();
    lut->SetTableRange(min, max);
    lut->SetNumberOfTableValues(tableSize);
    for(size_t i = 0; i < tableSize; ++i)
    {
        double *rgb;
        rgb = ctf->GetColor(static_cast<double>(i)/tableSize);
        lut->SetTableValue(i, rgb[0], rgb[1], rgb[2]);
    }
    lut->SetRampToLinear();
    lut->Build();

    return lut;
}

cheers,

Paulo

1 Like