
function S = scrap(I, minsize, conn)

  if (nargin() == 2)
    conn = 8;
  endif
 
  if ~(islogical(I))
    I = logical(I);
  endif
  L = bwlabel(I, conn);

  stats = regionprops(L, 'Area', 'PixelIdxList');
  S = I;

  for region = 1 : length(stats)
    if stats(region).Area < minsize
      S(stats(region).PixelIdxList) = 0;
    endif
  endfor

endfunction
