Freitag, 24. Mai 2013

Converting RGB Image to HSI






Converting RGB Image to HSI

H stands for Hue, S for Saturation and I for Intensity.



MATLAB CODE:
Read a RGB Image
A=imread('peppers.png');
figure,imshow(A);title('RGB Image');




%Represent the RGB image in [0 1] range
I=double(A)/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);

%Normalize 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(A));
HSI(:,:,1)=H;
HSI(:,:,2)=S;
HSI(:,:,3)=I;



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



















Explanation:

1.    Read a RGB image using ‘imread’ function.
2.    Each RGB component will be in the range of [0 255].  Represent the image in [0 1] range by dividing the image by 255.
3.    Find the theta value. If B<=G then H= theta. If B>G then H= 360-theta
4.    Use ‘acosd’ function to find inverse cosine and obtain the result in degrees.
5.    Divide the hue component by 360 to represent in the range [0 1]
6.    Similarly, find the saturation and the intensity components.
7.    Display the image.