大津阈值分割法.docclc clear y=imread( '' ); a=rgb2gray(y); L=256;
Ps = zeros(L,1); nk=zeros(L,1); [row,col]=size(y); n=row*col; for i = 1:row for j = 1:col
end
num = double(y(i,j))+1; nk(num) = nk(num)+1; end for i=1:L
Ps(i)=nk(i)/n;
end
%figure(4),imhist(hist); %p=imhist(hist); figure imshow(a) count=imhist(a); [m,n]=size(a);
A=m*n;
L=256; count=count/A; for i=1:L if count(i)~=0
st=i-1; break ; end end for i=L:-1:1 if count(i)~=0 nd=i-1;
break ;
end
end
f=count(st+1:nd+1);
p=st; q=nd-st;
u=0;
for i=1:q u=u+f(i)*(p+i-1); ua(i)=u;
end;
for i=1:q w(i)=sum(f(1:i));
end;
d=(u*w-ua).八2./(w.*(1-w));
[y,tp]=max(d);
th=tp+p;
for i=1:m
for j=1:n
if a(i,j)>th
a(i,j)=255;
else
a(i,j)=0;
end
end
end
figure
imshow(a);
大津阈值分割法 来自淘豆网m.daumloan.com转载请标明出处.