MATLAB image processing _HSV and RGB color space of mutual transfer

Source: Internet
Author: User
Tags color gamut

Needless to say, there is no technical content, because the following code is the self-contained conversion function in MATLAB. Posted here just to facilitate later review, study its conversion algorithm:


HSV Space : H (Hue)--s (saturation)--v (brightness)

Similar to the HSI color space : H (Hue)--s (saturation)--I. (intensity)


Attention:

Intensity and brightness are actually a concept.

Saturation represents the amount of light that penetrates into the white light, the more white light, the smaller the saturation, the less white light, the greater the saturation, the greater the purity of the color.


Here's the code:

Rgb2hsv.m

function [H,s,v] = RGB2HSV (r,g,b)%RGB2HSV convert red-green-blue colors to hue-saturation-value.% h = RGB2HSV (M) Convert s an RGB color map to an HSV color map.% each map was a matrix with any number of rows, exactly three columns,% and ele  ments in the interval 0 to 1.  The columns of the input matrix,% M, represent intensity of red, blue and green, respectively. the% columns of the resulting output matrix, H, represent hue, saturation% and color value, respectively.%% HSV = RG B2HSV (RGB) converts the RGB image RGB (Z-array) to the% equivalent HSV image HSV (in-z array). Percent CLASS support%--- ----------% If The input is a RGB image, it can be of class uint8, uint16, or% double;  The output image is of class double. If the input is a percent colormap, the input and output colormaps is both of class double.%% see also Hsv2rgb, ColorMap, Rgbplot. % undocumented syntaxes:% [h,s,v] = RGB2HSV (r,g,b) converts the RGB image r,g,b to the% equivalent HSV image h,s,v.Percent HSV = RGB2HSV (r,g,b) converts the RGB image r,g,b to the% equivalent HSV image stored in the-Z Array (HSV). [H,s,v] = RGB2HSV (RGB) converts the RGB image RGB (in-array) to% the equivalent HSV image h,s,v.%% see Alvy Ray Smith , Color gamut Transform Pairs, SIGGRAPH ' 78.% Copyright 1984-2006 the MathWorks, Inc.% $Revision: 5.15.4.3 $ $Date:      2010/08/23 23:13:14 $switch nargin Case 1, if Isa (R, ' Uint8 '), R = Double (r)/255;     ElseIf Isa (R, ' uint16 ') R = Double (r)/65535;      End Case 3, if Isa (R, ' Uint8 '), R = Double (r)/255;     ElseIf Isa (R, ' uint16 ') R = Double (r)/65535;      End If Isa (G, ' uint8 '), G = double (g)/255;     ElseIf Isa (G, ' uint16 ') g = double (g)/65535;      End If Isa (b, ' uint8 '), B = double (b)/255;     ElseIf Isa (b, ' uint16 ') b = double (b)/65535; End otherwise, error (Message (' MATLAB:rgb2hsv:WrongInputNum ')); end threed = (Ndims (r) ==3); % determine if input includes a arrayif threed, G = R (:,:, 2); B = R (:,:, 3);  R = R (:,:, 1);  Siz = size (r);  R = R (:); g = g (:); b = B (:); ElseIf nargin==1, G = R (:, 2); B = R (:, 3); R = R (:, 1);  Siz = Size (R), else if ~isequal (size (R), size (g), size (b)), Error (Message (' MATLAB:rgb2hsv:InputSizeMismatch '));  End siz = Size (r); R = R (:); g = g (:); b = B (:); ENDV = max (r,g), b); h = zeros (size (v)); s = (V-min (min (r,g), b)); z = ~s;s = s + z;k = find (r = = v); h (k) = (g (k)-B (k))./s (k); k = find (g = = v), h (k) = 2 + (b (k)-R (k))./s (k); k = Find (b = = v); h (k) = 4 + (r (k)-G ( k)./s (k); h = h/6;k = Find (H < 0), h (k) = h (k) + 1;h= (~z). *h;k = Find (v); s (k) = (~z (k)). *s (k)./V (k); s (~v) = 0;if nargout    <=1, if (threed | | nargin==3), h = reshape (h,siz);    s = reshape (s,siz);    v = reshape (v,siz);  H=cat (3,H,S,V);  else h=[h S v];  Endelse h = reshape (h,siz);  s = reshape (s,siz); v = reshape (v,siz); end

Hsv2rgb.m

function [Rout,g,b] = Hsv2rgb (hin,s,v)%hsv2rgb Convert hue-saturation-value colors to red-green-blue.% M = Hsv2rgb (H) CO Nverts an HSV-color map to an RGB-color map.% each map was a matrix with any number of rows, exactly three columns,% an  D elements in the interval 0 to 1.  The columns of the input matrix,% H, represent hue, saturation and value, respectively. The columns of% the resulting output matrix, M, represent intensity of red, blue and% green, respectively.%% RGB = H SV2RGB (HSV) converts the HSV image HSV (in-z array) to the% equivalent RGB image rgb (Z-array). Percent as the hue varies F Rom 0 to 1, the resulting color varies from% red, through yellow, green, cyan, blue and magenta, back to red.% when th E saturation is 0, the colors is unsaturated;  They are% simply shades of gray. When the saturation is 1, the colors are% fully saturated;  They contain no white component. As the value% varies from 0 to 1, the brightness increases.%% the ColorMap HSV is Hsv2rgb ([H S v]) where H is a linear ramp% from 0 to 1 and both S and V were all 1 ' s.%% See also RGB2HSV, COLORMAP, RG bplot.% undocumented syntaxes:% [r,g,b] = Hsv2rgb (h,s,v) converts the HSV image h,s,v to the% equivalent RGB image R , g,b.%% RGB = Hsv2rgb (h,s,v) converts the HSV image h,s,v to the percent equivalent RGB image stored in the-Z array (RGB). Percent percent [r,g,b] = Hsv2rgb (HSV) converts the HSV image HSV (in array) to% the equivalent RGB image r,g,b.% see Alvy Ray S    Mith, Color gamut Transform Pairs, SIGGRAPH ' 78.% Copyright 1984-2011 the MathWorks, inc. if Nargin = = 1 HSV colormap ThreeD = Ndims (hin) ==3; % determine if input includes a, array if threed, H = Hin (:,:, 1); s = Hin (:,:, 2);    v = Hin (:,:, 3); else H = hin (:, 1); s = Hin (:, 2);    v = Hin (:, 3); Endelseif Nargin = = 3 if ~isequal (size (hin), size (s), size (v)), error (Message (' MATLAB:hsv2rgb:InputSizeMismatch '))    ; End h = Hin;else error (Message (' MATLAB:hsv2rgb:WrongInputNuM ')); end h = 6.*h;k = Floor (h);p = H-k;t = 1-s;n = 1-s.*p;p = N (s.* (1-p));% processing each value of K Separat Ely to avoid simultaneously storing% many temporary matrices the same size as k in MEMORYKC = (k==0 | k==6); r = Kc;g = kc. *p;b = KC.*T;KC = (k==1), R = r + kc.*n;g = g + kc;b = b + kc.*t;kc = (k==2); r = r + kc.*t;g = g + kc;b = b + kc.*p;kc = (k ==3); r = r + kc.*t;g = g + kc.*n;b = b + kc;kc = (k==4); r = r + kc.*p;g = g + kc.*t;b = b + kc;kc = (k==5); r = r + kc;g = G + kc.*t;b = B + kc.*n;if nargout <= 1 if Nargin = = 3 | |    ThreeD rout = Cat (3,r,g,b);    else rout = [R G B];    End rout = Bsxfun (@times, V./max (Rout (:)), rout), Else F = V./max ([Max (:)); Max (g (:)); Max (b (:))]);    rout = F.*r;    g = f.*g; b = F.*b;end


MATLAB image processing _HSV and RGB color space of mutual transfer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.