[754a6f]: inst / bwareaopen.m  Maximize  Restore  History

Download this file

128 lines (118 with data), 5.0 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
## Copyright (C) 2013 CarnĂŤ Draug <carandraug@octave.org>
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, see <http://www.gnu.org/licenses/>.
## -*- texinfo -*-
## @deftypefn {Function File} {} bwareaopen (@var{bw}, @var{lim})
## @deftypefnx {Function File} {} bwareaopen (@var{bw}, @var{lim}, @var{conn})
## Perform area opening.
##
## Remove objects with less than @var{lim} elements from a binary image
## @var{bw}.
##
## Element connectivity @var{conn}, to define the size of objects, can be
## specified with a numeric scalar (number of elements in the neighborhood):
##
## @table @samp
## @item 4 or 8
## for 2 dimensional matrices;
## @item 6, 18 or 26
## for 3 dimensional matrices;
## @end table
##
## or with a binary matrix representing a connectivity array. Defaults to
## @code{conndef (ndims (@var{bw}), "maximal")} which is equivalent to
## @var{conn} of 8 and 26 for 2 and 3 dimensional matrices respectively.
##
## @seealso{bwconncomp, conndef, bwboundaries}
## @end deftypefn
function bw = bwareaopen (bw, lim, conn)
if (nargin < 2 || nargin > 3)
print_usage ();
elseif (! ismatrix (bw) || ! (isnumeric (bw) || islogical (bw)))
error ("bwareaopen: BW must be an a numeric matrix");
elseif (! isnumeric (lim) || ! isscalar (lim) || lim < 0 || fix (lim) != lim)
error ("bwareaopen: LIM must be a non-negative scalar integer")
endif
if (nargin < 3)
## Defining default connectivity here because it's dependent
## on the first argument
conn = conndef (ndims (bw), "maximal");
else
conn = make_conn ("bwareaopen", 3, ndims (bw), conn);
endif
## Output is always of class logical
bw = logical (bw);
## We only have work to do when lim > 1
if (lim > 1)
idx_list = bwconncomp (bw, conn).PixelIdxList;
ind = cell2mat (idx_list (cellfun ("numel", idx_list) < lim)');
bw(ind) = false;
endif
endfunction
%!test
%! in = [ 0 0 1 0 0 1 0 1 0 0
%! 0 0 1 0 0 0 0 0 1 1
%! 1 0 0 0 0 1 1 0 0 0
%! 1 0 0 0 1 0 0 0 0 0
%! 1 1 1 1 0 0 0 0 0 1
%! 0 1 0 1 1 0 0 1 0 0
%! 1 0 0 0 1 0 0 0 0 0
%! 0 0 0 1 1 0 0 1 0 0
%! 0 1 0 1 1 0 0 1 1 0
%! 0 1 0 1 1 1 0 0 1 0];
%! assert (bwareaopen (in, 1, 4), logical (in))
%!
%! out = [0 0 0 0 0 0 0 0 0 0
%! 0 0 0 0 0 0 0 0 0 0
%! 1 0 0 0 0 0 0 0 0 0
%! 1 0 0 0 0 0 0 0 0 0
%! 1 1 1 1 0 0 0 0 0 0
%! 0 1 0 1 1 0 0 0 0 0
%! 0 0 0 0 1 0 0 0 0 0
%! 0 0 0 1 1 0 0 0 0 0
%! 0 0 0 1 1 0 0 0 0 0
%! 0 0 0 1 1 1 0 0 0 0];
%! assert (bwareaopen (logical (in), 10, 4), logical (out))
%! assert (bwareaopen (in, 10, 4), logical (out))
%! assert (bwareaopen (in, 10, [0 1 0; 1 1 1; 0 1 0]), logical (out))
%!
%! out = [0 0 0 0 0 0 0 0 0 0
%! 0 0 0 0 0 0 0 0 0 0
%! 1 0 0 0 0 1 1 0 0 0
%! 1 0 0 0 1 0 0 0 0 0
%! 1 1 1 1 0 0 0 0 0 0
%! 0 1 0 1 1 0 0 0 0 0
%! 1 0 0 0 1 0 0 0 0 0
%! 0 0 0 1 1 0 0 0 0 0
%! 0 0 0 1 1 0 0 0 0 0
%! 0 0 0 1 1 1 0 0 0 0];
%! assert (bwareaopen (in, 10, 8), logical (out))
%! assert (bwareaopen (in, 10, ones (3)), logical (out))
%! assert (bwareaopen (in, 10), logical (out))
%!
%! out = [0 0 0 0 0 0 0 0 0 0
%! 0 0 0 0 0 0 0 0 0 0
%! 1 0 0 0 0 0 0 0 0 0
%! 1 0 0 0 0 0 0 0 0 0
%! 1 1 1 1 0 0 0 0 0 0
%! 0 1 0 1 1 0 0 0 0 0
%! 0 0 0 0 1 0 0 0 0 0
%! 0 0 0 1 1 0 0 1 0 0
%! 0 0 0 1 1 0 0 1 1 0
%! 0 0 0 1 1 1 0 0 1 0];
%! assert (bwareaopen (in, 4, [1 1 0; 1 1 1; 0 1 1]), logical (out))
%!error bwareaopen ("not an image", 78, 8)
%!error bwareaopen (rand (10) > 0.5, 10, 100)
%!error bwareaopen (rand (10) > 0.5, 10, "maximal")
%!error bwareaopen (rand (10) > 0.5, 10, [1 1 1; 0 1 1; 0 1 0])

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks