clear, close, clc
a=imread('tekst1.jpg');
a=medfilt2(a, [3 3]);
b = im2double(a);

prag=0.5;
prag1=1;
while prag1-prag>eps,   
m1 = mean(b(b < prag));
m2 = mean(b(b > prag));
prag1=prag;
prag=(m1+m2)/2;
end
binarna = im2bw(b, prag);

figure,
subplot(121), imshow(a), title('Orginalna slika')
subplot(122),imshow(binarna), title('Segmentirana slika')
round(prag*255)