1######################################################################## 2## 3## Copyright (C) 1994-2021 The Octave Project Developers 4## 5## See the file COPYRIGHT.md in the top-level directory of this 6## distribution or <https://octave.org/copyright/>. 7## 8## This file is part of Octave. 9## 10## Octave is free software: you can redistribute it and/or modify it 11## under the terms of the GNU General Public License as published by 12## the Free Software Foundation, either version 3 of the License, or 13## (at your option) any later version. 14## 15## Octave is distributed in the hope that it will be useful, but 16## WITHOUT ANY WARRANTY; without even the implied warranty of 17## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18## GNU General Public License for more details. 19## 20## You should have received a copy of the GNU General Public License 21## along with Octave; see the file COPYING. If not, see 22## <https://www.gnu.org/licenses/>. 23## 24######################################################################## 25 26## -*- texinfo -*- 27## @deftypefn {} {@var{I} =} ind2gray (@var{x}, @var{map}) 28## Convert a color indexed image to a grayscale intensity image. 29## 30## The image @var{x} must be an indexed image which will be converted using the 31## colormap @var{map}. If @var{map} does not contain enough colors for the 32## image, pixels in @var{x} outside the range are mapped to the last color in 33## the map before conversion to grayscale. 34## 35## The output @var{I} is of the same class as the input @var{x} and may be 36## one of @code{uint8}, @code{uint16}, @code{single}, or @code{double}. 37## 38## Implementation Note: There are several ways of converting colors to 39## grayscale intensities. This functions uses the luminance value obtained 40## from @code{rgb2gray} which is @code{I = 0.299*R + 0.587*G + 0.114*B}. 41## Other possibilities include the value component from @code{rgb2hsv} or 42## using a single color channel from @code{ind2rgb}. 43## @seealso{gray2ind, ind2rgb} 44## @end deftypefn 45 46function I = ind2gray (x, map) 47 48 if (nargin != 2) 49 print_usage (); 50 endif 51 [x, map] = ind2x ("ind2gray", x, map); 52 53 ## Convert colormap to luminance intensity values 54 map *= [0.29894; 0.58704; 0.11402]; 55 56 ## Convert colormap to same class as that of input so that reshape 57 ## will produce output of the same type as the input. 58 cls = class (x); 59 if (isinteger (x)) 60 ## if we later add support for int16 images, this will not work. Look into 61 ## im2int16 from image package for such case 62 map *= intmax (cls); 63 elseif (strcmp (cls, "single")) 64 map = single (map); 65 endif 66 67 ## Replace indices in the input matrix with the indexed luminance value. 68 I = reshape (map(x(:)), size (x)); 69 70endfunction 71 72 73%!shared i2g 74%! i2g = ind2gray (1:100, gray (100)); 75%! 76%!assert (i2g, 0:1/99:1, eps) 77%!assert (gray2ind (i2g, 100), uint8 (0:99)) 78 79## Test input validation 80%!error ind2gray () 81%!error ind2gray (1) 82%!error ind2gray (1,2,3) 83%!error <X must be an indexed image> ind2gray (ones (3,3,3), jet (64)) 84%!error <X must be an indexed image> ind2gray (1+i, jet (64)) 85%!error <X must be an indexed image> ind2gray (sparse (1), jet (64)) 86%!error <X must be an indexed image> ind2gray (1.1, jet (64)) 87%!error <X must be an indexed image> ind2gray ({1}, jet (64)) 88%!error <MAP must be a valid colormap> ind2gray (1, {1}) 89%!error <MAP must be a valid colormap> ind2gray (1, 1+i) 90%!error <MAP must be a valid colormap> ind2gray (1, ones (2,2,2)) 91%!error <MAP must be a valid colormap> ind2gray (1, ones (2,4)) 92%!error <MAP must be a valid colormap> ind2gray (1, [-1]) 93%!error <MAP must be a valid colormap> ind2gray (1, [2]) 94 95%!warning <contains colors outside of colormap> ind2gray ([0 1 2], gray (5)); 96%!warning <contains colors outside of colormap> ind2gray ([1 2 6], gray (5)); 97%!warning <contains colors outside of colormap> ind2gray (uint8 ([1 2 5]), gray (5)); 98