大津阈值分割法.docx
文本预览下载声明
clccleary=imread(mat4.ico);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 num = double(y(i,j))+1; nk(num) = nk(num)+1; endendfor i=1:L Ps(i)=nk(i)/n; end%figure(4),imhist(hist); %p=imhist(hist); figureimshow(a)count=imhist(a);[m,n]=size(a);A=m*n;L=256;count=count/A;for i=1:Lif count(i)~=0 st=i-1;break;endendfor i=L:-1:1if count(i)~=0 nd=i-1;break;endendf=count(st+1:nd+1); p=st; q=nd-st;u=0;for i=1:qu=u+f(i)*(p+i-1); ua(i)=u; end;for i=1:qw(i)=sum(f(1:i)); end;d=(u*w-ua).^2./(w.*(1-w));[y,tp]=max(d); th=tp+p;for i=1:mfor j=1:nif a(i,j)th a(i,j)=255;else a(i,j)=0;endendendfigureimshow(a);处理前照片处理后照片:
显示全部