clc;clear;I=imread('cameraman.tif');figure;imshow(I);title('原图像');I=double(I);[n,m]=size(I);%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%迭代法求阈值T=ones(100,1);T(2)=(min(I(:))+max(I(:)))/2;T(1)=0;t=2;whileabs(T(t)-T(t-1))1%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%a=0;A=0;b=0;B=0;fori=1:nforj=1:mifI(i,j)T(t)a=a+1;A=A+I(i,j);else%%循环的主体b=b+1;B=B+I(i,j);endendendu1=A/a;u2=B/b;t=t+1;T(t)=0.5*(u1+u2);%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%end%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%根据迭代法求得的阈值进行分割fori=1:nforj=1:mifI(i,j)T(t)I(i,j)=0;elseI(i,j)=1;endendend%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%显示处理图像figure;imshow(double(I));title('分割图像');