[vtkusers] Convert grayscale image to rgb

Milan Vidakovic milan.vidakovic at tp.rs
Thu Jun 5 11:33:59 EDT 2014


I managed to get decent results using the following formula. So, 16bit
grayscale to RGBA:


*vtkSmartPointer<vtkLookupTable> lookupTable=
vtkSmartPointer<vtkLookupTable>::New();*
* lookupTable->SetNumberOfTableValues(VTK_UNSIGNED_CHAR_MAX + 1);  *
* lookupTable->SetRange(0.0, VTK_UNSIGNED_CHAR_MAX);*

* // Iterate through every scalar value from 0-255*
* // and fill RGB tables with scalar=R=G=B values*
* for (int i = 0; i <= VTK_UNSIGNED_CHAR_MAX; i++)*
* { *
* double doubleIndex = static_cast<double>(i); *
* lookupTable->SetTableValue(i, *
* VTK_UNSIGNED_CHAR_MAX - doubleIndex, *
* VTK_UNSIGNED_CHAR_MAX - doubleIndex, *
* VTK_UNSIGNED_CHAR_MAX - doubleIndex, *
* 1.0); *
* }*

* lookupTable->Build();*

*                vtkImageData* imageData <= 16bit grayscale*
* imageData->AllocateScalars(VTK_UNSIGNED_SHORT, 1);*
* // Cast 16bit grayscale image to 8bit and rescale*
* vtkSmartPointer<vtkImageShiftScale> rescaler =
vtkSmartPointer<vtkImageShiftScale>::New();    *
* rescaler->SetOutputScalarTypeToUnsignedChar(); *
* rescaler->SetScale(1.0 / (VTK_UNSIGNED_CHAR_MAX + 1)); *
* rescaler->SetShift(0);*
* rescaler->SetInputData(imageData);*

* // Map the input image using RGBA lookup-table*

* vtkSmartPointer<vtkImageMapToColors> imageColorMapper=
vtkSmartPointer<vtkImageMapToColors>::New(); *
* imageColorMapper->PassAlphaToOutputOn();  *
* imageColorMapper->SetLookupTable(lookupTable);  *
* imageColorMapper->SetInputConnection(rescaler->GetOutputPort());*

*                imageColorMapper->GetOutput() => 8bit RGBA*

There might have been a more elegant solution, but this is what worked for
me.

Thanks to all!


On Wed, Jun 4, 2014 at 1:34 PM, David Cole <dlrdave at aol.com> wrote:

> > Hi David. Thank you very much for the example, it did push me a bit
> > further. Another thing, I have a number of
> > 16bit grayscale images that also need converting. In what way should
> > I modify the lookup table to accommodate these files?
>
> See the updated example which I've put on the wiki now:
>
>     http://www.vtk.org/Wiki/VTK/Examples/Cxx/Images/ImageMapToColors
>
> You can use any of the classes that derive from vtkScalarsToColors as
> the lookup table.
>
>     http://www.vtk.org/doc/nightly/html/classvtkScalarsToColors.html
>
> If you just use vtkLookupTable as in the example, you can just modify
> the arguments to the SetRange call to cover whatever range of grayscale
> values you want to map to color. Anything lower than the lower arg will
> map to the lowest color, and anything higher than the higher arg will
> map to the highest color... in between values get interpolated.
>
>     http://www.vtk.org/doc/nightly/html/classvtkLookupTable.html
>
> Please keep replies on the list so others may also benefit from the
> discussion.
>
>
> Thanks,
> David C.
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20140605/9108758e/attachment.html>


More information about the vtkusers mailing list