function l = lung (im)
%  Does lung registration.  Returns an image with four labels :
%   2 lungs and two pieces above the shoulders.
%
%  l = lung (im)

first = min(min(im));
last = max(max(im)) / 2;

numlabels=5;

while numlabels~=4
  
  threshold = (first+last) / 2;  % also the mid-point

  threshold       % print it out so we know whats going on (debug)

  imt = ~thresh(im,threshold);
  
  imtl = label(imt);

  [newimtl,numlabels] = group(imtl);

  numlabels
 
  if numlabels > 4        % later we will use a rating system based on
     last = threshold;	  % more than just num of labels.
   else			  % for now, we need this working though.
     first = threshold;
  end

  

end 
  
  l = newimtl;
  