2222

clear ;
clc;
tic
a=imread('E:\lena.jpg');
I=rgb2gray(a);
z=imhist(I);
[m n]=size(I);
N=m*n;
for i=1:256
clear ;
clc;
tic
a=imread('E:\lena.jpg');
I=rgb2gray(a);
z=imhist(I);
[m n]=size(I);
N=m*n;
for i=1:256
    p(i)=z(i)/N;
end
for i=0:254
    for j=i+1:255 
        w0=0;
        w1=0;
        w2=0;
        for k=0:255
            if k<=i
                w0=w0+p(k+1);
            elseif k<=j
                w1=w1+p(k+1);
            else
                w2=w2+p(k+1);
            end
        end
        q0=0;
        q1=0;
        q2=0;
        if w0*w1*w2>1e-10
                for k=0:i
                    q0=q0+p(k+1)/w0*(exp(1-p(k+1)/w0));
                    
                end
                for k=i+1:j
                   q1=q1+p(k+1)/w1*(exp(1-p(k+1)/w1));
                   
                end
                for k=j+1:255
                    q2=q2+p(k+1)/w2*(exp(1-p(k+1)/w2));
                    
                end
                F(i+1,j+1)=q0+q1+q2;
          else
                F(i+1,j+1)=0;
        end 
    end
end
Fmax=max(F(:));
for i=1:254
    for j=2:255
        if F(i,j)==Fmax
                k1=i-1;
                k2=j-1;
               continue;
         end
    end
end
 toc
 tic
z=ones(m,n);
for i=1:m
    for j=1:n
        if I(i,j)<=k1
            z(i,j)=0;
        elseif I(i,j)<=k2
            z(i,j)=0.5;
       end
    end
end
 toc      
k1
k2
figure
subplot(1,2,1)
imshow(I)
subplot(1,2,2)
imshow(z)           

p(i)=z(i)/N;endfor i=0:254 for j=i+1:255 w0=0; w1=0; w2=0; for k=0:255 if k<=i w0=w0+p(k+1); elseif k<=j w1=w1+p(k+1); else w2=w2+p(k+1); end end q0=0; q1=0; q2=0; if w0*w1*w2>1e-10 for k=0:i q0=q0+p(k+1)/w0*(exp(1-p(k+1)/w0)); end for k=i+1:j q1=q1+p(k+1)/w1*(exp(1-p(k+1)/w1)); end for k=j+1:255 q2=q2+p(k+1)/w2*(exp(1-p(k+1)/w2)); end F(i+1,j+1)=q0+q1+q2; else F(i+1,j+1)=0; end endendFmax=max(F(:));for i=1:254 for j=2:255 if F(i,j)==Fmax k1=i-1; k2=j-1; continue; end endend toc ticz=ones(m,n);for i=1:m for j=1:n if I(i,j)<=k1 z(i,j)=0; elseif I(i,j)<=k2 z(i,j)=0.5; end endend toc k1k2figuresubplot(1,2,1)imshow(I)subplot(1,2,2)imshow(z)
      

你可能感兴趣的:(2222)