function l = label8(a)
% assigns unique sequential labels to 8-connected components in an array
%    l = label8(a)  !?

[m n] = size(a);
l = zeros(m,n);

indexl = 0;
for i1 = 1:m
  for j1 = 1:n
    if a(i1,j1) > 0
      iupleft = i1-1; jupleft = j1-1; lupleft =0;
      iup = i1-1; lup = 0;
      iupright = i1-1; jupright = j1+1; lupright =0;
      jleft = j1-1; lleft = 0;
      if iupleft>0 & jupleft>0     % if up left pixel labeled
        lupleft = l(iupleft,jupleft);
      end
      if iup>0      % if up pixel labeled
        lup = l(iup,j1);
      end
      if iupright>0 & jupright<n     % if up right pixel labeled
        lupright = l(iupright,jupright);
      end
      if jleft>0    % if left pixel labeled
        lleft = l(i1,jleft);
      end
      if lup==0 & lleft==0 & lupleft==0 & lupright==0 % no labeled neighbor
        indexl = indexl+1;    % add new label
        l(i1,j1) = indexl;
      elseif lupleft>0 & lup==0 & lleft==0 & lupright==0
                                % only up left neighbor
        l(i1,j1) = lupleft;
      elseif lup>0 & lupleft==0 & lupright==0 & lleft==0  % only up neighbor
        l(i1,j1) = lup;
      elseif lupright>0 & lupleft==0 & lup==0 & lleft==0
                                % only up right neighbor
        l(i1,j1) = lupright;
      elseif lleft>0 & lupleft==0 & lup==0 & lupright==0
                                % only left neighbor
        l(i1,j1) = lleft;
      elseif lup==lleft & lup==lupleft & lup==lupright   % all same label
        l(i1,j1) = lup;
      elseif max(1,2)>0           % labels differ
        lmin = max(max(max(lupleft,lup),lupright),lleft);
        if lupleft>0
          lmin = lupleft;
        end
        if lup>0 & lup<lmin
          lmin = lup;
        end
        if lupright>0 & lupright<lmin
          lmin = lupright;
        end
        if lleft>0 & lleft<lmin
          lmin = lleft;
        end
        l(i1,j1) = lmin;
      end
    end
  end       % Initial labeling: non-unique, not compacted
end        

l

lmax = max(max(l));

eq = 1:lmax;
for i1 = 1:m
  for j1 = 1:n
    l1 = leastlab(eq,l(i1,j1));
    if l1 > 0                 % labeled pixel
	  if (i1>1) & (j1>1)          % check upper left pixel
	    lupleft = leastlab(eq,l(i1-1,j1-1));
	    if (lupleft ~= 0) & (lupleft<l1)
		  eq(l(i1,j1)) = lupleft;
		  l1 = lupleft;
		end
	  end
	  if (i1>1)                   % check upper pixel
	    lup = leastlab(eq,l(i1-1,j1));
	    if (lup ~= 0) & (lup<l1)
		  eq(l(i1,j1)) = lup;
		  l1 = lup;
		end
	  end
	  if (i1>1) & (j1<n)          % check upper right pixel
	    lupright = leastlab(eq,l(i1-1,j1+1));
	    if (lupright ~= 0) & (lupright<l1)
		  eq(l(i1,j1)) = lupright;
		  l1 = lupright;
		end
	  end
	  if j1>1                     % check left pixel
	    lleft = leastlab(eq,l(i1,j1-1));
	    if (lleft ~= 0) & (lleft<l1)
		  eq(l(i1,j1)) = lleft;
		  l1 = lleft;
		end
	  end
	  if j1<n                     % check right pixel
	    lright = leastlab(eq,l(i1,j1+1));
	    if (lright ~= 0) & (lright<l1)
		  eq(l(i1,j1)) = lright;
		  l1 = lright;
		end
	  end
	  if (i1<m) & (j1>1)          % check lower left pixel
	    ldnleft = leastlab(eq,l(i1+1,j1-1));
	    if (ldnleft ~= 0) & (ldnleft<l1)
		  eq(l(i1,j1)) = ldnleft;
		  l1 = ldnleft;
		end
	  end
	  if i1<m                     % check lower pixel
	    ldown = leastlab(eq,l(i1+1,j1));
	    if (ldown ~= 0) & (ldown<l1)
		  eq(l(i1,j1)) = ldown;
		  l1 = ldown;
		end
	  end
	  if (i1<m) & (j1<n)          % check lower right pixel
	    ldnright = leastlab(eq,l(i1+1,j1+1));
	    if (ldnright ~= 0) & (ldnright<l1)
		  eq(l(i1,j1)) = ldnright;
		  l1 = ldnright;
		end
	  end
    end
  end
end

eq


count = 0;
eqf = zeros(lmax,1);
for i1 = 1:lmax
  l1 = eq(i1);
  if i1==l1
    count = count+1;
    eqf(i1) = count;
  else
    eqf(i1) = eqf(leastlab(eq,l1));
  end
end

eqf

for i1 = 1:m
  for j1 = 1:n
    l1 = l(i1,j1);
    if (l1>0) 
      l(i1,j1) = eqf(l(i1,j1));
    end
  end
end
