[name,path]=uigetfile('*.jpg;*.png;*.bmp','选择一张图片');
f=imread([path name]);
count = 0;
T = mean2(f);
done = false;
while ~done
countcount = count + 1;
g = f > T;
Tnext = 0.5*(mean(f(g)) + mean(f(~g)));
done = abs(T - Tnext) < 0.5;
T = Tnext;
end
g = im2bw(f, T/255);
subplot(121);
imshow(f)
subplot(122);
imshow(g);
![](https://img2024.cnblogs.com/blog/35695/202408/35695-20240826070647288-243310440.jpg)