zoukankan      html  css  js  c++  java
  • 色彩转换——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;

  • 相关阅读:
    x64 平台开发 Mapxtreme 编译错误
    hdu 4305 Lightning
    Ural 1627 Join(生成树计数)
    poj 2104 Kth Number(可持久化线段树)
    ural 1651 Shortest Subchain
    hdu 4351 Digital root
    hdu 3221 Bruteforce Algorithm
    poj 2892 Tunnel Warfare (Splay Tree instead of Segment Tree)
    hdu 4031 Attack(BIT)
    LightOJ 1277 Looking for a Subsequence
  • 原文地址:https://www.cnblogs.com/mtcnn/p/9412735.html
Copyright © 2011-2022 走看看