玩转FPGA山寨版

看了《玩转FPGA》,写的不错,写写山寨版和大家交流!

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

忙了几天终于搞定了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]);

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

posted on 2011-03-28 11:29  Neddy11  阅读(6884)  评论(0编辑  收藏  举报