首页 > 代码库 > RGB 颜色空间转 HSI 颜色空间的matlab程序实现

RGB 颜色空间转 HSI 颜色空间的matlab程序实现


RGB 颜色空间转 HSI 颜色空间的matlab程序实现


                程序实现的时候注意把RGB像素值归一化就是了,然后千千万万要根据公式来,不能大意,我之前就有theta为复数的情况,是sqrt对负数进行运算进而得到了复数的结果(BUG)


       程序是由matlab实现的,利用了矩阵运算的便捷性,这里如果直接操作单个元素的话。。。会很慢很慢,慢到能把我电脑卡死。。。如果改写成C可能程序的性能还是会有问题


好吧,介绍一下HSI和RGB的转化方法







%******************************************
%   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;
        end
end      
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
    end
end

H = H*180/pi;













RGB 颜色空间转 HSI 颜色空间的matlab程序实现