RGB与HSI颜色空间互换函数(matlab)

27 篇文章 1 订阅
18 篇文章 0 订阅
RGB与HSI颜色空间互换函数(matlab)
2011-09-13 16:14:14
标签: RGB  HSI  颜色空间  互换  休闲

近看了一本数字图像处理的原版书。其中提供了RGB与HSI颜色空间之间的相互转变函数。已在matlab7.0中测试成功。不敢独享,贴出来供大家参考。

function hsi=rgb2hsi(rgb)
%RGB2HSI Converts an RGB image to HSI
% HSI=RGB2HSI(rgb) converts an RGB image to HSI. The input image is
% assumed to be of size M-by-N-by-3, where the third dimension accounts
% for three image planes:red, green, and blue, in that order. If all RGB
% component images are equal, the HSI conversion is undefined. Ths input
% image can be of class double (with values in the rang[0,1]), uint8, or
% uint16.
% The output image, HSI, is of class double, where:
% hsi(:,:,1)= hue image normalized values to the range [0,1] by
% dividing all angle values by 2*pi.
% hsi(:,:,2)=saturation image, in the range [0,1].
% hsi(:,:,3)=intensity image, in the range [0,1].
%Extract the individual component images.
rgb=im2double(rgb);
r=rgb(:,:,1);
g=rgb(:,:,2);
b=rgb(:,:,3);
%Implement the conversion equations.
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;
%Combine all three results into an hsi image.
hsi=cat(3,H,S,I);


function rgb=hsi2rgb(hsi)
%HSI2RGB Converts an HSI image to RGB.
% HSI2RGB Converts an HSI image to RGB, where HSI is assumed to be of
% class double with:
% hsi(:,:,1)= hue image normalized values to the range [0,1] by
% dividing all angle values by 2*pi.
% hsi(:,:,2)=saturation image, in the range [0,1].
% hsi(:,:,3)=intensity image, in the range [0,1].
% The components of the output image are:
% rgb(:,:,1)=red;
% rgb(:,:,2)=green.
% rgb(:,:,3)=blue.
%Extract the individaul HSI component images.
H=hsi(:,:,1)*2*pi;
S=hsi(:,:,2);
I=hsi(:,:,3);

%Implement the conversion equations.
R=zeros(size(hsi,1),size(hsi,2));
G=zeros(size(hsi,1),size(hsi,2));
B=zeros(size(hsi,1),size(hsi,2));

% RG sector (0<=H<2*pi/3).
idx=find((0<=H)&(H<2*pi/3));
B(idx)=I(idx).*(1-S(idx));
R(idx)=I(idx).*(1+S(idx).*cos(H(idx))./cos(pi/3-H(idx)));
G(idx)=3*I(idx)-(R(idx)+B(idx));
%BG sector (2*pi/3<=H<4*pi/3).
idx=find((2*pi/3<=H)&(H<4*pi/3));
R(idx)=I(idx).*(1-S(idx));
G(idx)=I(idx).*(1+S(idx).*cos(H(idx)-2*pi/3)./cos(pi-H(idx)));
B(idx)=3*I(idx)-(R(idx)+G(idx));
%BR sector.
idx=find((4*pi/3<=H)&(H<=2*pi));
G(idx)=I(idx).*(1-S(idx));
B(idx)=I(idx).*(1+S(idx).*cos(H(idx)-4*pi/3)./cos(5*pi/3-H(idx)));
R(idx)=3*I(idx)-(G(idx)+B(idx));
%Combine all three results into an RGB image. Clip to [0,1] to compensate for floating-point arithmetic rounding effects.
rgb=cat(3,R,G,B);
rgb=max(min(rgb,1),0);

PS:eps在matlab中指最小的不等于0的正数,是matlab uint16数中最接近0的数,或者可以理解为可以使(1+eps)>1为真的最小数。加eps也就是为了在尽可能不影响计算结果的前提下避免0/0的情况出现的方法!

PS2:此处提供的rgb转成hsi的程序,结果提供的是double类型的结果,既用图像的double类型的数据计算的结果。如果要转成我们在书上见到的那种(H指角度范围在0到360度之间,S指色度值在0到1之间,I指亮度值在0到1之间)形式,可以在现有的H值上乘以360换算。

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值