/branches/R2-1-x/octave-forge/main/image/bwarea.m

# · MATLAB · 57 lines · 47 code · 10 blank · 0 comment · 4 complexity · cb8b9f16156f66200e4f650028394a13 MD5 · raw file

  1. ## Copyright (C) 2005 Sřren Hauberg
  2. ##
  3. ## This program is free software; you can redistribute it and/or modify
  4. ## it under the terms of the GNU General Public License as published by
  5. ## the Free Software Foundation; either version 2 of the License, or
  6. ## (at your option) any later version.
  7. ##
  8. ## This program is distributed in the hope that it will be useful,
  9. ## but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. ## GNU General Public License for more details.
  12. ##
  13. ## You should have received a copy of the GNU General Public License
  14. ## along with this program; if not, write to the Free Software
  15. ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. ## -*- texinfo -*-
  17. ## @deftypefn {Function File} @var{total}= bwarea(@var{bw})
  18. ## Estimates the area of the "on" pixels of @var{bw}.
  19. ## If @var{bw} is a binary image "on" pixels are defined as pixels
  20. ## valued 1. If @var{bw} is a grayscale image "on" pixels is defined
  21. ## as pixels with values larger than zero.
  22. ## This algorithm is not the same as counting the number of "on"
  23. ## pixels as it tries to estimate the area of the original object
  24. ## and not the image object.
  25. ## @end deftypefn
  26. ## Author: Sřren Hauberg <hauberg at gmail dot com>
  27. ##
  28. ## 2005-06-05 Sřren Hauberg <hauberg at gmail dot com>
  29. ## * Initial revision
  30. function total = bwarea(bw)
  31. if (isgray(bw))
  32. bw = (bw > 0);
  33. endif
  34. if (!isbw(bw))
  35. error("input image muste be either binary or gray scale.\n");
  36. endif
  37. four = ones(2);
  38. two = diag([1 1]);
  39. fours = conv2(bw, four);
  40. twos = conv2(bw, two);
  41. nQ1 = sum(fours(:) == 1);
  42. nQ3 = sum(fours(:) == 3);
  43. nQ4 = sum(fours(:) == 4);
  44. nQD = sum(fours(:) == 2 & twos(:) != 1);
  45. nQ2 = sum(fours(:) == 2 & twos(:) == 1);
  46. total = 0.25*nQ1 + 0.5*nQ2 + 0.875*nQ3 + nQ4 + 0.75*nQD;
  47. endfunction