RGB颜色空间
经过详细的实验结果验证,人眼中的600~700万个锥状细胞可分为3个主要的感知类别,分别对应于红色,绿色,蓝色。大约65%的
锥状细胞对红光敏感,33%对绿光敏感,只有2%对蓝光敏感(但是蓝色锥状细胞对蓝光更敏感)。
下图显示了人眼中的红色,绿色和蓝色锥状细胞吸收光的平均实验曲线。由于人眼的这些吸收特性,所看到的彩色是所谓的原色红
(R)、绿(G)、蓝(B)的各种组合。
RGB颜色空间,采用了笛卡尔坐标系,如下图所示:
HIS颜色空间
HIS模型反应了人的视觉系统观察彩色的方式,使用非常接近于人对彩色感知的方式来定义彩色。对于图像处理来说,这种模型的优
势在于将颜色信息和灰度信息分开了。色调(Hue)分量是描述一种纯色的颜色属性(如红色,绿色,黄色),饱和度(Saturation)分量是一
种纯色被白光稀释的程度的度量,也可以理解为颜色的浓淡程度(如深红色,淡绿色),亮度(Instensity)分量描述颜色的亮暗程度。这个
模型的建立基于以下两个重要事实:
1、I分量与图像的色彩信息无关;
2、 H和S分量与人感受颜色的方式紧密相连。
HIS颜色空间,如下图所示:
正因为在HIS彩色空间中亮度和色度是互相分离的,所以在彩色图像分割应用中具有较大优势。但是目前很少有硬件设备支持这种彩
色模型,因此需要从其他颜色空间进行转换。
以下是几种常用的RGB-HSI转换公式:
以下是对这几种转换方式的对比:
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);
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);