function [newl,numlabels] = group(im)
%
% group: groups a labeled image, gets rid of insignificants, etc.
% On input:
%     im: labeled image
% On output:
%     newl: relabeled image with groups
%     numlabels: number of groups
% Call:
%     [newl, numlabels] = group(im)
%

  [m,n] = size(im);

  l = label4(im);

  numlabels = max(max(l));

  features = zeros(numlabels,5);   % Initialize features structure

  % 1 = Area
  % 2 = MinX  3 = MaxX
  % 4 = MinY  5 = MaxY

  o = ones(m,n);

  for i1 = 1:numlabels
    features(i1,1) = sum(sum(l == i1*o));
  end


%  for i1 = 1:m
%    for j1 = 1:n
%      if l(i1,j1)
%        features(l(i1,j1),1) = features(l(i1,j1),1) + 1;

        %if (i1 < features(l(i1,j1),2)) | (~features(l(i1,j1),2))
	%  features(l(i1,j1),2) = i1;
 	%end
	%if (i1 > features(l(i1,j1),3))
  	%  features(l(i1,j1),3) = i1;
	%end 

	%if (j1 < features(l(i1,j1),4)) | (~features(l(i1,j1),4))
	%  features(l(i1,j1),4) = j1;
	%end
	%if (j1 > features(l(i1,j1),5))
  	%  features(l(i1,j1),5) = j1;
	%end

%      end

%    end
%  end

  
  for i1 = 1:numlabels 			% get rid of any insignificant blobs
    if features(i1,1) < ((m*n) / 64)	
       temp = (l ~= i1*o);
       l = l.*temp;
       numlabels = numlabels - 1;
    end
  end

  % Now, lets re-label the image, to shift the labels down so that there are
  % no "holes" in the label numbers

  %newl = label(l);
  %numlabels = max(max(newl));
 
  newl = l;

%  for i1 = 1:numlabels		% get rid of the fake holes from label.m
%    if features(i1,1) == 0
%      numlabels	
%      numlabels = numlabels - 1;
%    end
%  end

