frogfish 发表于 2007-7-25 08:29

遗传算法的图象分割matlab程序

用matlab写的遗传算法分割程序,用的是Sheffield大学gatbs工具箱
能够运行得到真确结果!

a10.m
O=imread('Lenna.bmp');
figure(1);imshow(O);

NIND = 40; % Number of individuals
MAXGEN =50; % Maximum no. of generations
PRECI = 8; % Precision of variables
GGAP = 0.9; % Generation gap
% Build field descriptor
FieldD = ;
% Initialise population
Chrom = crtbp(NIND, PRECI);
gen = 0;% Counter
phen=bs2rv(Chrom,FieldD);% Evaluate initial population
ObjV = target(O,phen);
while gen < MAXGEN,% Generational loop
% Assign fitness values to entire population
FitnV = ranking(-ObjV);
SelCh = select('sus', Chrom, FitnV, GGAP);% Select individuals for breeding
SelCh = recombin('xovsp',SelCh,0.7);% Recombine individuals (crossover)
SelCh = mut(SelCh);% Apply mutation
phenSel = bs2rv(SelCh,FieldD);% Evaluate offspring, call objective function
ObjVSel=target(O,phenSel);
=reins(Chrom,SelCh,1,1,ObjV,ObjVSel);% Reinsert offspring into population
gen = gen+1;% Increment counter
end
=max(ObjV);
M=bs2rv(Chrom(I,:),FieldD)
=size(O);
for i=1:m
    for j=1:n
      if O(i,j)>M
            O(i,j)=256;
      end
    end
end
figure(2)
imshow(O);


target.m
function f=target(T,M)
dT=double(T);
=size(T); W=length(M);f=zeros(W,1);
for k=1:W
s1=0;I=0;s2=0;J=0;
for i=1:U
    for j=1:V
      if T(i,j)<=M(k)
            s1=s1+dT(i,j);I=I+1;
      end
      if T(i,j)>M(k)
            s2=s2+dT(i,j);J=J+1;
      end
    end
end
p1=s1/I;p2=s2/J;
f(k)=I*J*(p1-p2)*(p1-p2)/(256*256*256*256);
end

本程序由研学hwh4103

amanda-mat 发表于 2009-6-27 10:42

能不能说一下具体要怎么运行?还需要什么初始文件?图像分割程序,那是不是要图像的原始文件啊?谢谢!

hjd2003 发表于 2012-4-5 15:59

谢谢楼主分享!学习学习!!!!!
页: [1]
查看完整版本: 遗传算法的图象分割matlab程序