数字图像处
4.图像分割
H=imread('C:\Users\Administrator\Desktop\zhengqiji.jpg'); H=rgb2gray(H); T1=mean(H(:)); a=T1;
[m,n]=size(H); B=zeros(m,n); D=H;
t=0;T=0;bottle1=0;bottle2=0;n1=0;n2=0;
---------------迭代法确定阈值------------------------------ while T1~=T
T=T1;bottle1=0;bottle2=0;n1=0;n2=0; for i=1:m for j=1:n
B(i,j)=H(i,j); if B(i,j)>T
(1)
数字图像处
(2) else bottle2=bottle2+B(i,j); n2=n2+1; (3) T1=(bottle1/n1+bottle2/n2)/2; end end end end
-------------------------用阈值对图像进行处理---------------------- for i=1:m for j=1:n if H(i,j)>T D(i,j)=0; else
D(i,j)=H(i,j);
end end end
imshow(D)