function a = goodseg(b,l,f);
%
%  Attempts 'good' segementation of genetics bands, based of bprops output
%
%    a = goodseg(b,l,f)
%
%  On input:
%       b : properties of labels (bands) from bprops
%       l : original, labeled ridge image
%	f : original gray level image
%
%  On output:
%	a : labeled bands with good segmentation
%


[m,n] = size(l);
maxl = max(max(l));
maxgray = max(max(f)) / 6;   % these need to be figured out somehow
thresh = max(max(f)) / 4;
a = l;
numkill = 0;

big = b(1,5);
lastbig = b(1,5);
asize = 0;
for i1=1:maxl
  asize = asize + b(i1,5);
  if b(i1,5) > big
     lastbig = big; 
     big = b(i1,5);
  end
end
asize = round(asize / maxl);

cutsimple = lastbig / 6
if cutsimple < 3
  cutsimple = 3;
end

for i1=1:maxl
  killi1 = 0;

  % Check for minimum threshold and get rid of simpletons

  if (b(i1,5)<=cutsimple) % | (b(i1,8) < maxgray)
    killi1 = 1;
  end

  % Width should exceed height - from f27.m

  if (b(i1,6)<2*b(i1,7))		
    killi1 = 1;
  end;

  % Check upper and Lower neighbors for huge difference	

  if b(i1,13)~=0
    if (b(b(i1,13),8) - b(i1,8)) > thresh
      killi1 = 1;
      
      if b(i1,14)~=0 
        b(b(i1,14),13) = b(i1,13);   % Make this guys lower neighbor's upper 
	 % neighbor his original upper neighbor (if that makes any sense)
      end	     
    end
  end

  if b(i1,14)~=0
    if (b(b(i1,14),8) - b(i1,8)) > thresh
      killi1 = 1;
 
      if b(i1,13)~=0	
        b(b(i1,13),14) = b(i1,14);   % Same as above only reverse
      end	 
    end
  end
 
  if killi1 
    if (numkill<1)
      killar = i1;
      numkill = 1;
    else
      numkill = numkill + 1;	  	    
      killar = [killar, i1];
    end
  end
end


% kill all labels in killar

a = kill(l,killar,numkill);



