找回密码
 注册
关于网站域名变更的通知
查看: 477|回复: 1
打印 上一主题 下一主题

用MATLAB图像处理功能实现HSV与RGB颜色的空间互转

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2019-12-9 11:22 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

EDA365欢迎您登录!

您需要 登录 才可以下载或查看,没有帐号?注册

x
5 E/ t: y' k. X8 m% B
     废话不多说,没什么技术含量,因为下面的代码是matlab中自带的转换函数。在这里贴出来只是为了方便以后复习、研究其转换的算法:$ u6 i, D9 S- {7 ~3 d# ^' S% x
% f/ Y  P  ^- }# Q' @6 _
HSV空间:分别是H(色调)——S(饱和度)——V(亮度)& J4 O0 R  Q) T
" L8 L) G3 g% V& H, J) g
与HSI颜色空间类似:分别是H(色调)——S(饱和度)——I(强度); a, `: N. B0 K0 O8 P7 |3 _% S3 o+ l. |
& e% O+ Q+ `' i# p3 u. j3 p6 r" Y
注意:
2 i  I: v) Y6 E4 y6 M/ \" F. s* |) x$ y8 Z3 W! W# l  P* a
   强度和亮度差不多是一个概念。4 ]5 j1 P/ T+ i0 D: M% B! |$ y3 z* G
* P6 w0 @$ E3 \6 T8 z: I: a
   饱和度代表的是渗入白光的数量级,白光越多,饱和度越小,白光越少,饱和度越大,表示颜色的纯度更大。7 r# u( q9 T/ S1 d' |* V

4 V- V, ~# X' [/ A1 N9 n下面是代码:
7 t8 M/ K" d) ^. Q5 w, p8 G4 S5 |5 P: ^1 o: o( ^6 @3 m4 p
rgb2hsv.m8 r* N% _; o1 f0 G+ ^# `

5 v& U' J9 i3 c7 ?
  • function [h,s,v] = rgb2hsv(r,g,b)
  • %RGB2HSV Convert red-green-blue colors to hue-saturation-value.
  • %   H = RGB2HSV(M) converts an RGB color map to an HSV color map.
  • %   Each map is a matrix with any number of rows, exactly three columns,
  • %   and elements 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 = RGB2HSV(RGB) converts the RGB image RGB (3-D array) to the
  • %   equivalent HSV image HSV (3-D array).
  • %
  • %   CLASS SUPPORT
  • %   -------------
  • %   If the input is an RGB image, it can be of class uint8, uint16, or
  • %   double; the output image is of class double.  If the input is a
  • %   colormap, the input and output colormaps are 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.
  • %
  • %   HSV = RGB2HSV(R,G,B) converts the RGB image R,G,B to the
  • %   equivalent HSV image stored in the 3-D array (HSV).
  • %
  • %   [H,S,V] = RGB2HSV(RGB) converts the RGB image RGB (3-D 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 3-D array
  • if 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(:);
  • end
  • v = max(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];
  •   end
  • else
  •   h = reshape(h,siz);
  •   s = reshape(s,siz);
  •   v = reshape(v,siz);
  • end

  • # `4 P8 {0 Y! S+ e, e+ d- F
         % r0 e: K5 Y5 S9 O) F" c
  h" k+ n6 L3 r8 X0 t0 B/ u

( Q3 S9 g1 Q9 Q" ]7 r! vhsv2rgb.m
3 E/ f% \* V" y" L: A+ U! y$ G; f( H. B  S' I8 ^% H
  • function [rout,g,b] = hsv2rgb(hin,s,v)
  • %HSV2RGB Convert hue-saturation-value colors to red-green-blue.
  • %   M = HSV2RGB(H) converts an HSV color map to an RGB color map.
  • %   Each map is a matrix with any number of rows, exactly three columns,
  • %   and 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 = HSV2RGB(HSV) converts the HSV image HSV (3-D array) to the
  • %   equivalent RGB image RGB (3-D array).
  • %
  • %   As the hue varies from 0 to 1, the resulting color varies from
  • %   red, through yellow, green, cyan, blue and magenta, back to red.
  • %   When the saturation is 0, the colors are 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 are all 1's.
  • %
  • %   See also RGB2HSV, COLORMAP, RGBPLOT.
  • %   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
  • %   equivalent RGB image stored in the 3-D array (RGB).
  • %
  • %   [R,G,B] = HSV2RGB(HSV) converts the HSV image HSV (3-D array) to
  • %   the equivalent RGB image R,G,B.
  • %   See Alvy Ray Smith, 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 3-D array
  •     if threeD,
  •         h = hin(:,:,1); s = hin(:,:,2); v = hin(:,:,3);
  •     else
  •         h = hin(:,1); s = hin(:,2); v = hin(:,3);
  •     end
  • elseif 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 = 1-(s.*(1-p));
  • % Processing each value of k separately to avoid simultaneously storing
  • % many temporary matrices the same size as k in memory
  • kc = (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(r(:)); max(g(:)); max(b(:))]);
  •     rout = f.*r;
  •     g = f.*g;
  •     b = f.*b;
  • end

  • 5 p. F: ?4 T% n6 S% T/ ^  b; w
         
) J( _* B; r3 F4 I8 {" Q6 k; I7 d
: k  S& i3 h2 d1 Z; u

该用户从未签到

2#
发表于 2019-12-9 19:03 | 只看该作者
看看,谢谢分享。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

推荐内容上一条 /1 下一条

EDA365公众号

关于我们|手机版|EDA365电子论坛网 ( 粤ICP备18020198号-1 )

GMT+8, 2025-11-24 03:18 , Processed in 0.125000 second(s), 23 queries , Gzip On.

深圳市墨知创新科技有限公司

地址:深圳市南山区科技生态园2栋A座805 电话:19926409050

快速回复 返回顶部 返回列表