1 ## Copyright (C) 2000, 2001 Kai Habel
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.
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.
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/>.
17 ## @deftypefn {Function File} @var{gray}= rgb2gray (@var{rgb})
18 ## Converts an RGB image to a gray scale image, or a color map
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.
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.
29 ## Author: Kai Habel <kai.habel@gmx.de>
30 ## Date: 19. March 2000
32 function gray = rgb2gray (rgb)
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)
46 gray = uint8(mean(rgb,3));
48 gray = uint16(mean(rgb,3));
50 error("rgb2gray: unsupported class %s", class(rgb));
53 error("rgb2gray: the input must either be an RGB image or a color map");