彩色模型(又称彩色空间或彩色系统)是描述色彩的一种方法,本质上,彩色模型就是坐标系统和子空间的规范,系统中的每种颜色由单个点来表示。下面介绍两种最常用的彩色模型。
一、RGB彩色模型:
RGB模型是最通用的面向设备的彩色模型,主要用于彩色显示器和彩色视频摄像机。RGB模型利用了三原色原理,即大多数颜色都可由红、绿、蓝三原色按不同比例混合构成。该模型确定的彩色子空间如下:
红、绿、蓝在3个角上,青、深红、黄在另外3个角上,黑色在原点处,白色位于离原点最远的角上;灰度等级沿着主对角线从黑色到白色分布。为了方便,上述将R、G、B值归一化至[0,1]区间。
在RGB彩色模型中,任何一幅图像都由3个图像分量组成,每个分量图像都是其原色图像。对于RGB图像,每一副红、绿、蓝图像都是8比特图像,3幅图像送至显示器时在屏幕上混合成一幅彩色图像。
二、HSI彩色模型:
HSI模型更符合人描述和解释颜色的方式,以色调(Hue)、饱和度(Saturation)和亮度(Intensity) 三个特征量来描述颜色。
色调(H):是色彩的基本属性,就是平常所说的颜色名称,如红色、黄色等;
饱和度S):是指色彩的纯度(纯光被白光稀释的程度),饱和度越高,颜色越鲜艳;
亮度(I):是一个主观的描述子,实际上不可测量,体现了无色的强度概念;
通常把色调和饱和度统称为色度,HSI模型将图像的亮度信息和色度信息分离,使得很多针对灰度图像的算法可直接作用在亮度图像上而不该变彩色图像的色度信息。
HSI模型确定的颜色空间如下:
亮度I:彩色点距离双圆锥体底端的距离,取值[0,1]。如:黑色亮度为0,白色亮度为1;
色调H:从红色轴开始色调逆时针增长,取值范围[0,2pi]。如:红色色调为0,绿色2pi/3,蓝色4pi/3。
饱和度S:从原点到彩色点半径的长度,取值[0,1]。圆周上彩色点饱和度为1(纯色),原点处饱和度为0。
三、RGB与HSI的转换:
1、 RGB到HSI的变换
首先将R、G、B被归一化至[0,1]内,则求出的S 、I也在[0,1]内,H在[0,2pi]内,若需要可将H除以2pi归一化至[0,1]。
其中
2、 HSI到RGB的变换
首先将若S、I在归一化至[0,1]内,H归一化至[0,2pi]内,求出的R、G、B值在[0,1]内,具体分三种情况:
(1)时:
(2)时:
(3)时:
四、程序:(摘自冈萨雷斯DIPUM Toolbox)
1、RGB转HIS:
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]. 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);
2、HIS转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.
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);
3、测试:
I=imread('lena.tiff');
rgb=I;
hsi = rgb2hsi(rgb);
rgb = hsi2rgb(hsi);
figure;
subplot(1,3,1);imshow(I);title('original');
subplot(1,3,2);imshow(hsi);title('rgb-->hsi');
subplot(1,3,3);imshow(rgb);title('hsi-->rgb');
版权声明:本文为博主原创文章,未经博主允许不得转载。