HSV颜色空间转 RGB 颜色空间
............折腾了半天,转换回来的图像和原图总是不对,后来发现get_light_channel的时候bug了追究半天...
https://en.wikipedia.org/wiki/HSL_and_HSV
RGB转HSV
% Function writer : EOF
% code file : RGB2SHV.m
% code date : 2014.10.19
% Translate RGB into HSV-space
%
% Code description:
% Attetion.
% The pixel value of inputed 'Image' must between 0~255;
function [H,S,V] = RGB2SHV(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);
H = zeros(Height_Image,Width_Image);
S = zeros(Height_Image,Width_Image);
R = double(Image(:,:,1));
G = double(Image(:,:,2));
B = double(Image(:,:,3));
max_value = get_light_channel(Image);
min_value = get_dark_channel(Image);
for row = 1: Height_Image
for col = 1:Width_Image
if max_value(row,col) == 0
S(row,col) = 0;
else
S(row,col) = ((max_value(row,col) - min_value(row,col))/max_value(row,col));
end
if S(row,col) == 0
H(row,col) = 0;
else
if max_value(row,col) == R(row,col)
H(row,col) = mod((G(row,col) - B(row,col))/(max_value(row,col) - min_value(row,col)),6);
end
if max_value(row,col) == G(row,col)
H(row,col) = 2 + (B(row,col) - R(row,col))/(max_value(row,col) - min_value(row,col));
end
if max_value(row,col) == B(row,col)
H(row,col) = 4 + (R(row,col) - G(row,col))/(max_value(row,col) - min_value(row,col));
end
H(row,col) = H(row,col).*60;
if H(row,col) < 0
H(row,col) = H(row,col) + 360;
end
end
end
end
V = double(max_value)./255;
end
HSV转RGB
% Function writer : EOF
% code file : HSV2RGB.m
% code date : 2014.10.19
% Translate RGB into HSV-space
%
% Code description:
% Attetion.
% The pixel value of inputed 'Image' must between 0~255; V =(0,1) S = (0,1)
function Output = HSV2RGB_Color(H,S,V)
Height_Img = size(H,1);
Width_Img = size(H,2);
C = V.*S;
H_temp = H./60;
X = C.* (1 - abs(mod(H_temp,2) - 1));
Output = zeros(Height_Img,Width_Img,3);
for row = 1:Height_Img
for col = 1 : Width_Img
if H_temp(row,col) >= 0 && H_temp(row,col) < 1
Output(row,col,1) = C(row,col);
Output(row,col,2) = X(row,col);
Output(row,col,3) = 0;
end
if H_temp(row,col) >= 1 && H_temp(row,col) < 2
Output(row,col,1) = X(row,col);
Output(row,col,2) = C(row,col);
Output(row,col,3) = 0;
end
if H_temp(row,col) >= 2 && H_temp(row,col) < 3
Output(row,col,1) = 0;
Output(row,col,2) = C(row,col);
Output(row,col,3) = X(row,col);
end
if H_temp(row,col) >= 3 && H_temp(row,col) < 4
Output(row,col,1) = 0;
Output(row,col,2) = X(row,col);
Output(row,col,3) = C(row,col);
end
if H_temp(row,col) >= 4 && H_temp(row,col) < 5
Output(row,col,1) = X(row,col);
Output(row,col,2) = 0;
Output(row,col,3) = C(row,col);
end
if H_temp(row,col) >= 5 && H_temp(row,col) < 6
Output(row,col,1) = C(row,col);
Output(row,col,2) = 0;
Output(row,col,3) = X(row,col);
end
end
end
m = V - C;
Output(:,:,1) = Output(:,:,1) + m;
Output(:,:,2) = Output(:,:,2) + m;
Output(:,:,3) = Output(:,:,3) + m;
end