In this section , we learn to convert color images RGB in Grayscale images.
A easy method to make this conversion is to average the three colorscale.
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')
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:
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')
To represent a gray color in a image color, we hav to associate the same value to the three color scale. as:
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')
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')
Copyright © 2010-2014, all rights reserved, contact: operationpixel@free.fr