[2D] Largest Area

  1. function [outim] = largestarea(im,threshold)
  2. % Tim Zaman © 2010, TU Delft
  3. % 'largestarea' reads in a 2-d binary image and outputs a binary image,
  4. %   retaining only the largest blobs larger than the largest by a threshold.
  5. %
  6. % Usage: [outim] = largestarea(im,threshold)
  7. %
  8. % im - 2-d binary image
  9. % outim - Output binary image (with 1s and 0s)
  10. %
  11. % Example:
  12. %
  13. % im = imread('text.png');
  14. % outim = bwlargestblob(im,8);
  15. connectivity=4;
  16. if size(im,3)>1,
  17. error('largestarea accepts only 2 dimensional images');
  18. end
  19. [imlabel totalLabels] = bwlabel(im,connectivity);
  20. sizeBlob = zeros(1,totalLabels);
  21. for i=1:totalLabels,
  22. sizeblob(i) = length(find(imlabel==i));
  23. end
  24. max_global=max(sizeblob);
  25. i=0;
  26. while max(sizeblob)> max_global*threshold
  27. i=i+1;
  28. [maxno largestBlobNo(i)] = max(sizeblob);
  29. sizeblob(largestBlobNo(i))=1;
  30. end
  31. outim = zeros(size(im),'uint8');
  32. for ii=1:i
  33. outim(find(imlabel==largestBlobNo(ii))) = 1;
  34. end
  35. outim=logical(outim);
  36. end
]]>

You may also like...