%Example10_7
f=imread('scanned-text.tif');
imshow(f);
pause
imhist(f)
axis([0 255 0 61000])
pause
%f=double(f);
T=0.5*(double(min(f(:)))+double(max(f(:))))
done=false;
while ~done
   g=(f>=T);    % Segments forground from background
   my1= mean(f(g));  % 
   my2=mean(f(~g));
   Tnext =0.5*(my1+my2);
   done=abs(T-Tnext)<0.5;
   T=Tnext
end
g=(f>=T);    % Segment with final threshold
figure;imshow(g)
pause
% Otsu's method
T2=graythresh(f)
T2=T2*255
g2=(f>=T2);    % Segment with Otsu's threshold
figure;imshow(g2)
pause

close all