MATLAB从RGB到HSI
时间: 2024-05-12 14:19:29 浏览: 92
以下是将RGB转换为HSI的MATLAB代码:
```matlab
% 输入RGB图像
rgbImage = imread('image.jpg');
% 将RGB图像转换为double类型
rgbImage = im2double(rgbImage);
% 分离RGB通道
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% 计算亮度分量
intensity = (redChannel + greenChannel + blueChannel) / 3;
% 计算饱和度分量
minChannel = min(min(redChannel, greenChannel), blueChannel);
saturation = 1 - (3 ./ (redChannel + greenChannel + blueChannel)) .* minChannel;
% 计算色调分量
numerator = 0.5 .* ((redChannel - greenChannel) + (redChannel - blueChannel));
denominator = sqrt((redChannel - greenChannel).^2 + (redChannel - blueChannel) .* (greenChannel - blueChannel));
hue = acos(numerator ./ (denominator + eps));
% 对Hue进行调整
if blueChannel > greenChannel
hue = 2 * pi - hue;
end
% 将Hue、Saturation和Intensity合并为HSI图像
hsiImage = cat(3, hue, saturation, intensity);
% 显示HSI图像
imshow(hsiImage);
```
阅读全文