对这段遗传算法实现实现灰度图像阈值分割的代码理解不是很清楚,求帮忙注释一下关键代码段


clear;
close all;
clc;

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

I=imread(filename);

info=imfinfo(filename);

mingrayvalue=info.MinSampleValue;
maxgrayvalue=info.MaxSampleValue;
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(1/hist1(i+1));
end
HT=HT+temp;
end

%%程序主干部分

%种群随机初始化,种群数取10,染色体二进制编码取8位

population=10;

X0=round(rand(1,population)*graynum);

for i=1:population
adapt_value0(i)=ksw(X0(i),mingrayvalue,maxgrayvalue,hist1,HT);
end

adapt_average0=mean(adapt_value0);

X1=X0;
adapt_value1=adapt_value0;
adapt_average1=adapt_average0;

%循环搜索,搜索代数取50

generation=100;

for k=1:generation

s1=select(X1,adapt_value1);

s_code1=dec2bin(s1,8);

c1=cross(s_code1);

v1=mutation(c1);

X2=(bin2dec(v1))';

for i=1:population
adapt_value2(i)=ksw(X2(i),mingrayvalue,maxgrayvalue,hist1,HT);
end

adapt_average2=mean(adapt_value2);

if abs(adapt_average2-adapt_average1)<=0.0001
break;
else
X1=X2;
adapt_value1=adapt_value2;
adapt_average1=adapt_average2;
end
end

max_value=max(adapt_value2);
number=find(adapt_value2==max_value);
t_opt=round(mean(X2(number)));
%%阈值分割及显示部分

threshold_opt=t_opt/graynum;

I1=im2bw(I,threshold_opt);

disp('灰度图像阈值分割的效果如图所示:');
disp('源图为:Fifure No.1');
disp('遗传算法阈值分割后的图像为:Fifure No.2');
disp('最大类间方差法阈值分割后的图像为:Fifure No.3');

figure(1);
imshow(I);
title('原始图像');

figure(2);
imshow(I1);
title('遗传算法阈值分割后的图像');

level=graythresh(I);
level1=round(level*graynum);
I2=im2bw(I,level);
figure(3);
imshow(I2);
title('最大类间方差法阈值分割后的图像');

disp('遗传算法阈值为:');
disp(t_opt);
disp('最大类间方差法阈值为:');
disp(level1);

%%程序结束

c matlab

べ悠然飘过べ 11 years, 10 months ago

Your Answer