最大熵阈值分割

clear all;
I=imread('E:/pictures/meishi.jpg');
subplot 121;imshow(I);
if length(size(I))==3
    I=rgb2gray(I);
end
[X,Y]=size(I);
V_max=max(max(I));
V_min=min(min(I));
T0=(V_max+V_min)/2;
h=imhist(I);
grayp=imhist(I)/numel(I);
I=double(I);
H0=-sum(grayp(find(grayp(1:end)>0)).*log(grayp(find(grayp(1:end)>0))));
cout=100;
while(cout>0)
    Tmax=0;
    grayPd=0;grayPd=0;
    Hd=0;Hd=0;
    T1=T0;
    A1=0;A2=0;
    B1=0;B2=0;
    for i=1:X
        for j=1:Y
            if(I(i,j)<=T1)
                A1=A1+1;
                B1=B1+I(i,j);
            else
                A2=A2+1;
                B2=B2+I(i,j);
            end
        end    
    end
    M1=B1/A1;
    M2=B2/A2;
    T2=(M1+M2)/2;
    TT=round(T2);
    grayPd=sum(grayp(1:TT));
    if grayPd==0
        grayPd=eps;
    end
    grayPb=1-grayPd;
    if grayPb==0
        grayPb=eps;
    end
    Hd=-sum(grayp(find(grayp(1:TT)>0))/grayPd.*log((grayp(find(grayp(1:TT)>0))/grayPd)));
    Hb=-sum(grayp(TT+find(grayp(1+TT:end)>0))/grayPb.*log((grayp(TT+find(grayp(1+TT:end)>0))/grayPb)));
    H1=Hd+Hb;
    cout=cout-1;
    if(abs(H0-H1)<0.0001)|(cout==0)
        Tmax=T2;
        break;
    else
        T0=T2;
        H0=H1;
    end
end
Tmax
cout
for i=1:X
    for j=1:Y
        if (I(i,j)<=Tmax)
            I(i,j)=0;
        else 
            I(i,j)=1;
        end
    end
end
subplot 122;imshow(I);
    

    

  

转载于:https://www.cnblogs.com/StillWater-is-me/p/11180865.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值