functionmain()
clearall
closeall
clc
globalchrom oldpop fitness lchrom popsize cross_rate mutation_rate yuzhisum
globalmaxgen m n fit gen yuzhi A B C oldpop1 popsize1 b b1 fitness1 yuzhi1
A=imread('mh.jpg');%读入道路图像
%A=imresize(A,0.4);
B=rgb2gray(A);%灰度化
C=B;
C=imresize(B,0.1);%将读入的图像缩小
lchrom=8;%染色体长度
popsize=10;%种群大小
cross_rate=0.7;%交叉概率
mutation_rate=0.4;%变异概率
maxgen=150;%最大代数
[m,n]=size(C);
initpop;%初始种群
forgen=1:maxgen
generation;%遗传操作
end
findresult;%图象分割结果
%输出进化各曲线
figure;
gen=1:maxgen;
plot(gen,fit(1,gen));
title('最佳适应度值进化曲线');
%初始化种群%
functioninitpop()
globallchrom oldpop popsize chrom C
imshow(C);
fori=1:popsize
chrom=rand(1,lchrom);
forj=1:lchrom
ifchrom(1,j)<0.5
chrom(1,j)=0;
else
chrom(1,j)=1;
end
end
oldpop(i,1:lchrom)=chrom;
end
%产生新一代个体%
functiongeneration()
fitness_order;%计算适应度值及排序
select;%选择操作
crossover;%交叉
mutation;%变异
%计算适度值并且排序%
functionfitness_order()
globallchrom oldpop fitness popsize chrom fit gen C m n fitness1 yuzhisum
globallowsum higsum u1 u2 yuzhi gen oldpop1 popsize1 b1 b yuzhi1
ifpopsize>=5
popsize=ceil(popsize-0.03*gen);
end
ifgen==75%当进化到末期的时候调整种群规模和交叉、变异概率
cross_rate=0.3;%交叉概率