1## Copyright (C) 2005 Søren Hauberg <soren@hauberg.org> 2## 3## This program is free software; you can redistribute it and/or modify it under 4## the terms of the GNU General Public License as published by the Free Software 5## Foundation; either version 3 of the License, or (at your option) any later 6## version. 7## 8## This program is distributed in the hope that it will be useful, but WITHOUT 9## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 11## details. 12## 13## You should have received a copy of the GNU General Public License along with 14## this program; if not, see <http://www.gnu.org/licenses/>. 15 16## -*- texinfo -*- 17## @deftypefn {Function File} {@var{total} =} bwarea (@var{bw}) 18## Estimate total area of objects on the image @var{bw}. 19## 20## The image @var{bw} can be of any class, even non-logical, in which case non 21## zero valued pixels are considered to be an object. 22## 23## This algorithm is not the same as counting the number of pixels belonging to 24## an object as it tries to estimate the area of the original object. The value 25## of each pixel to the total area is weighted in relation to its neighbour 26## pixels. 27## 28## @seealso{im2bw, bweuler, bwperim, regionprops} 29## @end deftypefn 30 31function total = bwarea (bw) 32 if (nargin != 1) 33 print_usage; 34 elseif (!isimage (bw) || ndims (bw) != 2) 35 error("bwarea: input image must be a 2D image"); 36 elseif (!islogical (bw)) 37 bw = (bw != 0) 38 endif 39 40 four = ones (2); 41 two = diag ([1 1]); 42 43 fours = conv2 (bw, four); 44 twos = conv2 (bw, two); 45 46 nQ1 = sum (fours(:) == 1); 47 nQ3 = sum (fours(:) == 3); 48 nQ4 = sum (fours(:) == 4); 49 nQD = sum (fours(:) == 2 & twos(:) != 1); 50 nQ2 = sum (fours(:) == 2 & twos(:) == 1); 51 52 total = 0.25*nQ1 + 0.5*nQ2 + 0.875*nQ3 + nQ4 + 0.75*nQD; 53 54endfunction 55