f=csvread('x0y0z0.csv',1,1)
t=0.5*(double(min(f(:)))+double(max(f(:))))
done=false;
while ~done
g=f>t
tnext=0.5*(mean(f(g))+mean(f(~g)))
done=abs(t-tnext)<0.5
t=tnext
end
line1=(line>=t)*255
%plot(1:5000,line,1:5000,line1,'*')
line2=line1(2:5000)
det=abs(line2(1:4999)-line1(1:4999))
plot(1:5000,line,1:4999,det,'r')