[Insight-users] RGB to gray conversion with itk::ImageFileReader

Luis Ibanez luis.ibanez at kitware.com
Tue Aug 17 14:23:51 EDT 2004


Hi Kaushik,


The conversion from RGB to Grayscale is done in the
class ConvertPixelBuffer.  Please look at the file


       Insight/Code/IO/
            itkConvertPixelBuffer.txx



in particular to lines 129-146:




> ::ConvertRGBToGray(InputPixelType* inputData, 
>                    OutputPixelType* outputData , int size)
> {   
>   // Weights convert from linear RGB to CIE luminance assuming a
>   // modern monitor.  See Charles Pontyon's Colour FAQ
>   // http://www.inforamp.net/~poynton/notes/colour_and_gamma/ColorFAQ.html
>   InputPixelType* endInput = inputData + size*3;
>   while(inputData != endInput)
>     {
>     OutputComponentType val = static_cast<OutputComponentType>( 
>       0.2125 * static_cast<OutputComponentType>(*inputData) +
>       0.7154 * static_cast<OutputComponentType>(*(inputData+1)) +
>       0.0721 * static_cast<OutputComponentType>(*(inputData+2))   );
>     inputData += 3;
>     OutputConvertTraits::SetNthComponent(0, *outputData++, val);
>     }
>   
> }



As you can see the factors used are:


       Gray = 0.2125 * Red   +
              0.7154 * Green +
              0.0721 * Blue


If you want to use other factors, you can load your image as a
native RGB image using the RGBPixel class for instantiating
your image type, and then use ImageAdaptors or the UnaryFunctor
Filter in order to convert it to Grayscale.

Please look at the RGBToLuminanceImageAdaptor
http://www.itk.org/Insight/Doxygen/html/classitk_1_1RGBToLuminanceImageAdaptor.html

and the RGBToLuminanceImageFilter:
http://www.itk.org/Insight/Doxygen/html/classitk_1_1RGBToLuminanceImageFilter.html


Regards,


    Luis


--------------------------
Pavani, Sri Kaushik wrote:
> Hi,
>  
> typedef itk::Image<unsigned char, *2* > ImageType;
> typedef itk::ImageFileReader< ImageType > ReaderType;
>  
> When I use this reader to read a color image with (RGB components), how 
> does the pixel information from the three channels of the original image 
> (R, G and B) get mapped to the 2D image instance I created?
>  
> I suspected that the standard procedure of converting RGB to gray (Y 
> [luminance] = 0.3R + 0.59G + 0.11B) was done internally, but the results 
> of tests I made regarding this were slightly different. I obtained Y = 
> 0.2197R + 0.691G + 0.089B. Was this transformation purposeful, or, am I 
> doing something wrong?
>  
> Many Thanks,
> Kaushik.
>  
> _________________________________________________________
> Sri Kaushik Pavani
> María de Luna, 1. Ed. Ada Byron, D2.03.
> Dep. de Ingeniería Electrónica y Comunicaciones
> Centro Polictécnico Superior
> Universidad de Zaragoza
> E-50018 Zaragoza.
>  
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Insight-users mailing list
> Insight-users at itk.org
> http://www.itk.org/mailman/listinfo/insight-users






More information about the Insight-users mailing list