]> Creatis software - CreaPhase.git/blob - octave_packages/image-1.0.15/rgb2gray.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / image-1.0.15 / rgb2gray.m
1 ## Copyright (C) 2000, 2001  Kai Habel
2 ##
3 ## This program is free software; you can redistribute it and/or modify
4 ## it under the terms of the GNU General Public License as published by
5 ## the Free Software Foundation; either version 2 of the License, or
6 ## (at your option) any later version.
7 ##
8 ## This program is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 ## GNU General Public License for more details.
12 ##
13 ## You should have received a copy of the GNU General Public License
14 ## along with this program; If not, see <http://www.gnu.org/licenses/>.
15
16 ## -*- texinfo -*-
17 ## @deftypefn {Function File} @var{gray}= rgb2gray (@var{rgb})
18 ## Converts an RGB image to a gray scale image, or a color map
19 ## to a gray map.
20 ##
21 ## If the input is an RGB image, the conversion to a gray image
22 ## is computed as the mean value of the color channels.
23 ##
24 ## If the input is a color map it is converted into the YIQ space
25 ## of ntsc. The luminance value (Y) is taken to create a gray color map.
26 ## R = G = B = Y
27 ## @end deftypefn
28
29 ## Author:      Kai Habel <kai.habel@gmx.de>
30 ## Date:        19. March 2000
31
32 function gray = rgb2gray (rgb)
33
34   if (nargin != 1)
35     print_usage();
36   endif
37
38   if (ismatrix (rgb) && ndims(rgb) == 2 && columns(rgb) == 3)
39     ntscmap = rgb2ntsc (rgb);
40     gray = ntscmap (:, 1) * ones (1, 3);
41   elseif (ismatrix(rgb) && ndims(rgb) == 3)
42     switch(class(rgb))
43     case "double"
44       gray = mean(rgb,3);
45     case "uint8"
46       gray = uint8(mean(rgb,3));
47     case "uint16"
48       gray = uint16(mean(rgb,3));
49     otherwise
50       error("rgb2gray: unsupported class %s", class(rgb));
51     endswitch
52   else
53     error("rgb2gray: the input must either be an RGB image or a color map");
54   endif
55 endfunction