Implementation of MATLAB program for converting RGB color space to HSI color space
During the implementation of the program, pay attention to the normalization of RGB pixel values, and then thousands of users should follow the formula. I have previously had Theta as a complex number, SQRT calculates the negative number and returns the result of the plural number (Bug)
The program is implemented by MATLAB and makes use of the convenience of matrix operations. If you directly operate a single element here... It will be very slow and slow. It will be so slow that I can be stuck in my computer... If it is rewritten to C, the program performance may still be faulty.
Okay. Let's introduce the HSI and RGB conversion methods.
%******************************************% Code writer : EOF% Code file : RGB2HSI.m% Code date : 2014.10.15%% Code description:% This code is a demo for how to transfrom pixel'value in% RGB-space into HSI-space.%%******************************************Img_Original = imread('/home/jasonleaster/Software/opencv-2.4.9/Project_in_XTU/PDF/mountain.png');Height_Original = size(Img_Original,1);Width_Original = size(Img_Original,2);Channel_Orignal = size(Img_Original,3);Theta = zeros(1,Height_Original * Width_Original);S = zeros(1,Height_Original * Width_Original);I = zeros(1,Height_Original * Width_Original);R_temp = double(Img_Original(:,:,1));G_temp = double(Img_Original(:,:,2));B_temp = double(Img_Original(:,:,3));R = R_temp./(R_temp + G_temp + B_temp);G = G_temp./(R_temp + G_temp + B_temp);B = B_temp./(R_temp + G_temp + B_temp);for row = 1:Height_Original for col = 1: Width_Original % initialize min with 255(the max value in RGB space) min = 255; for channel = 1:Channel_Orignal if min > Img_Original(row,col,channel) min = Img_Original(row,col,channel); end end Dark_channel(row,col) = double(min)/255; endend Theta = acos(0.5*((R - G) + (R - B))./(sqrt((R -G).^2 + (R-B).*(G-B))));S = 1 - (3./(R + G + B)).* double(Dark_channel);I = 255*(R + G + B)./3;S = 100*S;for row = 1: Height_Original for col = 1:Width_Original if B(row,col) <= G(row,col) H(row,col) = Theta(row,col); else H(row,col) = 2*pi - Theta(row,col); end endendH = H*180/pi;
Implementation of MATLAB program for converting RGB color space to HSI color space