paper 74:MATLAB图像处理_HSV与RGB颜色空间互转
HSV空间:分别是H(色调)——S(饱和度)——V(亮度)
与HSI颜色空间类似:分别是H(色调)——S(饱和度)——I(强度)
注意:
强度和亮度差不多是一个概念。
饱和度代表的是渗入白光的数量级,白光越多,饱和度越小,白光越少,饱和度越大,表示颜色的纯度更大。
下面是代码:
rgb2hsv.m
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
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
paper 74:MATLAB图像处理_HSV与RGB颜色空间互转的更多相关文章
- RGB 颜色空间转 HSI 颜色空间的matlab程序实现
RGB 颜色空间转 HSI 颜色空间的matlab程序实现 2014.10.20之前的内容有误,这里依据wikipedia更新了算法内容. 算法以wiki为准 https://en.wikipedia ...
- MATLAB图像处理函数汇总(二)
60.imnoise 功能:增加图像的渲染效果. 语法: J = imnoise(I,type) J = imnoise(I,type,parameters) 举例 I = imread('eight ...
- MATLAB图像处理函数汇总(一)
1.applylut功能: 在二进制图像中利用lookup表进行边沿操作.语法:A = applylut(BW,lut)举例lut = makelut('sum(x(:)) == 4',2);BW1 ...
- matlab图像处理
matlab图像处理 转自:http://www.cnblogs.com/lovebay/p/5094146.html 1. 图像和图像数据 缺省情况下,MATLAB将图像中的数据存储为双精度类型(d ...
- 学习笔记(2)---Matlab 图像处理相关函数命令大全
Matlab 图像处理相关函数命令大全 一.通用函数: colorbar 显示彩色条 语法:colorbar \ colorbar('vert') \ colorbar('horiz') \ col ...
- MATLAB图像处理工具箱
下列表格中除了个别函数外,其余函数都是图像处理工具箱提供的关于图像处理的函数,现摘录到此以备查找. 表1 图像显示 函数名 功能说明 函数名 功能说明 colorbar 颜色条显示 montage 按 ...
- MATLAB图像处理基础
MATLAB图像处理基础 2.2.1 图像文件格式及图像类型 1.MATLAB支持的几种图像文件格式: ⑴JPEG(Joint Photogyaphic Expeyts Group):一种称为联合图像 ...
- Matlab图像处理(01)-Matlab基础
枫竹梦对于Matlab几乎是零基础,只是在上学的时候稍稍接触一点,万万没有想到现在还能用到Matlab.进入正题>>> 图像的基本概念 一幅图像可以被定义为一个二维函数f(x,y), ...
- Matlab图像处理相关
相关函数: 读取:imread() %参数为文件名(路径)或url,格式等 写入:imwrite() %参数为写入数据矩阵,写入文件名(路径),格式等 显示:imshow() %显示由输入决定,属性自 ...
随机推荐
- Let's Encrypt这个免费的证书签发服务
使用的是Let's Encrypt这个免费的证书签发服务,按照这里的教程一步步照着来,很快就完成了. 迁移过程总体来说比较顺利,只是遇到了两个不大不小的坑.一个是域名的跳转问题,迁移完成以后对于所有h ...
- "rel=nofollow"属性简介
nofollow是HTML元标签(meta)的content属性和链接标签(a)的rel属性的一个值,告诉机器(爬虫)无需追踪目标页,为了对抗blogspam(博客垃圾留言信息),Google推荐使用 ...
- jquery.autocomplete.js 插件的自定义搜索规则
这二天开始用jquery.autocomplete这个自动完成插件.功能基本比较强大,但自己在实际需求中发现还是有一处不足!问题是这样:当我定义了一个本地数据JS文件时,格式为JSON式的数组.如下: ...
- NGINX原理分析 之 SLAB分配机制
1 引言 众所周知,操作系统使用伙伴系统管理内存,不仅会造成大量的内存碎片,同时处理效率也较低下.SLAB是一种内存管理机制,其拥有较高的处理效率,同时也 有效的避免内存碎片的产生,其核心思想是预分配 ...
- Surround the Trees---hdu1392(凸包GraHam模板)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1392 题意:有n棵树,每棵树有一个坐标,想用一些绳子把这些树包含起来,求需要绳子的长度: 就是求凸包的 ...
- css中的盒子模型
css中的盒子模型 css中的盒子模型,有两种,一种是“标准 W3C 盒子模型”,另外一种是IE盒子模型. 1.w3c盒子模型 从图中可以看出:w3c盒子模型的范围包括了:margin,borde ...
- 【长期有效】1分钱抢100M上网流量及iPhone5S免费抽奖
声明:本活动经作者亲自体验,绝对真实.安全.可靠.下文中图片为手机界面截图,最终得到官方手机流量增加100M的短信通知,回家过年使用最合适! 0.点击下方活动图片,进入“百度钱包”页面,你也可以直接 ...
- Oracle ORA-12519: TNS:no appropriate service handler found 解决
有时候连得上数据库,有时候又连不上. 可能是数据库上当前的连接数目已经超过了它能够处理的最大值. select count(*) from v$process --当前的连接数select value ...
- 8月17日 Power-BI关于全国房地产开发投资情况分析 QQ群视频交流开课啦
<ignore_js_op> 数读|中国的经济只剩下房地产了么? 引言: 近日一则标题为“房奴们又立功啦,7月份新增贷款几乎都来自房贷!”的报道吸引了大众的目光.该报道指出在央行8月13日 ...
- python_序列
1. python存在6中内建序列:列表.元组.字符串.Unicode字符串.buffer对象.xrange对象 列表可以修改,元组和字符串不可以修改. 2. 序列支持的操作: 索引 序列中所有的元素 ...