comparison scripts/image/gray2ind.m @ 15690:7d21456c09d1

gray2ind: also accept binary images and default n to 2 in such cases
author Carnë Draug <carandraug+dev@gmail.com>
date Mon, 12 Nov 2012 05:25:37 +0000
parents 5d3a684236b0
children dffb28f47ea8
comparison
equal deleted inserted replaced
15689:14b7679891dd 15690:7d21456c09d1
18 18
19 ## -*- texinfo -*- 19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {[@var{img} =} gray2ind (@var{I}) 20 ## @deftypefn {Function File} {[@var{img} =} gray2ind (@var{I})
21 ## @deftypefnx {Function File} {[@var{img} =} gray2ind (@var{I}, @var{n}) 21 ## @deftypefnx {Function File} {[@var{img} =} gray2ind (@var{I}, @var{n})
22 ## @deftypefnx {Function File} {[@var{img}, @var{map} =} gray2ind (@dots{}) 22 ## @deftypefnx {Function File} {[@var{img}, @var{map} =} gray2ind (@dots{})
23 ## Convert a gray scale intensity image to an Octave indexed image. 23 ## Convert a gray scale or binary image to an indexed image.
24 ##
24 ## The indexed image will consist of @var{n} different intensity values. 25 ## The indexed image will consist of @var{n} different intensity values.
25 ## If not given @var{n} defaults to 64. 26 ## If not given @var{n} defaults to 64 and 2 for gray scale and binary images
27 ## respectively.
26 ## @seealso{ind2gray, rgb2ind} 28 ## @seealso{ind2gray, rgb2ind}
27 ## @end deftypefn 29 ## @end deftypefn
28 30
29 ## Author: Tony Richardson <arichard@stark.cc.oh.us> 31 ## Author: Tony Richardson <arichard@stark.cc.oh.us>
30 ## Created: July 1994 32 ## Created: July 1994
33 function [X, map] = gray2ind (I, n = 64) 35 function [X, map] = gray2ind (I, n = 64)
34 ## Check input 36 ## Check input
35 if (nargin < 1 || nargin > 2) 37 if (nargin < 1 || nargin > 2)
36 print_usage (); 38 print_usage ();
37 endif 39 endif
40
41 ## default n is different if image is logical
42 if (nargin < 2 && isa (I, "logical"))
43 n = 2;
44 endif
45
38 C = class (I); 46 C = class (I);
39 if (! ismatrix (I) || ndims (I) != 2) 47 if (! ismatrix (I) || ndims (I) != 2)
40 error ("gray2ind: first input argument must be a gray scale image"); 48 error ("gray2ind: first input argument must be a gray scale image");
41 endif 49 endif
42 if (! isscalar (n) || n < 0) 50 if (! isscalar (n) || n < 0)
43 error ("gray2ind: second input argument must be a positive integer"); 51 error ("gray2ind: second input argument must be a positive integer");
44 endif 52 endif
45 ints = {"uint8", "uint16", "int8", "int16"}; 53 ints = {"uint8", "uint16", "int8", "int16"};
46 floats = {"double", "single"}; 54 floats = {"double", "single"};
47 if (! ismember (C, {ints{:}, floats{:}})) 55 if (! ismember (C, {ints{:}, floats{:}, "logical"}))
48 error ("gray2ind: invalid data type '%s'", C); 56 error ("gray2ind: invalid data type '%s'", C);
49 endif 57 endif
50 if (ismember (C, floats) && (min (I(:)) < 0 || max (I(:)) > 1)) 58 if (ismember (C, floats) && (min (I(:)) < 0 || max (I(:)) > 1))
51 error ("gray2ind: floating point images may only contain values between 0 and 1"); 59 error ("gray2ind: floating point images may only contain values between 0 and 1");
52 endif 60 endif