matlab中rgb2hsi和hsi2rgb

借鉴与此

将此函数保存于某个.m文件中,即可直接调用
上述链接还有hsi2rgb

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. The input image can be of class double (with values 
%   in the range [0, 1]), uint8, or uint16.  
% 
%   The output image, HSI, is of class double, where: 
%     hsi(:, :, 1) = hue image normalized 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]. 

%   Copyright 2002-2004 R. C. Gonzalez, R. E. Woods, & S. L. Eddins 
%   Digital Image Processing Using MATLAB, Prentice-Hall, 2004 
%   $Revision: 1.4 $  $Date: 2003/09/29 15:21:54 $ 

% Extract the individual component immages. 
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); 
end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值