下面的这个代码是在使用了rgb2hsi()方法失败后,进行修改的。
通过分离RGB通道,将图像的红色通道、绿色通道和蓝色通道分别存储在变量R、G和B中。
- % 读取图像
- img = imread('dog.jpg');
-
- % 将RGB图像归一化到0到1之间
- img = im2double(img);
-
- % 分离RGB通道
- R = img(:, :, 1);
- G = img(:, :, 2);
- B = img(:, :, 3);
-
- % 计算色调(Hue)
- numerator = 0.5 * ((R - G) + (R - B));
- denominator = sqrt((R - G).^2 + (R - B).*(G - B));
- theta = acos(numerator ./ (denominator + eps));
- H = theta;
- H(B > G) = 2*pi - H(B > G);
- H = H / (2 * pi);
-
- % 计算饱和度(Saturation)
- S = 1 - 3 * min(min(R, G), B) ./ (R + G + B + eps);
-
- % 计算亮度(Intensity)
- I = (R + G + B) / 3;
-
- % 合并HSI通道
- hsi = cat(3, H, S, I);
-
- % 显示原始图像和HSI图像
- subplot(1, 2, 1);
- imshow(img);
- title('原始图像');
- subplot(1, 2, 2);
- imshow(hsi);
- title('HSI图像');
