色彩转换——RGB & HSI

RGB to HSI

I=(R+G+B)/3;

S=1-3*min(R,G,B)/(R+G+B);

H = cos^(-1)((0.5*((R-G)+(R-B))) / ((R-G)^2 + (R-B)*(G-B))^(0.5))

If S = 0; H =0 ;

If B > G; H=360-H;


HSI to RGB

If  0  <  H <= 120 then 
    B = 1/3(1-S)
    R = 1/3(1+ ((S cos H) / (cos(60 -H))))
    G = 1 -(B+R)
If 120 < H <= 240 then
    H = H  – 120
    R = 1/3(1-S)
    G = 1/3(1+ ((S cos H) / (cos(60 -H))))
    B = 1 -(R+G)
If 240 < H <=360 then
    H = H  – 240
    G = 1/3(1-S)
    B = 1/3(1+ ((S cos H) / (cos(60 -H))))
    R = 1 -(G+B)


算法参考来源: 冈萨雷斯 《数字图像处理》


function  [H,S,I]=RgbToHsi(Image_Input)

Image_Input=double(Image_Input)/255;
R_Input=Image_Input(:,:,1);
G_Input=Image_Input(:,:,2);
B_Input=Image_Input(:,:,3);
I = (R_Input+G_Input+B_Input)/3.0;

[heigth,width]=size(R_Input);
S(1:heigth,1:width)=0;
H(1:heigth,1:width)=0;

for i=1:heigth
    for j=1:width
        temp=[R_Input(i,j),G_Input(i,j),B_Input(i,j)];
        S(i,j)=1-3*min(temp)/sum(temp);
        X=R_Input(i,j);
        Y=G_Input(i,j);
        Z=B_Input(i,j);
        temp_1=((X-Y)+(X-Z)+1e-6)/(2*sqrt((X-Y).^2+(X-Z)*(Y-Z))+1e-6);
        Sigma=acos(temp_1);
        if(Z<=Y)
            H(i,j)=Sigma/(2*pi);
        else
            H(i,j)=(2*pi-Sigma)/(2*pi);
        end
    end
end


function Image_out=HsiToRgb(H,S,I)

H_angle=H*360;
[heigth,width]=size(H);
R(1:heigth,1:width)=0;
G(1:heigth,1:width)=0;
B(1:heigth,1:width)=0;

for i=1:heigth
    for j=1:width
        if(S(i,j)<1e-6)
            R(i,j)=I(i,j);
            G(i,j)=I(i,j);
            B(i,j)=I(i,j);
        else
            if(H_angle(i,j)>=0 && H_angle(i,j)<=120)
                B(i,j)=(1-S(i,j))*I(i,j);
                sigma=(H_angle(i,j)-60)*pi/180;
                temp=tan(sigma)/sqrt(3);
                G(i,j)=(1.5+1.5*temp)*I(i,j)-(0.5+1.5*temp)*B(i,j);
                R(i,j)=3*I(i,j)-G(i,j)-B(i,j);
            else
                if(H_angle(i,j)>=120 && H_angle(i,j)<=240)
                    R(i,j)=(1-S(i,j))*I(i,j);
                    sigma=(H_angle(i,j)-180)*pi/180;
                    temp=tan(sigma)/sqrt(3);
                    B(i,j)=(1.5+1.5*temp)*I(i,j)-(0.5+1.5*temp)*R(i,j);
                    G(i,j)=3*I(i,j)-R(i,j)-B(i,j);
                else
                    G(i,j)=(1-S(i,j))*I(i,j);
                    sigma=(H_angle(i,j)-300)*pi/180;
                    temp=(tan(sigma))/sqrt(3);
                    R(i,j)=(1.5+1.5*temp)*I(i,j)-(0.5+1.5*temp)*G(i,j);
                    B(i,j)=3*I(i,j)-R(i,j)-G(i,j);
                end
            end
        end
    end
end

Image_out(:,:,1)=R;
Image_out(:,:,2)=G;
Image_out(:,:,3)=B;

转载于:https://www.cnblogs.com/muyuge/p/6152402.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值