Sunteți pe pagina 1din 7

function rgb = hsi2rgb(hsi)

%HSI2RGB Converts an HSI image to RGB.


%
RGB = HSI2RGB(HSI) converts an HSI image to RGB, where HSI is
%
assumed to be of class double with:
%
hsi(:, :, 1) = hue image, assumed to be in the range
%
[0, 1] by having been divided by 2*pi.
%
hsi(:, :, 2) = saturation image, in the range [0, 1].
%
hsi(:, :, 3) = intensity image, in the range [0, 1].
%
%
The components of the output image are:
%
rgb(:, :, 1) = red.
%
rgb(:, :, 2) = green.
%
rgb(:, :, 3) = blue.
%
%
%

Copyright 2002-2004 R. C. Gonzalez, R. E. Woods, & S. L. Eddins


Digital Image Processing Using MATLAB, Prentice-Hall, 2004
$Revision: 1.5 $ $Date: 2003/10/13 01:01:06 $

%
H
S
I

Extract the
= hsi(:, :,
= hsi(:, :,
= hsi(:, :,

individual HSI component images.


1) * 2 * pi;
2);
3);

%
R
G
B

Implement the conversion equations.


= zeros(size(hsi, 1), size(hsi, 2));
= zeros(size(hsi, 1), size(hsi, 2));
= zeros(size(hsi, 1), size(hsi, 2));

% RG sector (0 <= H < 2*pi/3).


idx = find( (0 <= H) & (H < 2*pi/3));
B(idx) = I(idx) .* (1 - S(idx));
R(idx) = I(idx) .* (1 + S(idx) .* cos(H(idx)) ./ ...
cos(pi/3 - H(idx)));
G(idx) = 3*I(idx) - (R(idx) + B(idx));
% BG sector (2*pi/3 <= H < 4*pi/3).
idx = find( (2*pi/3 <= H) & (H < 4*pi/3) );
R(idx) = I(idx) .* (1 - S(idx));
G(idx) = I(idx) .* (1 + S(idx) .* cos(H(idx) - 2*pi/3) ./ ...
cos(pi - H(idx)));
B(idx) = 3*I(idx) - (R(idx) + G(idx));
% BR sector.
idx = find( (4*pi/3 <= H) & (H <= 2*pi));
G(idx) = I(idx) .* (1 - S(idx));
B(idx) = I(idx) .* (1 + S(idx) .* cos(H(idx) - 4*pi/3) ./ ...
cos(5*pi/3 - H(idx)));
R(idx) = 3*I(idx) - (G(idx) + B(idx));
% Combine all three results into an RGB image. Clip to [0, 1] to
% compensate for floating-point arithmetic rounding effects.
rgb = cat(3, R, G, B);
rgb = max(min(rgb, 1), 0);

function rgb = hsi2rgb(hsi)


%HSI2RGB Converts an HSI image to RGB.
% RGB = HSI2RGB(HSI) converts an HSI image to RGB, where HSI is
% assumed to be of class double with:
%
hsi(:, :, 1) = hue image, assumed to be in the range
%
[0, 1] by having been divided by 2*pi.
%
hsi(:, :, 2) = saturation image, in the range [0, 1].
%
hsi(:, :, 3) = intensity image, in the range [0, 1].
%
% The components of the output image are:
%
rgb(:, :, 1) = red.
%
rgb(:, :, 2) = green.
%
rgb(:, :, 3) = blue.
% Copyright 2002-2004 R. C. Gonzalez, R. E. Woods, & S. L. Eddins
% Digital Image Processing Using MATLAB, Prentice-Hall, 2004
% $Revision: 1.5 $ $Date: 2003/10/13 01:01:06 $
% Extract the individual HSI component images.
H = hsi(:, :, 1) * 2 * pi;
S = hsi(:, :, 2);
I = hsi(:, :, 3);
% Implement the conversion equations.
R = zeros(size(hsi, 1), size(hsi, 2));
G = zeros(size(hsi, 1), size(hsi, 2));
B = zeros(size(hsi, 1), size(hsi, 2));
% RG sector (0 <= H < 2*pi/3).
idx = find( (0 <= H) & (H < 2*pi/3));
B(idx) = I(idx) .* (1 - S(idx));
R(idx) = I(idx) .* (1 + S(idx) .* cos(H(idx)) ./ ...
cos(pi/3 - H(idx)));
G(idx) = 3*I(idx) - (R(idx) + B(idx));
% BG sector (2*pi/3 <= H < 4*pi/3).
idx = find( (2*pi/3 <= H) & (H < 4*pi/3) );
R(idx) = I(idx) .* (1 - S(idx));
G(idx) = I(idx) .* (1 + S(idx) .* cos(H(idx) - 2*pi/3) ./ ...
cos(pi - H(idx)));
B(idx) = 3*I(idx) - (R(idx) + G(idx));
% BR sector.
idx = find( (4*pi/3 <= H) & (H <= 2*pi));
G(idx) = I(idx) .* (1 - S(idx));

B(idx) = I(idx) .* (1 + S(idx) .* cos(H(idx) - 4*pi/3) ./ ...


cos(5*pi/3 - H(idx)));
R(idx) = 3*I(idx) - (G(idx) + B(idx));
% Combine all three results into an RGB image. Clip to [0, 1] to
% compensate for floating-point arithmetic rounding effects.
rgb = cat(3, R, G, B);
rgb = max(min(rgb, 1), 0);

%RGB2HSI
Transform RGB to HSI color space
%
%
[h s i] = rgb2hsi(r, g, b)
%
[h s i] = rgb2hsi(rgb)
%
%
Convert (r,g,b) color coordinates to HSI coordinates.
%
% Copyright (C) 1995-2009, by Peter I. Corke
%
% This file is part of The Machine Vision Toolbox for Matlab (MVTB).
%
% MVTB is free software: you can redistribute it and/or modify
% it under the terms of the GNU Lesser General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% MVTB is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU Lesser General Public License for more details.
%
% You should have received a copy of the GNU Leser General Public License
% along with MVTB. If not, see <http://www.gnu.org/licenses/>.
function [h,s,i] = rgb2hsi(r,g,b)
if ndims(r) == 3,
g = r(:,:,2);
b = r(:,:,3);
r = r(:,:,1);
end

i = (r+g+b)/3.0;
% from Alan Roberts' FAQ
rg = r-g;
rb = r-b;
gb = g-b;
h = acos((0.5*rg + rb) ./ (rg.^2 + sqrt(rb.*gb)))*180/pi;
a = [r g b];
s = 1 - 3./i*min(a(:));

function hsi = rgb2hsi(rgb)


%RGB2HSI Converts an RGB image to HSI.
%
HSI = RGB2HSI(RGB) converts an RGB image to HSI. The input image
%
is assumed to be of size M-by-N-by-3, where the third dimension
%
accounts for three image planes: red, green, and blue, in that
%
order. If all RGB component images are equal, the HSI conversion
%
is undefined. The input image can be of class double (with values
%
in the range [0, 1]), uint8, or uint16.
%
%
The output image, HSI, is of class double, where:
%
hsi(:, :, 1) = hue image normalized to the range [0, 1] by
%
dividing all angle values by 2*pi.
%
hsi(:, :, 2) = saturation image, in the range [0, 1].
%
hsi(:, :, 3) = intensity image, in the range [0, 1].
%
%
%

Copyright 2002-2004 R. C. Gonzalez, R. E. Woods, & S. L. Eddins


Digital Image Processing Using MATLAB, Prentice-Hall, 2004
$Revision: 1.5 $ $Date: 2005/01/18 13:44:59 $

% Extract the individual component images.


rgb = im2double(rgb);
r = rgb(:, :, 1);
g = rgb(:, :, 2);
b = rgb(:, :, 3);
% Implement the conversion equations.
num = 0.5*((r - g) + (r - b));
den = sqrt((r - g).^2 + (r - b).*(g - b));
theta = acos(num./(den + eps));
H = theta;
H(b > g) = 2*pi - H(b > g);
H = H/(2*pi);
num = min(min(r, g), b);
den = r + g + b;

den(den == 0) = eps;
S = 1 - 3.* num./den;
H(S == 0) = 0;
I = (r + g + b)/3;
% Combine all three results into an hsi image.
hsi = cat(3, H, S, I);

function [VG, A, PPG]= colorgrad(f, T)


%COLORGRAD Computes the vector gradient of an RGB image.
%
[VG, VA, PPG] = COLORGRAD(F, T) computes the vector gradient, VG,
%
and corresponding angle array, VA, (in radians) of RGB image
%
F. It also computes PPG, the per-plane composite gradient
%
obtained by summing the 2-D gradients of the individual color
%
planes. Input T is a threshold in the range [0, 1]. If it is
%
included in the argument list, the values of VG and PPG are
%
thresholded by letting VG(x,y) = 0 for values <= T and VG(x,y) =
%
VG(x,y) otherwise. Similar comments apply to PPG. If T is not
%
included in the argument list then T is set to 0. Both output
%
gradients are scaled to the range [0, 1].
%
%
%

Copyright 2002-2004 R. C. Gonzalez, R. E. Woods, & S. L. Eddins


Digital Image Processing Using MATLAB, Prentice-Hall, 2004
$Revision: 1.6 $ $Date: 2003/11/21 14:27:21 $

if (ndims(f) ~= 3) | (size(f, 3) ~= 3)
error('Input image must be RGB.');
end
% Compute the x and y derivatives
% using Sobel operators.
sh = fspecial('sobel');
sv = sh';
Rx = imfilter(double(f(:, :, 1)),
Ry = imfilter(double(f(:, :, 1)),
Gx = imfilter(double(f(:, :, 2)),
Gy = imfilter(double(f(:, :, 2)),
Bx = imfilter(double(f(:, :, 3)),
By = imfilter(double(f(:, :, 3)),

of the three component images

sh,
sv,
sh,
sv,
sh,
sv,

'replicate');
'replicate');
'replicate');
'replicate');
'replicate');
'replicate');

% Compute the parameters of the vector gradient.


gxx = Rx.^2 + Gx.^2 + Bx.^2;
gyy = Ry.^2 + Gy.^2 + By.^2;
gxy = Rx.*Ry + Gx.*Gy + Bx.*By;
A = 0.5*(atan(2*gxy./(gxx - gyy + eps)));
G1 = 0.5*((gxx + gyy) + (gxx - gyy).*cos(2*A) + 2*gxy.*sin(2*A));
% Now repeat for angle + pi/2. Then select the maximum at each point.
A = A + pi/2;
G2 = 0.5*((gxx + gyy) + (gxx - gyy).*cos(2*A) + 2*gxy.*sin(2*A));
G1 = G1.^0.5;
G2 = G2.^0.5;
% Form VG by picking the maximum at each (x,y) and then scale
% to the range [0, 1].
VG = mat2gray(max(G1, G2));
% Compute the per-plane gradients.
RG = sqrt(Rx.^2 + Ry.^2);
GG = sqrt(Gx.^2 + Gy.^2);
BG = sqrt(Bx.^2 + By.^2);
% Form the composite by adding the individual results and
% scale to [0, 1].
PPG = mat2gray(RG + GG + BG);
% Threshold the result.
if nargin == 2
VG = (VG > T).*VG;
PPG = (PPG > T).*PPG;
end

S-ar putea să vă placă și