X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?p=CreaPhase.git;a=blobdiff_plain;f=octave_packages%2Fimage-1.0.15%2Frgb2gray.m;fp=octave_packages%2Fimage-1.0.15%2Frgb2gray.m;h=050009e3b3cbbd72b6c28a4934f4fc2737a44044;hp=0000000000000000000000000000000000000000;hb=f5f7a74bd8a4900f0b797da6783be80e11a68d86;hpb=1705066eceaaea976f010f669ce8e972f3734b05 diff --git a/octave_packages/image-1.0.15/rgb2gray.m b/octave_packages/image-1.0.15/rgb2gray.m new file mode 100644 index 0000000..050009e --- /dev/null +++ b/octave_packages/image-1.0.15/rgb2gray.m @@ -0,0 +1,55 @@ +## Copyright (C) 2000, 2001 Kai Habel +## +## This program is free software; you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 2 of the License, or +## (at your option) any later version. +## +## This program 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 General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with this program; If not, see . + +## -*- texinfo -*- +## @deftypefn {Function File} @var{gray}= rgb2gray (@var{rgb}) +## Converts an RGB image to a gray scale image, or a color map +## to a gray map. +## +## If the input is an RGB image, the conversion to a gray image +## is computed as the mean value of the color channels. +## +## If the input is a color map it is converted into the YIQ space +## of ntsc. The luminance value (Y) is taken to create a gray color map. +## R = G = B = Y +## @end deftypefn + +## Author: Kai Habel +## Date: 19. March 2000 + +function gray = rgb2gray (rgb) + + if (nargin != 1) + print_usage(); + endif + + if (ismatrix (rgb) && ndims(rgb) == 2 && columns(rgb) == 3) + ntscmap = rgb2ntsc (rgb); + gray = ntscmap (:, 1) * ones (1, 3); + elseif (ismatrix(rgb) && ndims(rgb) == 3) + switch(class(rgb)) + case "double" + gray = mean(rgb,3); + case "uint8" + gray = uint8(mean(rgb,3)); + case "uint16" + gray = uint16(mean(rgb,3)); + otherwise + error("rgb2gray: unsupported class %s", class(rgb)); + endswitch + else + error("rgb2gray: the input must either be an RGB image or a color map"); + endif +endfunction