# RGB-to-HSI and HSI-to-RGB Conversions

by 4hathacker  |  in Image Processing at  8:54 AM

RGB to HSI conversion is one of the methods for image enhancement. Let us first discuss about RGB and HSI models and then using simple formulas just see how this conversion happens.

We have studied about RGB Color model in one of the posts earlier.Let us revise the RGB once again only the part that is important for HSI conversion.
In the RGB model, an image consists of three independent image planes, one in each of the primary colours: red, green and blue. Specifying a particular colour is by specifying the amount of each of the primary components present. Figure shows the geometry of the RGB colour model for specifying colours using a Cartesian coordinate system. The greyscale spectrum, i.e. those colours made from equal amounts of each primary, lies on the line joining the black and white vertices.

In HSI model, color can be specified by three attributes/quantities namely hue, saturation and intensity.
Hue is a color attribute which is used to describe a pure form of color (may be pure yellow, red or orange).
Saturation is a measure of degree to which a pure color is diluted by white light.
Intensity is the information of colors in the image.
All in one, we can say that HSI model is in close relation with the color perception of human beings.

Conversion formula:
In the image below, see for the rgb_to_hsi conversion formula and also find hsi_to_rgb conversion formula in the next image.

MATLAB Program:
Now let us create a simple program which asks for an image location (use inverted quotes for location) and then gives us back the converted HSI image. Here I am only giving the program code built into a function for rgb_to_hsi conversion. Try yourself to build a code for hsi_to_rgb conversion of image using the formula given.

function rgb_to_hsi()

x = input('Enter the loaction of RGB Color image: ');
imshow(rgb);title('RGB image');

%Represent the RGB image in [0 1] range
I = double(rgb)/255;
R = I(:,:,1);
G = I(:,:,2);
B = I(:,:,3);

%Hue
numi = 1/2*((R-G)+(R+B));
denom = ((R-G).^2+((R-B).*(G-B))).^0.5;

%To avoid divide by zero exception add a small number in the denominator
H = acosd((numi./(denom+0.000001)));

%If B>G then H = 360 - Theta
H(B>G)=360-H(B>G);

%Normalise to the range [0 1]
H = H/360;

%Saturation
S = 1- (3./(sum(I,3)+0.000001)).*min(I,[],3);

%Intensity
I = sum(I,3)./3;

%HSI
HSI = zeros(size(rgb));
HSI(:,:,1)=H;
HSI(:,:,2)=S;
HSI(:,:,3)=I;

figure, imshow(HSI);title('HSI Image');

Thats enough for RGB and HSI. To know more about that just search on google for Munsell Color System. It will get you to deeper concepts and similar algorithms.