Convert grayscale image to rgb

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

Convert grayscale image to rgb

Milan Vidakovic
I have a number of single-scalar grayscale vtkImageData objects that would need to convert to rgb. Is there a way to perform this transformation without loosing the grayscale information? Even a change from grayscale to mono-color would do the trick.

Thank you!
Milan

_______________________________________________
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
Reply | Threaded
Open this post in threaded view
|

Re: Convert grayscale image to rgb

David Gobbi
Use vtkImageMapToColors, here is a python example of
how to set it up:

rgbConverter = vtkImageMapToColors()
rgbConverter.SetOutputFormatToRGB()
rgbConverter.SetLookupTable(vtkScalarsToColors())

The "vtkScalarsToColors" object describes how the
mapping of scalars to colors is done.  By default, it just
replicates the scalar into the r, g, and b components.

  David


On Sat, May 31, 2014 at 9:21 AM, Milan Vidakovic <[hidden email]> wrote:
> I have a number of single-scalar grayscale vtkImageData objects that would
> need to convert to rgb. Is there a way to perform this transformation
> without loosing the grayscale information? Even a change from grayscale to
> mono-color would do the trick.
>
> Thank you!
> Milan
_______________________________________________
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
Reply | Threaded
Open this post in threaded view
|

Re: Convert grayscale image to rgb

Milan Vidakovic
Thank you for a quick response. I did try it, and the scalar space does increase to rgb, but my output is completely black. My vtkScalarsToColors might be lacking something. The code is: 

vtkSmartPointer<vtkScalarsToColors> scalarsToColors =  vtkSmartPointer<vtkScalarsToColors>::New();

vtkSmartPointer<vtkImageMapToColors> colorMapper = vtkSmartPointer<vtkImageMapToColors>::New();
firstColorMapper->SetOutputFormatToRGB();
firstColorMapper->SetInputData(myImageVtkImageData);
firstColorMapper->SetLookupTable( scalarsToColors );


On Sat, May 31, 2014 at 5:44 PM, David Gobbi <[hidden email]> wrote:
Use vtkImageMapToColors, here is a python example of
how to set it up:

rgbConverter = vtkImageMapToColors()
rgbConverter.SetOutputFormatToRGB()
rgbConverter.SetLookupTable(vtkScalarsToColors())

The "vtkScalarsToColors" object describes how the
mapping of scalars to colors is done.  By default, it just
replicates the scalar into the r, g, and b components.

  David


On Sat, May 31, 2014 at 9:21 AM, Milan Vidakovic <[hidden email]> wrote:
> I have a number of single-scalar grayscale vtkImageData objects that would
> need to convert to rgb. Is there a way to perform this transformation
> without loosing the grayscale information? Even a change from grayscale to
> mono-color would do the trick.
>
> Thank you!
> Milan


_______________________________________________
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
Reply | Threaded
Open this post in threaded view
|

Re: Convert grayscale image to rgb

David Gobbi
You might have to tell scalarsToColors what the range of your
data is, before you use the colorMapper:

scalarsToColors->SetRange(imageData->GetScalarRange());


On Sat, May 31, 2014 at 9:58 AM, Milan Vidakovic <[hidden email]> wrote:

> Thank you for a quick response. I did try it, and the scalar space does
> increase to rgb, but my output is completely black. My vtkScalarsToColors
> might be lacking something. The code is:
>
> vtkSmartPointer<vtkScalarsToColors> scalarsToColors =
> vtkSmartPointer<vtkScalarsToColors>::New();
>
> vtkSmartPointer<vtkImageMapToColors> colorMapper =
> vtkSmartPointer<vtkImageMapToColors>::New();
> firstColorMapper->SetOutputFormatToRGB();
> firstColorMapper->SetInputData(myImageVtkImageData);
> firstColorMapper->SetLookupTable( scalarsToColors );
>
>
> On Sat, May 31, 2014 at 5:44 PM, David Gobbi <[hidden email]> wrote:
>>
>> Use vtkImageMapToColors, here is a python example of
>> how to set it up:
>>
>> rgbConverter = vtkImageMapToColors()
>> rgbConverter.SetOutputFormatToRGB()
>> rgbConverter.SetLookupTable(vtkScalarsToColors())
>>
>> The "vtkScalarsToColors" object describes how the
>> mapping of scalars to colors is done.  By default, it just
>> replicates the scalar into the r, g, and b components.
>>
>>   David
>>
>>
>> On Sat, May 31, 2014 at 9:21 AM, Milan Vidakovic <[hidden email]>
>> wrote:
>> > I have a number of single-scalar grayscale vtkImageData objects that
>> > would
>> > need to convert to rgb. Is there a way to perform this transformation
>> > without loosing the grayscale information? Even a change from grayscale
>> > to
>> > mono-color would do the trick.
>> >
>> > Thank you!
>> > Milan
_______________________________________________
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
Reply | Threaded
Open this post in threaded view
|

Re: Convert grayscale image to rgb

Milan Vidakovic
Did try adding range, but it made no difference. The images are displayed on vtkImageViewer2, if that plays a part.

Thank you.


On Sat, May 31, 2014 at 6:11 PM, David Gobbi <[hidden email]> wrote:
You might have to tell scalarsToColors what the range of your
data is, before you use the colorMapper:

scalarsToColors->SetRange(imageData->GetScalarRange());


On Sat, May 31, 2014 at 9:58 AM, Milan Vidakovic <[hidden email]> wrote:
> Thank you for a quick response. I did try it, and the scalar space does
> increase to rgb, but my output is completely black. My vtkScalarsToColors
> might be lacking something. The code is:
>
> vtkSmartPointer<vtkScalarsToColors> scalarsToColors =
> vtkSmartPointer<vtkScalarsToColors>::New();
>
> vtkSmartPointer<vtkImageMapToColors> colorMapper =
> vtkSmartPointer<vtkImageMapToColors>::New();
> firstColorMapper->SetOutputFormatToRGB();
> firstColorMapper->SetInputData(myImageVtkImageData);
> firstColorMapper->SetLookupTable( scalarsToColors );
>
>
> On Sat, May 31, 2014 at 5:44 PM, David Gobbi <[hidden email]> wrote:
>>
>> Use vtkImageMapToColors, here is a python example of
>> how to set it up:
>>
>> rgbConverter = vtkImageMapToColors()
>> rgbConverter.SetOutputFormatToRGB()
>> rgbConverter.SetLookupTable(vtkScalarsToColors())
>>
>> The "vtkScalarsToColors" object describes how the
>> mapping of scalars to colors is done.  By default, it just
>> replicates the scalar into the r, g, and b components.
>>
>>   David
>>
>>
>> On Sat, May 31, 2014 at 9:21 AM, Milan Vidakovic <[hidden email]>
>> wrote:
>> > I have a number of single-scalar grayscale vtkImageData objects that
>> > would
>> > need to convert to rgb. Is there a way to perform this transformation
>> > without loosing the grayscale information? Even a change from grayscale
>> > to
>> > mono-color would do the trick.
>> >
>> > Thank you!
>> > Milan


_______________________________________________
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
Reply | Threaded
Open this post in threaded view
|

Re: Convert grayscale image to rgb

David Cole-2
Do these examples give you the proper output on your machine?

http://www.vtk.org/Wiki/VTK/Examples/Cxx/Images/Colored2DImageFusion
http://www.vtk.org/Wiki/VTK/Examples/Cxx/Images/Transparency

If so, perhaps you could compare their code to yours and figure out
what your code lacks...


HTH,
David C.

_______________________________________________
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
Reply | Threaded
Open this post in threaded view
|

Re: Convert grayscale image to rgb

Milan Vidakovic
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().

Thanks,
Milan


On Sun, Jun 1, 2014 at 5:20 PM, David Cole <[hidden email]> wrote:
Do these examples give you the proper output on your machine?

http://www.vtk.org/Wiki/VTK/Examples/Cxx/Images/Colored2DImageFusion
http://www.vtk.org/Wiki/VTK/Examples/Cxx/Images/Transparency

If so, perhaps you could compare their code to yours and figure out what your code lacks...


HTH,
David C.



_______________________________________________
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
Reply | Threaded
Open this post in threaded view
|

Re: Convert grayscale image to rgb

David Cole-2
In reply to this post by Milan Vidakovic
> 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;
}


_______________________________________________
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
Reply | Threaded
Open this post in threaded view
|

Re: Convert grayscale image to rgb

David Cole-2
In reply to this post by Milan Vidakovic
> 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.


_______________________________________________
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
Reply | Threaded
Open this post in threaded view
|

Re: Convert grayscale image to rgb

Milan Vidakovic
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 <[hidden email]> 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.




_______________________________________________
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
Reply | Threaded
Open this post in threaded view
|

Re: Convert grayscale image to rgb

David Gobbi
Hi Milan,

The vtkImageShiftScale operation is totally unnecessary here,
just set the range of the lookup table to the range of your data,
e.g.:

lookupTable->SetRange(0.0, VTK_UNSIGNED_SHORT_MAX);

I hope this helps.

  - David


On Thu, Jun 5, 2014 at 9:33 AM, Milan Vidakovic <[hidden email]> wrote:

> 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 <[hidden email]> 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.
_______________________________________________
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