HSV空间:分别是H(色调)——S(饱和度)——V(亮度)

与HSI颜色空间类似:分别是H(色调)——S(饱和度)——I(强度)

注意:

强度和亮度差不多是一个概念。

饱和度代表的是渗入白光的数量级,白光越多,饱和度越小,白光越少,饱和度越大,表示颜色的纯度更大。

下面是代码:

rgb2hsv.m

  1. function [h,s,v] = rgb2hsv(r,g,b)
  2. %RGB2HSV Convert red-green-blue colors to hue-saturation-value.
  3. % H = RGB2HSV(M) converts an RGB color map to an HSV color map.
  4. % Each map is a matrix with any number of rows, exactly three columns,
  5. % and elements in the interval 0 to 1. The columns of the input matrix,
  6. % M, represent intensity of red, blue and green, respectively. The
  7. % columns of the resulting output matrix, H, represent hue, saturation
  8. % and color value, respectively.
  9. %
  10. % HSV = RGB2HSV(RGB) converts the RGB image RGB (3-D array) to the
  11. % equivalent HSV image HSV (3-D array).
  12. %
  13. % CLASS SUPPORT
  14. % -------------
  15. % If the input is an RGB image, it can be of class uint8, uint16, or
  16. % double; the output image is of class double. If the input is a
  17. % colormap, the input and output colormaps are both of class double.
  18. %
  19. % See also HSV2RGB, COLORMAP, RGBPLOT.
  20.  
  21. % Undocumented syntaxes:
  22. % [H,S,V] = RGB2HSV(R,G,B) converts the RGB image R,G,B to the
  23. % equivalent HSV image H,S,V.
  24. %
  25. % HSV = RGB2HSV(R,G,B) converts the RGB image R,G,B to the
  26. % equivalent HSV image stored in the 3-D array (HSV).
  27. %
  28. % [H,S,V] = RGB2HSV(RGB) converts the RGB image RGB (3-D array) to
  29. % the equivalent HSV image H,S,V.
  30. %
  31. % See Alvy Ray Smith, Color Gamut Transform Pairs, SIGGRAPH '78.
  32.  
  33. % Copyright 1984-2006 The MathWorks, Inc.
  34. % $Revision: 5.15.4.3 $ $Date: 2010/08/23 23:13:14 $
  35.  
  36. switch nargin
  37. case 1,
  38. if isa(r, 'uint8'),
  39. r = double(r) / 255;
  40. elseif isa(r, 'uint16')
  41. r = double(r) / 65535;
  42. end
  43. case 3,
  44. if isa(r, 'uint8'),
  45. r = double(r) / 255;
  46. elseif isa(r, 'uint16')
  47. r = double(r) / 65535;
  48. end
  49.  
  50. if isa(g, 'uint8'),
  51. g = double(g) / 255;
  52. elseif isa(g, 'uint16')
  53. g = double(g) / 65535;
  54. end
  55.  
  56. if isa(b, 'uint8'),
  57. b = double(b) / 255;
  58. elseif isa(b, 'uint16')
  59. b = double(b) / 65535;
  60. end
  61.  
  62. otherwise,
  63. error(message('MATLAB:rgb2hsv:WrongInputNum'));
  64. end
  65.  
  66. threeD = (ndims(r)==3); % Determine if input includes a 3-D array
  67.  
  68. if threeD,
  69. g = r(:,:,2); b = r(:,:,3); r = r(:,:,1);
  70. siz = size(r);
  71. r = r(:); g = g(:); b = b(:);
  72. elseif nargin==1,
  73. g = r(:,2); b = r(:,3); r = r(:,1);
  74. siz = size(r);
  75. else
  76. if ~isequal(size(r),size(g),size(b)),
  77. error(message('MATLAB:rgb2hsv:InputSizeMismatch'));
  78. end
  79. siz = size(r);
  80. r = r(:); g = g(:); b = b(:);
  81. end
  82.  
  83. v = max(max(r,g),b);
  84. h = zeros(size(v));
  85. s = (v - min(min(r,g),b));
  86.  
  87. z = ~s;
  88. s = s + z;
  89. k = find(r == v);
  90. h(k) = (g(k) - b(k))./s(k);
  91. k = find(g == v);
  92. h(k) = 2 + (b(k) - r(k))./s(k);
  93. k = find(b == v);
  94. h(k) = 4 + (r(k) - g(k))./s(k);
  95. h = h/6;
  96. k = find(h < 0);
  97. h(k) = h(k) + 1;
  98. h=(~z).*h;
  99.  
  100. k = find(v);
  101. s(k) = (~z(k)).*s(k)./v(k);
  102. s(~v) = 0;
  103.  
  104. if nargout<=1,
  105. if (threeD || nargin==3),
  106. h = reshape(h,siz);
  107. s = reshape(s,siz);
  108. v = reshape(v,siz);
  109. h=cat(3,h,s,v);
  110. else
  111. h=[h s v];
  112. end
  113. else
  114. h = reshape(h,siz);
  115. s = reshape(s,siz);
  116. v = reshape(v,siz);
  117. end

  

  1. function [rout,g,b] = hsv2rgb(hin,s,v)
  2. %HSV2RGB Convert hue-saturation-value colors to red-green-blue.
  3. % M = HSV2RGB(H) converts an HSV color map to an RGB color map.
  4. % Each map is a matrix with any number of rows, exactly three columns,
  5. % and elements in the interval 0 to 1. The columns of the input matrix,
  6. % H, represent hue, saturation and value, respectively. The columns of
  7. % the resulting output matrix, M, represent intensity of red, blue and
  8. % green, respectively.
  9. %
  10. % RGB = HSV2RGB(HSV) converts the HSV image HSV (3-D array) to the
  11. % equivalent RGB image RGB (3-D array).
  12. %
  13. % As the hue varies from 0 to 1, the resulting color varies from
  14. % red, through yellow, green, cyan, blue and magenta, back to red.
  15. % When the saturation is 0, the colors are unsaturated; they are
  16. % simply shades of gray. When the saturation is 1, the colors are
  17. % fully saturated; they contain no white component. As the value
  18. % varies from 0 to 1, the brightness increases.
  19. %
  20. % The colormap HSV is hsv2rgb([h s v]) where h is a linear ramp
  21. % from 0 to 1 and both s and v are all 1's.
  22. %
  23. % See also RGB2HSV, COLORMAP, RGBPLOT.
  24.  
  25. % Undocumented syntaxes:
  26. % [R,G,B] = HSV2RGB(H,S,V) converts the HSV image H,S,V to the
  27. % equivalent RGB image R,G,B.
  28. %
  29. % RGB = HSV2RGB(H,S,V) converts the HSV image H,S,V to the
  30. % equivalent RGB image stored in the 3-D array (RGB).
  31. %
  32. % [R,G,B] = HSV2RGB(HSV) converts the HSV image HSV (3-D array) to
  33. % the equivalent RGB image R,G,B.
  34.  
  35. % See Alvy Ray Smith, Color Gamut Transform Pairs, SIGGRAPH '78.
  36. % Copyright 1984-2011 The MathWorks, Inc.
  37.  
  38. if nargin == 1 % HSV colormap
  39. threeD = ndims(hin)==3; % Determine if input includes a 3-D array
  40. if threeD,
  41. h = hin(:,:,1); s = hin(:,:,2); v = hin(:,:,3);
  42. else
  43. h = hin(:,1); s = hin(:,2); v = hin(:,3);
  44. end
  45. elseif nargin == 3
  46. if ~isequal(size(hin),size(s),size(v)),
  47. error(message('MATLAB:hsv2rgb:InputSizeMismatch'));
  48. end
  49. h = hin;
  50. else
  51. error(message('MATLAB:hsv2rgb:WrongInputNum'));
  52. end
  53.  
  54. h = 6.*h;
  55. k = floor(h);
  56. p = h-k;
  57. t = 1-s;
  58. n = 1-s.*p;
  59. p = 1-(s.*(1-p));
  60.  
  61. % Processing each value of k separately to avoid simultaneously storing
  62. % many temporary matrices the same size as k in memory
  63. kc = (k==0 | k==6);
  64. r = kc;
  65. g = kc.*p;
  66. b = kc.*t;
  67.  
  68. kc = (k==1);
  69. r = r + kc.*n;
  70. g = g + kc;
  71. b = b + kc.*t;
  72.  
  73. kc = (k==2);
  74. r = r + kc.*t;
  75. g = g + kc;
  76. b = b + kc.*p;
  77.  
  78. kc = (k==3);
  79. r = r + kc.*t;
  80. g = g + kc.*n;
  81. b = b + kc;
  82.  
  83. kc = (k==4);
  84. r = r + kc.*p;
  85. g = g + kc.*t;
  86. b = b + kc;
  87.  
  88. kc = (k==5);
  89. r = r + kc;
  90. g = g + kc.*t;
  91. b = b + kc.*n;
  92.  
  93. if nargout <= 1
  94. if nargin == 3 || threeD
  95. rout = cat(3,r,g,b);
  96. else
  97. rout = [r g b];
  98. end
  99. rout = bsxfun(@times, v./max(rout(:)), rout);
  100. else
  101. f = v./max([max(r(:)); max(g(:)); max(b(:))]);
  102. rout = f.*r;
  103. g = f.*g;
  104. b = f.*b;
  105. end

  

paper 74:MATLAB图像处理_HSV与RGB颜色空间互转的更多相关文章

  1. RGB 颜色空间转 HSI 颜色空间的matlab程序实现

    RGB 颜色空间转 HSI 颜色空间的matlab程序实现 2014.10.20之前的内容有误,这里依据wikipedia更新了算法内容. 算法以wiki为准 https://en.wikipedia ...

  2. MATLAB图像处理函数汇总(二)

    60.imnoise 功能:增加图像的渲染效果. 语法: J = imnoise(I,type) J = imnoise(I,type,parameters) 举例 I = imread('eight ...

  3. MATLAB图像处理函数汇总(一)

    1.applylut功能: 在二进制图像中利用lookup表进行边沿操作.语法:A = applylut(BW,lut)举例lut = makelut('sum(x(:)) == 4',2);BW1 ...

  4. matlab图像处理

    matlab图像处理 转自:http://www.cnblogs.com/lovebay/p/5094146.html 1. 图像和图像数据 缺省情况下,MATLAB将图像中的数据存储为双精度类型(d ...

  5. 学习笔记(2)---Matlab 图像处理相关函数命令大全

    Matlab 图像处理相关函数命令大全 一.通用函数: colorbar  显示彩色条 语法:colorbar \ colorbar('vert') \ colorbar('horiz') \ col ...

  6. MATLAB图像处理工具箱

    下列表格中除了个别函数外,其余函数都是图像处理工具箱提供的关于图像处理的函数,现摘录到此以备查找. 表1 图像显示 函数名 功能说明 函数名 功能说明 colorbar 颜色条显示 montage 按 ...

  7. MATLAB图像处理基础

    MATLAB图像处理基础 2.2.1 图像文件格式及图像类型 1.MATLAB支持的几种图像文件格式: ⑴JPEG(Joint Photogyaphic Expeyts Group):一种称为联合图像 ...

  8. Matlab图像处理(01)-Matlab基础

    枫竹梦对于Matlab几乎是零基础,只是在上学的时候稍稍接触一点,万万没有想到现在还能用到Matlab.进入正题>>> 图像的基本概念 一幅图像可以被定义为一个二维函数f(x,y), ...

  9. Matlab图像处理相关

    相关函数: 读取:imread() %参数为文件名(路径)或url,格式等 写入:imwrite() %参数为写入数据矩阵,写入文件名(路径),格式等 显示:imshow() %显示由输入决定,属性自 ...

随机推荐

  1. Linus:利用二级指针删除单向链表

    Linus大神在slashdot上回答一些编程爱好者的提问,其中一个人问他什么样的代码是他所喜好的,大婶表述了自己一些观点之后,举了一个指针的例子,解释了什么才是core low-level codi ...

  2. zepto源码--isEmptyObject,isNumeric,inArray,trim--学习笔记

    1.isEmptyObject,判断对象是否为空对象的函数 定义变量name,遍历传入对象的属性name,如果存在任何属性,则返回false,判定传入的参数为非空对象,否则即为空对象. 2.isNum ...

  3. Silverlight页面通过继承扩展实现

    在Silverlight中有些项目对UserControl重新做了封装基类,如PageBase,要求项目中每个页面都要从PageBase派生,但是过程比较坎坷,本文针对这个功能点的实现以及实现过程中遇 ...

  4. git push错误解决方案

    错误提示: error: The requested URL returned error: 403 Forbidden while accessing https://nanfei9330@gith ...

  5. Github简明教程(转)

    原文地址 : http://wuyuans.com/2012/05/github-simple-tutorial/ github是一个基于git的代码托管平台,付费用户可以建私人仓库,我们一般的免费用 ...

  6. Xcode --自动注释插件VVDocumenter-Xcode(配置须知)

    VVDocumenter-Xcode 是由 @onevcat 喵神开发的一个Xcode插件,其作用是在Xcode中输入"///"后自动生成规范的文档注释,的确非常好用而且实用. 百 ...

  7. 5分钟弄懂Docker!

    http://www.csdn.net/article/2014-07-02/2820497-what%27s-docker 关注点:1.DOCKER和VM的架构区别 2.Docker 的容器利用了  ...

  8. Java Main Differences between Java and C++

    转载自:http://www.cnblogs.com/springfor/p/4036739.html C++ supports pointers whereas Java does not. But ...

  9. SQLServer DMV Query

    1.DMV Query to identify all active SQL Server Sessions The query below identifies all currently acti ...

  10. 微信支付开发(3) JS API支付

    由于微信支付接口更新,本文档已过期,请查看新版微信支付教程.地址 http://www.cnblogs.com/txw1958/category/624506.html 本文介绍如何使用JS API支 ...