matlab将rgb转hsi
时间: 2023-07-30 11:06:37 浏览: 134
MATLAB 图像处理 机器视觉RGB2HSI
可以使用下面的代码将 RGB 转换为 HSI:
```matlab
function [H,S,I] = rgb2hsi(R,G,B)
% Convert RGB image to HSI
% Input: R,G,B (Matrices representing the RGB image)
% Output: H,S,I (Matrices representing the HSI image)
% Normalize RGB values
R_n = double(R)/255;
G_n = double(G)/255;
B_n = double(B)/255;
% Calculate I (Intensity)
I = (R_n + G_n + B_n)/3;
% Calculate S (Saturation)
S = 1 - 3*min(min(R_n,G_n),B_n)./(R_n+G_n+B_n);
% Calculate H (Hue)
theta = acos(0.5*((R_n-G_n)+(R_n-B_n))./sqrt((R_n-G_n).^2 + (R_n-B_n).*(G_n-B_n)) + eps);
H = zeros(size(R));
H(B_n > G_n) = 2*pi - theta(B_n > G_n);
H(B_n <= G_n) = theta(B_n <= G_n);
% Convert H to degrees
H = (H*180)/pi;
end
```
上述代码中,输入是 RGB 三个通道的矩阵,输出是 HSI 三个通道的矩阵。注意,在计算 S 时,需要先将 RGB 的值归一化到 [0,1] 内。
阅读全文