Mercurial > hg > octave-image
annotate inst/isbw.m @ 915:8c8ed7c4ab83 default tip
* bwlabeln.cc (bwlabel_nd): Fix small bug in comment
author | Jordi Gutiérrez Hermoso <jordigh@octave.org> |
---|---|
date | Tue, 18 Nov 2014 11:30:57 -0500 |
parents | 50fb3e71ef72 |
children |
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 ## |
561
c45838839d86
maint: update license to GPLv3 and mention non GPL files
carandraug
parents:
511
diff
changeset
|
4 ## This program is free software; you can redistribute it and/or modify it under |
c45838839d86
maint: update license to GPLv3 and mention non GPL files
carandraug
parents:
511
diff
changeset
|
5 ## the terms of the GNU General Public License as published by the Free Software |
c45838839d86
maint: update license to GPLv3 and mention non GPL files
carandraug
parents:
511
diff
changeset
|
6 ## Foundation; either version 3 of the License, or (at your option) any later |
c45838839d86
maint: update license to GPLv3 and mention non GPL files
carandraug
parents:
511
diff
changeset
|
7 ## version. |
186 | 8 ## |
561
c45838839d86
maint: update license to GPLv3 and mention non GPL files
carandraug
parents:
511
diff
changeset
|
9 ## This program is distributed in the hope that it will be useful, but WITHOUT |
c45838839d86
maint: update license to GPLv3 and mention non GPL files
carandraug
parents:
511
diff
changeset
|
10 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
c45838839d86
maint: update license to GPLv3 and mention non GPL files
carandraug
parents:
511
diff
changeset
|
11 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
c45838839d86
maint: update license to GPLv3 and mention non GPL files
carandraug
parents:
511
diff
changeset
|
12 ## details. |
186 | 13 ## |
561
c45838839d86
maint: update license to GPLv3 and mention non GPL files
carandraug
parents:
511
diff
changeset
|
14 ## You should have received a copy of the GNU General Public License along with |
c45838839d86
maint: update license to GPLv3 and mention non GPL files
carandraug
parents:
511
diff
changeset
|
15 ## this program; if not, see <http://www.gnu.org/licenses/>. |
186 | 16 |
17 ## -*- texinfo -*- | |
894
50fb3e71ef72
doc: fix multiple issues for newer versions of Texinfo
Carnë Draug <carandraug@octave.org>
parents:
850
diff
changeset
|
18 ## @deftypefn {Function File} {} isbw (@var{img}) |
50fb3e71ef72
doc: fix multiple issues for newer versions of Texinfo
Carnë Draug <carandraug@octave.org>
parents:
850
diff
changeset
|
19 ## @deftypefnx {Function File} {} isbw (@var{img}, @var{logic}) |
571
d8ad8f768386
isbw: delegate checks to new shared private functions and add test blocks
carandraug
parents:
561
diff
changeset
|
20 ## Return true if @var{img} is a black and white image. |
502 | 21 ## |
850
c2bd6e0752ec
Make the is* image type functions identify N dimensional images.
Carnë Draug <carandraug@octave.org>
parents:
800
diff
changeset
|
22 ## A variable can be considered a black and white image if it is a |
c2bd6e0752ec
Make the is* image type functions identify N dimensional images.
Carnë Draug <carandraug@octave.org>
parents:
800
diff
changeset
|
23 ## non-sparse matrix of size @nospell{MxNx1xK}, and depending on the |
c2bd6e0752ec
Make the is* image type functions identify N dimensional images.
Carnë Draug <carandraug@octave.org>
parents:
800
diff
changeset
|
24 ## string @var{logic}, defined as: |
c2bd6e0752ec
Make the is* image type functions identify N dimensional images.
Carnë Draug <carandraug@octave.org>
parents:
800
diff
changeset
|
25 ## |
c2bd6e0752ec
Make the is* image type functions identify N dimensional images.
Carnë Draug <carandraug@octave.org>
parents:
800
diff
changeset
|
26 ## @table @asis |
c2bd6e0752ec
Make the is* image type functions identify N dimensional images.
Carnë Draug <carandraug@octave.org>
parents:
800
diff
changeset
|
27 ## @item @qcode{"logical"} (default) |
c2bd6e0752ec
Make the is* image type functions identify N dimensional images.
Carnë Draug <carandraug@octave.org>
parents:
800
diff
changeset
|
28 ## @var{img} must be of class logical. |
502 | 29 ## |
850
c2bd6e0752ec
Make the is* image type functions identify N dimensional images.
Carnë Draug <carandraug@octave.org>
parents:
800
diff
changeset
|
30 ## @item @qcode{"non-logical"} |
c2bd6e0752ec
Make the is* image type functions identify N dimensional images.
Carnë Draug <carandraug@octave.org>
parents:
800
diff
changeset
|
31 ## all values in @var{img} are either 1 or 0. |
c2bd6e0752ec
Make the is* image type functions identify N dimensional images.
Carnë Draug <carandraug@octave.org>
parents:
800
diff
changeset
|
32 ## @end table |
c2bd6e0752ec
Make the is* image type functions identify N dimensional images.
Carnë Draug <carandraug@octave.org>
parents:
800
diff
changeset
|
33 ## |
c2bd6e0752ec
Make the is* image type functions identify N dimensional images.
Carnë Draug <carandraug@octave.org>
parents:
800
diff
changeset
|
34 ## @strong{Note:} despite their suggestive names, the functions isbw, |
c2bd6e0752ec
Make the is* image type functions identify N dimensional images.
Carnë Draug <carandraug@octave.org>
parents:
800
diff
changeset
|
35 ## isgray, isind, and isrgb, are ambiguous since it is not always possible |
c2bd6e0752ec
Make the is* image type functions identify N dimensional images.
Carnë Draug <carandraug@octave.org>
parents:
800
diff
changeset
|
36 ## to distinguish between those image types. For example, an uint8 matrix |
c2bd6e0752ec
Make the is* image type functions identify N dimensional images.
Carnë Draug <carandraug@octave.org>
parents:
800
diff
changeset
|
37 ## can be both a grayscale and indexed image. They are good to dismiss |
c2bd6e0752ec
Make the is* image type functions identify N dimensional images.
Carnë Draug <carandraug@octave.org>
parents:
800
diff
changeset
|
38 ## input as an invalid image type, but not for identification. |
c2bd6e0752ec
Make the is* image type functions identify N dimensional images.
Carnë Draug <carandraug@octave.org>
parents:
800
diff
changeset
|
39 ## |
c2bd6e0752ec
Make the is* image type functions identify N dimensional images.
Carnë Draug <carandraug@octave.org>
parents:
800
diff
changeset
|
40 ## @seealso{im2bw, isgray, isind, islogical, isrgb} |
186 | 41 ## @end deftypefn |
42 | |
502 | 43 function bool = isbw (BW, logic = "logical") |
850
c2bd6e0752ec
Make the is* image type functions identify N dimensional images.
Carnë Draug <carandraug@octave.org>
parents:
800
diff
changeset
|
44 |
502 | 45 if (nargin < 1 || nargin > 2) |
46 print_usage; | |
186 | 47 endif |
48 | |
571
d8ad8f768386
isbw: delegate checks to new shared private functions and add test blocks
carandraug
parents:
561
diff
changeset
|
49 bool = false; |
850
c2bd6e0752ec
Make the is* image type functions identify N dimensional images.
Carnë Draug <carandraug@octave.org>
parents:
800
diff
changeset
|
50 if (isimage (BW) && ndims (BW) < 5 && size (BW, 3) == 1) |
c2bd6e0752ec
Make the is* image type functions identify N dimensional images.
Carnë Draug <carandraug@octave.org>
parents:
800
diff
changeset
|
51 if (strcmpi (logic, "logical")) |
c2bd6e0752ec
Make the is* image type functions identify N dimensional images.
Carnë Draug <carandraug@octave.org>
parents:
800
diff
changeset
|
52 ## this is the matlab compatible way (before they removed the function) |
c2bd6e0752ec
Make the is* image type functions identify N dimensional images.
Carnë Draug <carandraug@octave.org>
parents:
800
diff
changeset
|
53 bool = islogical (BW); |
186 | 54 |
850
c2bd6e0752ec
Make the is* image type functions identify N dimensional images.
Carnë Draug <carandraug@octave.org>
parents:
800
diff
changeset
|
55 elseif (strcmpi (logic, "non-logical")) |
c2bd6e0752ec
Make the is* image type functions identify N dimensional images.
Carnë Draug <carandraug@octave.org>
parents:
800
diff
changeset
|
56 bool = islogical (BW) || ispart (@is_bw_nonlogical, BW); |
c2bd6e0752ec
Make the is* image type functions identify N dimensional images.
Carnë Draug <carandraug@octave.org>
parents:
800
diff
changeset
|
57 |
c2bd6e0752ec
Make the is* image type functions identify N dimensional images.
Carnë Draug <carandraug@octave.org>
parents:
800
diff
changeset
|
58 else |
c2bd6e0752ec
Make the is* image type functions identify N dimensional images.
Carnë Draug <carandraug@octave.org>
parents:
800
diff
changeset
|
59 error ("isbw: LOGIC must be the string 'logical' or 'non-logical'") |
502 | 60 endif |
61 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
|
62 |
186 | 63 endfunction |
507
a60411e31d36
isbw: check sample of image first to speed up computation
carandraug
parents:
503
diff
changeset
|
64 |
a60411e31d36
isbw: check sample of image first to speed up computation
carandraug
parents:
503
diff
changeset
|
65 function bool = is_bw_nonlogical (BW) |
850
c2bd6e0752ec
Make the is* image type functions identify N dimensional images.
Carnë Draug <carandraug@octave.org>
parents:
800
diff
changeset
|
66 bool = ! any ((BW(:) != 1) & (BW(:) != 0)); |
507
a60411e31d36
isbw: check sample of image first to speed up computation
carandraug
parents:
503
diff
changeset
|
67 endfunction |
571
d8ad8f768386
isbw: delegate checks to new shared private functions and add test blocks
carandraug
parents:
561
diff
changeset
|
68 |
850
c2bd6e0752ec
Make the is* image type functions identify N dimensional images.
Carnë Draug <carandraug@octave.org>
parents:
800
diff
changeset
|
69 %!test |
c2bd6e0752ec
Make the is* image type functions identify N dimensional images.
Carnë Draug <carandraug@octave.org>
parents:
800
diff
changeset
|
70 %! a = round (rand (100)); |
c2bd6e0752ec
Make the is* image type functions identify N dimensional images.
Carnë Draug <carandraug@octave.org>
parents:
800
diff
changeset
|
71 %! assert (isbw (a, "non-logical"), true); |
c2bd6e0752ec
Make the is* image type functions identify N dimensional images.
Carnë Draug <carandraug@octave.org>
parents:
800
diff
changeset
|
72 %! assert (isbw (a, "logical"), false); |
c2bd6e0752ec
Make the is* image type functions identify N dimensional images.
Carnë Draug <carandraug@octave.org>
parents:
800
diff
changeset
|
73 %! assert (isbw (logical (a), "logical"), true); |
c2bd6e0752ec
Make the is* image type functions identify N dimensional images.
Carnë Draug <carandraug@octave.org>
parents:
800
diff
changeset
|
74 %! assert (isbw (logical (a), "non-logical"), true); |
c2bd6e0752ec
Make the is* image type functions identify N dimensional images.
Carnë Draug <carandraug@octave.org>
parents:
800
diff
changeset
|
75 %! |
c2bd6e0752ec
Make the is* image type functions identify N dimensional images.
Carnë Draug <carandraug@octave.org>
parents:
800
diff
changeset
|
76 %! ## change when the different value is near the start and then in middle, |
c2bd6e0752ec
Make the is* image type functions identify N dimensional images.
Carnë Draug <carandraug@octave.org>
parents:
800
diff
changeset
|
77 %! ## because of the way we test part of the image before the rest |
c2bd6e0752ec
Make the is* image type functions identify N dimensional images.
Carnë Draug <carandraug@octave.org>
parents:
800
diff
changeset
|
78 %! a(1, 1) = 2; |
c2bd6e0752ec
Make the is* image type functions identify N dimensional images.
Carnë Draug <carandraug@octave.org>
parents:
800
diff
changeset
|
79 %! assert (isbw (a, "non-logical"), false); |
c2bd6e0752ec
Make the is* image type functions identify N dimensional images.
Carnë Draug <carandraug@octave.org>
parents:
800
diff
changeset
|
80 %! |
c2bd6e0752ec
Make the is* image type functions identify N dimensional images.
Carnë Draug <carandraug@octave.org>
parents:
800
diff
changeset
|
81 %! a( 1, 1) = 1; |
571
d8ad8f768386
isbw: delegate checks to new shared private functions and add test blocks
carandraug
parents:
561
diff
changeset
|
82 %! a(50, 50) = 2; |
850
c2bd6e0752ec
Make the is* image type functions identify N dimensional images.
Carnë Draug <carandraug@octave.org>
parents:
800
diff
changeset
|
83 %! assert (isbw (a, "non-logical"), false); |