Color Processing

Grayscale Conversion

In this section , we learn to convert color images RGB in Grayscale images.

Average

A easy method to make this conversion is to average the three colorscale.

$Im{g}_{moy}=\genfrac{}{}{0.1ex}{}{\left(R\cdot G\cdot B\right)}{3}$

Where R,G and B are the three color space red, green and blue
Look at the example:

and the programme:

```clear all; close all; [filename, pathname] = uigetfile({'*.jpg;*.tif;*.png;*.gif;*.bmp','All Image Files';...           '*.*','All Files' },'mytitle',...           'C:\Work\myfile.jpg'); img = imread(filename); [ligne colonne dimension]=size(img) img=double(img); R=img(:,:,1); G=img(:,:,2); B=img(:,:,3); %conversion niveau de gris moyenne img_avg = (R+G+B) /3; figure(  'Name','Grayscale average',...             'NumberTitle','off',...             'color',[0.3137 0.3137 0.5098]); subplot(121) imshow(uint8(img)) title('Image ') subplot(122) imshow(uint8(img_avg)) title('Image GrayScale Average')```

Luminance

The last conversion disregardsthe human eyes physiology. Indeed, the eye doesn't see the three scale at the same intensity. So, the eye perceives better green colors than blue ones. The red is between the last both. The last conversion worls but doesn't represent what the eye see. To improve the conversion, we use the luminance computing as:

$Im{g}_{lum}=wr\cdot R+wg\cdot G+wb\cdot B$

where wr, wg and wb are the three weight factors for the three color R, G and B. It exists alot of convention for those factor. I give you two example. For the encoding analog color television signals wr=0,299 wg=0,587 et wb=0,114. For the digital color encoding: wr=0,2125 wg=0,7154 et wb=0,072. There is an example for those two luminance conversion:

and the program to make them

```%conversion luminance wr=0.299; wg=0.587; wb=0.114; img_L = ( R * wr + G * wg + B * wb ); wr=0.2125; wg=0.7154; wb=0.072; img_L2 = ( R * wr + G * wg + B * wb ); figure(  'Name','Grayscale Luminance',...             'NumberTitle','off',...             'color',[0.3137 0.3137 0.5098]); subplot(131) imshow(uint8(img)) title('Image ') subplot(132) imshow(uint8(img_L)) title('Image Luminance') subplot(133) imshow(uint8(img_L2)) title('Image Luminance 2')  ```

Hueless Color Image

To represent a gray color in a image color, we hav to associate the same value to the three color scale. as: $\left[R,G,B\right]=\left[Y,Y,Y\right]$
where Y is Grayscale levels.
An example:

and the program

```% Grayscale in ColorScale img_hueless = cat(3, img_L, img_L, img_L); figure(  'Name','Grayscale in Colorspace',...             'NumberTitle','off',...             'color',[0.3137 0.3137 0.5098]); subplot(121) imshow(uint8(img)) title('Image ') subplot(122) imshow(uint8(img_hueless)) title('Image Hueless Color')```

The Complete Code

```clear all; close all; [filename, pathname] = uigetfile({'*.jpg;*.tif;*.png;*.gif;*.bmp','All Image Files';...           '*.*','All Files' },'mytitle',...           'C:\Work\myfile.jpg'); img = imread(filename); [ligne colonne dimension]=size(img) img=double(img); R=img(:,:,1); G=img(:,:,2); B=img(:,:,3); %conversion niveau de gris moyenne img_avg = (R+G+B) /3; figure(  'Name','Grayscale average',...             'NumberTitle','off',...             'color',[0.3137 0.3137 0.5098]); subplot(121) imshow(uint8(img)) title('Image ') subplot(122) imshow(uint8(img_avg)) title('Image GrayScale Average') %conversion luminance wr=0.299; wg=0.587; wb=0.114; img_L = ( R * wr + G * wg + B * wb ); wr=0.2125; wg=0.7154; wb=0.072; img_L2 = ( R * wr + G * wg + B * wb ); figure(  'Name','Grayscale Luminance',...             'NumberTitle','off',...             'color',[0.3137 0.3137 0.5098]); subplot(131) imshow(uint8(img)) title('Image ') subplot(132) imshow(uint8(img_L)) title('Image Luminance') subplot(133) imshow(uint8(img_L2)) title('Image Luminance 2') % Grayscale in ColorScale img_hueless = cat(3, img_L, img_L, img_L); figure(  'Name','Grayscale in Colorspace',...             'NumberTitle','off',...             'color',[0.3137 0.3137 0.5098]); subplot(121) imshow(uint8(img)) title('Image ') subplot(122) imshow(uint8(img_hueless)) title('Image Hueless Color')```