⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 threshold_ksw.asv

📁 遗传算法就的开始株型紧凑可执行:早晨从看见着在科学考察偶支持
💻 ASV
字号:
%利用最佳直方图熵法(KSW熵法)实现灰度图像阈值分割
%暂未涉及遗传算法

filename=input('输入预进行阈值分割的灰度图像的文件名及路径:','s');

disp('指定阈值搜索范围(0~1):');
lowvalue=input('下限值:');
highvalue=input('上限值:');

I=imread(filename);

info=imfinfo(filename);
maxgrayvalue=info.MaxSampleValue;
mingrayvalue=info.MinSampleValue;
graynum=maxgrayvalue-mingrayvalue+1;

hist=imhist(I);
total=0;
for i=mingrayvalue:maxgrayvalue
    total=total+hist(i+1);
end
hist1=hist/total;

HT=0;
for i=mingrayvalue:maxgrayvalue
    if hist1(i+1)==0
        temp=0;
    else
        temp=-hist1(i+1)*log(hist1(i+1));
    end
    HT=HT+temp;
end


%
lowvalue1=round(lowvalue*graynum);
highvalue1=round(highvalue*graynum);

for t=lowvalue1:highvalue1
    
    Pt=0;
    for i=1:t
        Pt=Pt+hist1(i);
    end
    
    Ht=0;
    for i=1:t
        if hist1(i)==0
           temp=0;
        else
           temp=-hist1(i)*log(hist1(i));
        end
        Ht=Ht+temp;
    end
    
    if Pt==0
        
    H(t)=log(Pt*(1-Pt))+Ht/Pt+(HT-Ht)/(1-Pt);
    
end

H(mingrayvalue+1:lowvalue1-1)=0;
H(highvalue1+1:maxgrayvalue+1)=0;

max_shannon=max(H);
t_opt=find(H==max_shannon);

threshold_opt=t_opt/graynum;

I1=im2bw(I,threshold_opt);

disp('灰度图像阈值分割的效果如图所示:');
disp('源图为:Fifure No.1');
disp('最佳直方图熵法阈值分割后的图像为:Fifure No.2');
disp('Otsu法阈值分割后的图像为:Fifure No.3');

figure(1);
imshow(I);
title('源图');

figure(2);
imshow(I1);
title('最佳直方图熵法阈值分割后的图像');

level=graythresh(I);
I2=im2bw(I,level);
figure(3);
imshow(I2);
title('Otsu法阈值分割后的图像');








    
        

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -