annotate inst/isbw.m @ 507:a60411e31d36

isbw: check sample of image first to speed up computation
author carandraug
date Mon, 05 Dec 2011 03:59:44 +0000
parents 5dd8356bd17c
children 9135e5461550
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
502
86fd5ef6ab60 isbw: IMPORTANT COMMIT BREAKS COMPATIBILITY
carandraug
parents: 321
diff changeset
1 ## Copyright (C) 2000 Kai Habel <kai.habel@gmx.de>
86fd5ef6ab60 isbw: IMPORTANT COMMIT BREAKS COMPATIBILITY
carandraug
parents: 321
diff changeset
2 ## Copyright (C) 2011 Carnë Draug <carandraug+dev@gmail.com>
186
13c6a9bdec24 Changed the structure to match the package system
hauberg
parents:
diff changeset
3 ##
13c6a9bdec24 Changed the structure to match the package system
hauberg
parents:
diff changeset
4 ## This program is free software; you can redistribute it and/or modify
13c6a9bdec24 Changed the structure to match the package system
hauberg
parents:
diff changeset
5 ## it under the terms of the GNU General Public License as published by
502
86fd5ef6ab60 isbw: IMPORTANT COMMIT BREAKS COMPATIBILITY
carandraug
parents: 321
diff changeset
6 ## the Free Software Foundation; either version 3 of the License, or
186
13c6a9bdec24 Changed the structure to match the package system
hauberg
parents:
diff changeset
7 ## (at your option) any later version.
13c6a9bdec24 Changed the structure to match the package system
hauberg
parents:
diff changeset
8 ##
13c6a9bdec24 Changed the structure to match the package system
hauberg
parents:
diff changeset
9 ## This program is distributed in the hope that it will be useful,
13c6a9bdec24 Changed the structure to match the package system
hauberg
parents:
diff changeset
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
13c6a9bdec24 Changed the structure to match the package system
hauberg
parents:
diff changeset
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13c6a9bdec24 Changed the structure to match the package system
hauberg
parents:
diff changeset
12 ## GNU General Public License for more details.
13c6a9bdec24 Changed the structure to match the package system
hauberg
parents:
diff changeset
13 ##
13c6a9bdec24 Changed the structure to match the package system
hauberg
parents:
diff changeset
14 ## You should have received a copy of the GNU General Public License
274
3da7ef6dd4ee More copyright updates
adb014
parents: 248
diff changeset
15 ## along with this program; If not, see <http://www.gnu.org/licenses/>.
186
13c6a9bdec24 Changed the structure to match the package system
hauberg
parents:
diff changeset
16
13c6a9bdec24 Changed the structure to match the package system
hauberg
parents:
diff changeset
17 ## -*- texinfo -*-
502
86fd5ef6ab60 isbw: IMPORTANT COMMIT BREAKS COMPATIBILITY
carandraug
parents: 321
diff changeset
18 ## @deftypefn {Function File} @var{bool} = isbw (@var{img})
86fd5ef6ab60 isbw: IMPORTANT COMMIT BREAKS COMPATIBILITY
carandraug
parents: 321
diff changeset
19 ## @deftypefnx {Function File} @var{bool} = isbw (@var{img}, @var{logic})
86fd5ef6ab60 isbw: IMPORTANT COMMIT BREAKS COMPATIBILITY
carandraug
parents: 321
diff changeset
20 ## Return true if @var{img} is a black-white image.
86fd5ef6ab60 isbw: IMPORTANT COMMIT BREAKS COMPATIBILITY
carandraug
parents: 321
diff changeset
21 ##
86fd5ef6ab60 isbw: IMPORTANT COMMIT BREAKS COMPATIBILITY
carandraug
parents: 321
diff changeset
22 ## The optional argument @var{logic} must be the string `logical' or
86fd5ef6ab60 isbw: IMPORTANT COMMIT BREAKS COMPATIBILITY
carandraug
parents: 321
diff changeset
23 ## `non-logical'. The first defines a black and white image as a logical matrix,
86fd5ef6ab60 isbw: IMPORTANT COMMIT BREAKS COMPATIBILITY
carandraug
parents: 321
diff changeset
24 ## while the second defines it as a matrix comprised of the values 1 and 0
86fd5ef6ab60 isbw: IMPORTANT COMMIT BREAKS COMPATIBILITY
carandraug
parents: 321
diff changeset
25 ## only. Defaults to `logical'.
86fd5ef6ab60 isbw: IMPORTANT COMMIT BREAKS COMPATIBILITY
carandraug
parents: 321
diff changeset
26 ##
86fd5ef6ab60 isbw: IMPORTANT COMMIT BREAKS COMPATIBILITY
carandraug
parents: 321
diff changeset
27 ## @seealso{isgray, isind, islogical, isrgb}
186
13c6a9bdec24 Changed the structure to match the package system
hauberg
parents:
diff changeset
28 ## @end deftypefn
13c6a9bdec24 Changed the structure to match the package system
hauberg
parents:
diff changeset
29
502
86fd5ef6ab60 isbw: IMPORTANT COMMIT BREAKS COMPATIBILITY
carandraug
parents: 321
diff changeset
30 function bool = isbw (BW, logic = "logical")
86fd5ef6ab60 isbw: IMPORTANT COMMIT BREAKS COMPATIBILITY
carandraug
parents: 321
diff changeset
31 ## this function has been removed from version 7.3 (R2011b) of
86fd5ef6ab60 isbw: IMPORTANT COMMIT BREAKS COMPATIBILITY
carandraug
parents: 321
diff changeset
32 ## matlab's image processing toolbox
86fd5ef6ab60 isbw: IMPORTANT COMMIT BREAKS COMPATIBILITY
carandraug
parents: 321
diff changeset
33 if (nargin < 1 || nargin > 2)
86fd5ef6ab60 isbw: IMPORTANT COMMIT BREAKS COMPATIBILITY
carandraug
parents: 321
diff changeset
34 print_usage;
86fd5ef6ab60 isbw: IMPORTANT COMMIT BREAKS COMPATIBILITY
carandraug
parents: 321
diff changeset
35 elseif (!ischar (logic) && any (strcmpi (logic, {"logical", "non-logical"})))
86fd5ef6ab60 isbw: IMPORTANT COMMIT BREAKS COMPATIBILITY
carandraug
parents: 321
diff changeset
36 error ("second argument must either be a string 'logical' or 'non-logical'")
186
13c6a9bdec24 Changed the structure to match the package system
hauberg
parents:
diff changeset
37 endif
13c6a9bdec24 Changed the structure to match the package system
hauberg
parents:
diff changeset
38
502
86fd5ef6ab60 isbw: IMPORTANT COMMIT BREAKS COMPATIBILITY
carandraug
parents: 321
diff changeset
39 ## an image cannot be a sparse matrix
86fd5ef6ab60 isbw: IMPORTANT COMMIT BREAKS COMPATIBILITY
carandraug
parents: 321
diff changeset
40 if (!ismatrix (BW) || issparse (BW))
86fd5ef6ab60 isbw: IMPORTANT COMMIT BREAKS COMPATIBILITY
carandraug
parents: 321
diff changeset
41 bool = false;
86fd5ef6ab60 isbw: IMPORTANT COMMIT BREAKS COMPATIBILITY
carandraug
parents: 321
diff changeset
42 elseif (strcmpi (logic, "logical"))
86fd5ef6ab60 isbw: IMPORTANT COMMIT BREAKS COMPATIBILITY
carandraug
parents: 321
diff changeset
43 ## this is the matlab compatible way (before they removed the function)
86fd5ef6ab60 isbw: IMPORTANT COMMIT BREAKS COMPATIBILITY
carandraug
parents: 321
diff changeset
44 bool = islogical (BW);
186
13c6a9bdec24 Changed the structure to match the package system
hauberg
parents:
diff changeset
45
503
5dd8356bd17c isbw: made function still return true for non-logical matrix of 1 and 0 but issue warning. This will be changed again in future
carandraug
parents: 502
diff changeset
46 ## the following block is just temporary to keep backwards compatibility
507
a60411e31d36 isbw: check sample of image first to speed up computation
carandraug
parents: 503
diff changeset
47 if (!islogical (BW) && is_bw_nonlogical (BW))
502
86fd5ef6ab60 isbw: IMPORTANT COMMIT BREAKS COMPATIBILITY
carandraug
parents: 321
diff changeset
48 persistent warned = false;
86fd5ef6ab60 isbw: IMPORTANT COMMIT BREAKS COMPATIBILITY
carandraug
parents: 321
diff changeset
49 if (! warned)
86fd5ef6ab60 isbw: IMPORTANT COMMIT BREAKS COMPATIBILITY
carandraug
parents: 321
diff changeset
50 warned = true;
86fd5ef6ab60 isbw: IMPORTANT COMMIT BREAKS COMPATIBILITY
carandraug
parents: 321
diff changeset
51 warning ("isbw: image is not logical matrix and therefore not binary but all values are either 0 and 1.")
503
5dd8356bd17c isbw: made function still return true for non-logical matrix of 1 and 0 but issue warning. This will be changed again in future
carandraug
parents: 502
diff changeset
52 warning ("isbw: future versions of this function will return true. Consider using the call isbw (img, \"non-logical\").")
502
86fd5ef6ab60 isbw: IMPORTANT COMMIT BREAKS COMPATIBILITY
carandraug
parents: 321
diff changeset
53 endif
503
5dd8356bd17c isbw: made function still return true for non-logical matrix of 1 and 0 but issue warning. This will be changed again in future
carandraug
parents: 502
diff changeset
54 bool = true;
502
86fd5ef6ab60 isbw: IMPORTANT COMMIT BREAKS COMPATIBILITY
carandraug
parents: 321
diff changeset
55 endif
503
5dd8356bd17c isbw: made function still return true for non-logical matrix of 1 and 0 but issue warning. This will be changed again in future
carandraug
parents: 502
diff changeset
56 ## end of temporary block for backwards compatibility
186
13c6a9bdec24 Changed the structure to match the package system
hauberg
parents:
diff changeset
57
502
86fd5ef6ab60 isbw: IMPORTANT COMMIT BREAKS COMPATIBILITY
carandraug
parents: 321
diff changeset
58 elseif (strcmpi (logic, "non-logical"))
507
a60411e31d36 isbw: check sample of image first to speed up computation
carandraug
parents: 503
diff changeset
59 ## to speed this up, we can look at a sample of the image first
a60411e31d36 isbw: check sample of image first to speed up computation
carandraug
parents: 503
diff changeset
60 bool = is_bw_nonlogical (BW(1:ceil (rows (BW) /100), 1:ceil (columns (BW) /100)));
a60411e31d36 isbw: check sample of image first to speed up computation
carandraug
parents: 503
diff changeset
61 if (bool)
a60411e31d36 isbw: check sample of image first to speed up computation
carandraug
parents: 503
diff changeset
62 ## sample was true, we better make sure it's real
a60411e31d36 isbw: check sample of image first to speed up computation
carandraug
parents: 503
diff changeset
63 bool = is_bw_nonlogical (BW);
a60411e31d36 isbw: check sample of image first to speed up computation
carandraug
parents: 503
diff changeset
64 endif
502
86fd5ef6ab60 isbw: IMPORTANT COMMIT BREAKS COMPATIBILITY
carandraug
parents: 321
diff changeset
65 endif
503
5dd8356bd17c isbw: made function still return true for non-logical matrix of 1 and 0 but issue warning. This will be changed again in future
carandraug
parents: 502
diff changeset
66
186
13c6a9bdec24 Changed the structure to match the package system
hauberg
parents:
diff changeset
67 endfunction
507
a60411e31d36 isbw: check sample of image first to speed up computation
carandraug
parents: 503
diff changeset
68
a60411e31d36 isbw: check sample of image first to speed up computation
carandraug
parents: 503
diff changeset
69 function bool = is_bw_nonlogical (BW)
a60411e31d36 isbw: check sample of image first to speed up computation
carandraug
parents: 503
diff changeset
70 bool = all (all ((BW == 1) + (BW == 0)));
a60411e31d36 isbw: check sample of image first to speed up computation
carandraug
parents: 503
diff changeset
71 endfunction