用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 = [8;1;256;1;0;1;1];
- % 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);
- [Chrom ObjV]=reins(Chrom,SelCh,1,1,ObjV,ObjVSel);% Reinsert offspring into population
- gen = gen+1;% Increment counter
- end
- [Y,I]=max(ObjV);
- M=bs2rv(Chrom(I,:),FieldD)
- [m,n]=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);
- [U,V]=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本文内容由 frogfish 提供 |