RGB 颜色空间转 HSI 颜色空间的matlab程序实现
2014-10-15 21:01
525 查看
RGB 颜色空间转 HSI 颜色空间的matlab程序实现
2014.10.20之前的内容有误,这里根据wikipedia更新了算法内容. 算法以wiki为准
https://en.wikipedia.org/wiki/HSL_and_HSV
这里demo出 HSI中 S 空间的图像和暗通道图的对比.
会发现,确实右边到很暗,这是因为HSV转换的时候对RGB值做了归一化处理,如果打印出归一化处理后的R+G+B值会发现输出图像很亮(白茫茫一片~)
下图是取自图像第321列的数据分布,可以看见图像的灰度分布是很明晰的
下面给出了我写的转换函数,直接调用即可.
%%**************************************************************************
% Function writer : EOF
% code file : RGB2SHI_Color.m
% code date : 2014.10.16
% Translate RGB into HSI-space
%
% Code Description:
%
% If you want to translate a colourful Image which is coded as
% RGB colour space into HSI space, what you need to do is just input your
% colour image.
%
% This function would return HSI as a matrix [H,S,I].
%
%% *************************************************************************
function [H,S,I] = RGB2SHI_Color(Image)
if size(Image,3) ~= 3
fprintf('ERROR Imput-Image must be three channel image\n');
return;
end
Height_Image = size(Image,1);
Width_Image = size(Image,2);
Channel_Image = size(Image,3);
H = zeros(1,Height_Image * Width_Image);
H_temp = zeros(1,Height_Image * Width_Image);
S = zeros(1,Height_Image * Width_Image);
I = zeros(1,Height_Image * Width_Image);
%% Normalization into (0,1)
R_temp = double(Image(:,:,1));
G_temp = double(Image(:,:,2));
B_temp = double(Image(:,:,3));
R = R_temp./(R_temp + G_temp + B_temp);
G = G_temp./(R_temp + G_temp + B_temp);
B = B_temp./(R_temp + G_temp + B_temp);
Max_channel = max(max(R,G),B);
Min_channel = min(min(R,G),B);
Difference = Max_channel - Min_channel;
I = (R + G + B)./3;
for row = 1:Height_Image
for col = 1: Width_Image
% In fact , if Difference(row,col) is zero, the H_temp is
% undefine , it means that H_temp(row,col) close to
% infinite.
if Difference(row,col) == 0
H_temp(row,col) = 0;
end
if Max_channel(row,col) == R(row,col)
H_temp(row,col) = mod((G(row,col) - B(row,col)) ...
./Difference(row,col), 6 );
end
if Max_channel(row,col) == G(row,col)
H_temp(row,col) = (B(row,col) - R(row,col)) ...
./Difference(row,col) + 2;
end
if Max_channel(row,col) == B(row,col)
H_temp(row,col) = (B(row,col) - R(row,col)) ...
./Difference(row,col) + 4;
end
H(row,col) = H_temp(row,col)*60;
if I(row,col) == 0
S(row,col) = 0;
else
S(row,col) = 1 - (Min_channel(row,col)./I(row,col));
end
end
end
end
2014.10.20之前的内容有误,这里根据wikipedia更新了算法内容. 算法以wiki为准
https://en.wikipedia.org/wiki/HSL_and_HSV
这里demo出 HSI中 S 空间的图像和暗通道图的对比.
会发现,确实右边到很暗,这是因为HSV转换的时候对RGB值做了归一化处理,如果打印出归一化处理后的R+G+B值会发现输出图像很亮(白茫茫一片~)
下图是取自图像第321列的数据分布,可以看见图像的灰度分布是很明晰的
下面给出了我写的转换函数,直接调用即可.
%%**************************************************************************
% Function writer : EOF
% code file : RGB2SHI_Color.m
% code date : 2014.10.16
% Translate RGB into HSI-space
%
% Code Description:
%
% If you want to translate a colourful Image which is coded as
% RGB colour space into HSI space, what you need to do is just input your
% colour image.
%
% This function would return HSI as a matrix [H,S,I].
%
%% *************************************************************************
function [H,S,I] = RGB2SHI_Color(Image)
if size(Image,3) ~= 3
fprintf('ERROR Imput-Image must be three channel image\n');
return;
end
Height_Image = size(Image,1);
Width_Image = size(Image,2);
Channel_Image = size(Image,3);
H = zeros(1,Height_Image * Width_Image);
H_temp = zeros(1,Height_Image * Width_Image);
S = zeros(1,Height_Image * Width_Image);
I = zeros(1,Height_Image * Width_Image);
%% Normalization into (0,1)
R_temp = double(Image(:,:,1));
G_temp = double(Image(:,:,2));
B_temp = double(Image(:,:,3));
R = R_temp./(R_temp + G_temp + B_temp);
G = G_temp./(R_temp + G_temp + B_temp);
B = B_temp./(R_temp + G_temp + B_temp);
Max_channel = max(max(R,G),B);
Min_channel = min(min(R,G),B);
Difference = Max_channel - Min_channel;
I = (R + G + B)./3;
for row = 1:Height_Image
for col = 1: Width_Image
% In fact , if Difference(row,col) is zero, the H_temp is
% undefine , it means that H_temp(row,col) close to
% infinite.
if Difference(row,col) == 0
H_temp(row,col) = 0;
end
if Max_channel(row,col) == R(row,col)
H_temp(row,col) = mod((G(row,col) - B(row,col)) ...
./Difference(row,col), 6 );
end
if Max_channel(row,col) == G(row,col)
H_temp(row,col) = (B(row,col) - R(row,col)) ...
./Difference(row,col) + 2;
end
if Max_channel(row,col) == B(row,col)
H_temp(row,col) = (B(row,col) - R(row,col)) ...
./Difference(row,col) + 4;
end
H(row,col) = H_temp(row,col)*60;
if I(row,col) == 0
S(row,col) = 0;
else
S(row,col) = 1 - (Min_channel(row,col)./I(row,col));
end
end
end
end
相关文章推荐
- RGB 颜色空间转 HSI 颜色空间的matlab程序实现
- Python实现RGB与HSI颜色空间的互换
- matlab实现RGB与HSV(HSB)、HSL和HSI的色彩空间互转
- MATLAB自定义函数:RGB与HSI颜色空间互换
- OpenCV实现RGB颜色空间和HSI颜色空间的相互转换
- 使用OpenCV实现RGB、HSI、CMYK颜色空间的转换
- OpenCV实现RGB颜色空间和HSI颜色空间的相互转换
- OpenCV实现RGB颜色空间和HSI颜色空间的相互转换
- OpenCV实现RGB颜色空间和HSI颜色空间的相互转换
- RGB与HSI颜色空间互换函数(matlab)
- OpenCV实现RGB颜色空间和HSI颜色空间的相互转换
- RGB、HSV、HSI颜色空间
- RGB空间与HSI空间的转换算法的C#实现
- MATLAB图像处理_YUV与RGB颜色空间互转
- QT 实现彩色图亮度均衡,RGB和HSI空间互相转换
- 数字图像处理 颜色空间RGB、HSI、CMYK、YUV的相互转换
- 用c++实现颜色空间rgb,grey,luv和lab的互转
- 基于Tkinter利用python实现颜色空间转换程序
- MATLAB图像处理_HSV与RGB颜色空间互转
- YUV RGB HSV HSI CMYK HSL等各种颜色空间