Mercurial > hg > octave-image
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 |
rev | line source |
---|---|
502 | 1 ## Copyright (C) 2000 Kai Habel <kai.habel@gmx.de> |
2 ## Copyright (C) 2011 Carnë Draug <carandraug+dev@gmail.com> | |
186 | 3 ## |
4 ## This program is free software; you can redistribute it and/or modify | |
5 ## it under the terms of the GNU General Public License as published by | |
502 | 6 ## the Free Software Foundation; either version 3 of the License, or |
186 | 7 ## (at your option) any later version. |
8 ## | |
9 ## This program is distributed in the hope that it will be useful, | |
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 ## GNU General Public License for more details. | |
13 ## | |
14 ## You should have received a copy of the GNU General Public License | |
274 | 15 ## along with this program; If not, see <http://www.gnu.org/licenses/>. |
186 | 16 |
17 ## -*- texinfo -*- | |
502 | 18 ## @deftypefn {Function File} @var{bool} = isbw (@var{img}) |
19 ## @deftypefnx {Function File} @var{bool} = isbw (@var{img}, @var{logic}) | |
20 ## Return true if @var{img} is a black-white image. | |
21 ## | |
22 ## The optional argument @var{logic} must be the string `logical' or | |
23 ## `non-logical'. The first defines a black and white image as a logical matrix, | |
24 ## while the second defines it as a matrix comprised of the values 1 and 0 | |
25 ## only. Defaults to `logical'. | |
26 ## | |
27 ## @seealso{isgray, isind, islogical, isrgb} | |
186 | 28 ## @end deftypefn |
29 | |
502 | 30 function bool = isbw (BW, logic = "logical") |
31 ## this function has been removed from version 7.3 (R2011b) of | |
32 ## matlab's image processing toolbox | |
33 if (nargin < 1 || nargin > 2) | |
34 print_usage; | |
35 elseif (!ischar (logic) && any (strcmpi (logic, {"logical", "non-logical"}))) | |
36 error ("second argument must either be a string 'logical' or 'non-logical'") | |
186 | 37 endif |
38 | |
502 | 39 ## an image cannot be a sparse matrix |
40 if (!ismatrix (BW) || issparse (BW)) | |
41 bool = false; | |
42 elseif (strcmpi (logic, "logical")) | |
43 ## this is the matlab compatible way (before they removed the function) | |
44 bool = islogical (BW); | |
186 | 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 | 48 persistent warned = false; |
49 if (! warned) | |
50 warned = true; | |
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 | 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 | 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 | 57 |
502 | 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 | 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 | 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 |