1、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; end end for 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
2、);A=m*n;L=256;count=count/A;for i=1:L if count(i)=0 st=i-1; break; endendfor i=L:-1:1 if 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:m for j=1:n if a(i,j)th a(i,j)=255; else a(i,j)=0; end endendfigureimshow(a);处理前照片处理后照片: