Matlab中RGB和HSI的相互转换

本文介绍了一种从HSI颜色空间到RGB颜色空间及反之的转换方法。该方法适用于图像处理领域,通过数学公式实现两种颜色空间之间的转换。HSI模型基于人类对颜色的感知,而RGB则对应于红绿蓝三种基色。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

HSI----->>>RGB:

function rgb = hsi2rgb(hsi) 
%HSI2RGB Converts an HSI image to RGB. 
%   RGB = HSI2RGB(HSI) converts an HSI image to RGB, where HSI is 
%   assumed to be of class double with:   
%     hsi(:, :, 1) = hue image, assumed to be in the range 
%                    [0, 1] by having been divided 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. 
%   Copyright 2002-2004 R. C. Gonzalez, R. E. Woods, & S. L. Eddins 
%   Digital Image Processing Using MATLAB, Prentice-Hall, 2004 
%   $Revision: 1.5 $  $Date: 2003/10/13 01:01:06 $ 
% Extract the individual 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); 



RGB---->>HSI

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); 


 

 

### MATLABRGBHSI颜色空间的相互转换MATLAB中,可以利用内置函数或者自定义算法来完成RGBHSI颜色空间之间的相互转换。以下是两种方法的具体说明。 #### 方法一:使用MATLAB内置函数 `rgb2hsv` `hsv2rgb` 虽然MATLAB没有直接提供名为 `RGB2HSI` 的函数[^1],但可以通过其近似替代——HSV颜色模型来进行转换。HSV(Hue-Saturation-Value)模型与HSI(Hue-Saturation-Intensity)非常相似,主要区别在于强度分量的不同计算方式。因此,在实际应用中,通常可以用HSV代替HSI。 对于从RGBHSI的颜色转换,可采用如下代码: ```matlab function HSI = RGB2HSI(RGB) % 将输入图像从RGB转为HSV HSV = rgb2hsv(RGB); % 提取H、S分量并重新计算I分量 H = HSV(:, :, 1); % 色调 (Hue) S = HSV(:, :, 2); % 饱度 (Saturation) I = mean(double(RGB), 3) / 255; % 计算强度 (Intensity),范围0~1 % 组合成新的HSI矩阵 HSI = cat(3, H, S, I); end ``` 反向操作即由HSI返回至RGB,则需先调整HSV中的V值再通过 `hsv2rgb` 函数实现: ```matlab function RGB = HSI2RGB(HSI) % 解构HSI数据 H = HSI(:, :, 1); S = HSI(:, :, 2); I = HSI(:, :, 3); % 构造临时HSV数组用于后续转化 V = I .* (1 + S); % 更新后的明度(V) C = zeros(size(H)); % 初始化色相差C X = zeros(size(H)); % 初始化辅助变量X % 根据不同区间设置具体数值... [m,n,p]=size(HSI); for i=1:m for j=1:n htemp=floor(mod((H(i,j)*6)+eps,6))+1; if(htemp==1||htemp==6), C(i,j)=V(i,j)-I(i,j)*(1-S(i,j)); X(i,j)=(V(i,j)-C(i,j))/2; elseif(htemp>=2&&htemp<=4), C(i,j)=V(i,j)-I(i,j)*(1-S(i,j)/2); X(i,j)=(V(i,j)-C(i,j))/(sqrt(3)); end end end R=(C+X).*uint8(I*255./max(max(C))); G=((V-C)./sqrt(3)+X).*uint8(I*255./max(max(X))); B=(V-X-sqrt(3)*C).*uint8(I*255./max(max(C))); RGB=cat(3,R,G,B); end ``` 上述过程涉及较多细节运算,主要是为了适配两者间差异而设计的一些额外逻辑[^2]。 #### 方法二:手动编写转换公式 如果希望完全按照理论推导的方式进行转换而不依赖于现成工具箱内的功能,那么可以根据已知数学关系自行编码。下面给出一段简单的伪代码作为参考框架: ```matlab % 假设输入是一个标准的MxNx3大小的RGB图片矩阵 R = double(image(:,:,1)) ./ 255; G = double(image(:,:,2)) ./ 255; B = double(image(:,:,3)) ./ 255; numerator_H = ((R-G)+(R-B))./2; denominator_H = sqrt(((R-G).^2+(R-B).*(G-B))); theta = acos(numerator_H ./ denominator_H); H = theta; mask = B > G; H(mask) = 2*pi - H(mask); min_RGB = min(min(R,G),B); max_RGB = max(max(R,G),B); delta = max_RGB-min_RGB; S = delta ./ max_RGB; I = (R+G+B)/3; HSI_image = cat(3,H,S,I); ``` 此段脚本展示了如何逐像素地执行必要的代数变换以得出最终结果。 ---
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值