function [l,lm] = lanes1(bands,mask)
% [l,lm] = lanes1(bands,mask)
[bm bn] = size(bands);
l = bands;

above = zeros(bm,1);
[m n] = size(mask);
lm = zeros(m,n);
for n1 = 1:bm-1
  left1 = bands(n1,1); right1 = bands(n1,2);
  width1 = right1 - left1;
  if bands(n1,20)==n1
    for n2 = n1+1:bm
      left2 = bands(n2,1); right2 = bands(n2,2);
      width2 = right2 - left2;
      if above(n2) == 0
        rowa = 0;
      else
        rowa = bands(above(n2),15);
      end
      if (min(right1,right2)-max(left1,left2)) > 0.5*min(width1,width2) ...
         & bands(n2,20)==n2 & rowa < bands(n1,15)
        above(n2) = n1;
      end
    end
  end
end
for n1 = 1:bm-1
  for n2 = n1+1:bm
    n3 = above(n1); n4 = above(n2);
    if (n3==n4) & (n3>0)
      i1 = bands(n1,15); j1 = bands(n1,16);
      i2 = bands(n2,15); j2 = bands(n2,16);
      i3 = bands(n3,15); j3 = bands(n3,16);
      dist13 = sqrt((i3-i1)*(i3-i1) + (j3-j1)*(j3-j1));
      dist23 = sqrt((i3-i2)*(i3-i2) + (j3-j2)*(j3-j2));
      if dist13 < dist23
        above(n2) = 0;
      else
        above(n1) = 0;
      end
    end
  end
end
for n1 = 1:bm
  l(n1,19) = above(n1);
  n2 = above(n1);
  if n2 > 0
    j2 = bands(n2,16);
    imin = bands(n2,15);
    imax = bands(n1,15);
    for i2 = imin:imax
      lm(i2,j2) = 255;
    end
  end
end
