function s = sameh(a,h,t)
%   break up segments that have different heights
%       s = sameh(a,h)

%  sameh: create new segments for any segment with connected
%         subsegments with different heights
%
%  on input:
%
%    a:     2D array of labeled segments
%    h:     2D array of heights at pixels
%
%  on output:
%
%    s:	    2D array of labeled segments of uniform height
%
%  Authors:	Tom Henderson
%		Univ of Utah
%		8 Sept 1996
%

s = a;
lmax = max(max(a));
for l1 = 1:lmax
  [a1,ioff,joff] = lext(a,l1);
  s1 = getseg(a1,l1);
  [len1,two] = size(s1);
  if len1>3                  % #parameter: only does size 4 and up segs
    d1 = [];  ind1 = [];
    for index1 = 1:(len1-1)
      i1 = s1(index1,1);
      j1 = s1(index1,2);
      for index2 = index1+1:len1
        i2 = s1(index2,1);
        j2 = s1(index2,2);
        if abs(i1-i2)<2 & abs(j1-j2)<2
          d1 = [d1 ; abs(h(i1+ioff,j1+joff)-h(i2+ioff,j2+joff))];
          ind1 = [ind1; i1 j1 i2 j2];
        end
      end
    end
    d1max = length(d1);
    d1std = std(d1);
    d1mean = mean(d1);
    for index1 = 1:d1max
      if d1(index1) > d1mean+t*d1std
        a1(ind1(index1,1),ind1(index1,2)) = 0;
        a1(ind1(index1,3),ind1(index1,4)) = 0;
      end
    end
    lab1 = labelr8(a1);
    if max(max(lab1))>1
      for index1 = 1:d1max
        if d1(index1) > d1mean+t*d1std
          s(ind1(index1,1)+ioff,ind1(index1,2)+joff) = 0;
          s(ind1(index1,3)+ioff,ind1(index1,4)+joff) = 0;
        end
      end
    end
  end
end
s = labelr8(s);
