• Matlab 现实RGB和HSI转换


    忙了几天终于搞定了RGB和HSI的转化:

    ************************************************

    R [0,255],G [0,255],B [0,255]

    H [0,359],S [0,255],I [0,255]

    ************************************************

    function hsi=rgb2hsi(rgb)
    r=rgb(:,1);
    g=rgb(:,2);
    b=rgb(:,3);

    num=0.5*((r-g)+(r-b));
    den=sqrt((r-g).^2+(r-b).*(g-b));
    theta=acos(num./(den+eps));
    H=theta;
    H(b>g)=2*pi-H(b>g);
    H=H/(2*pi);

    num=min(min(r,g),b);
    den=r+g+b;
    den(den==0)=eps;
    S=1-3.*num./den;
    H(S==0)=0;
    I=(r+g+b)/3;
    hsi=round([360*H,255*S,I]);

    *********************************************************

    function rgb=hsi2rgb(hsi)
    a=size(hsi);
    b=a(1);
    H = hsi(:,1);
    S = hsi(:,2)/255;
    I = hsi(:,3)/255;

    for i=1:b
    if H(i) <=120
        B(i) = I(i)*(1-S(i));
        theta1 = H(i)*pi/180;
        theta2 = ( 60 - H(i) )*pi/180;
        R(i) = I(i)*(1+S(i)*cos(theta1)/cos(theta2));
        G(i) = 3*I(i) - B(i) - R(i);
    elseif H(i) <=240
        R(i) = I(i)*(1-S(i));
        theta1 = (H(i)-120)*pi/180;
        theta2 = ( 180 - H(i) )*pi/180;
        G(i) = I(i)*(1+S(i)*cos(theta1)./cos(theta2));
        B(i) = 3*I(i) - G(i) - R(i);
    else   
        G(i) = I(i)*(1-S(i));
        theta1 = (H(i)-240)*pi/180;
        theta2 = ( 300- H(i))*pi/180;
        B(i) = I(i)*(1+S(i)*cos(theta1)./cos(theta2));
        R(i)= 3*I(i) - G(i) - B(i);
    end
    end
    rgb =round([R'*255,G'*255,B'*255]);

    ***********************************************************

  • 相关阅读:
    neutron-server Connection pool is full, discarding connection 连接池过满
    C#中值类型与引用类型
    抽象类与接口的比较
    XML解析与文件存取
    Json序列化与反序列化
    关于Stream系列实战
    CTS,CLS,CLR
    冒泡排序——算法
    Js 调用 webservice
    使用WebService的优点
  • 原文地址:https://www.cnblogs.com/Neddy/p/1997567.html
Copyright © 2020-2023  润新知