声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

声振论坛 展示 基础理论 查看内容

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

2011-4-1 07:53| 发布者: 雪缘| 查看: 3466| 评论: 0|来自: 振动论坛

摘要: 用matlab写的遗传算法分割程序,用的是Sheffield大学gatbs工具箱 能够运行得到真确结果! a10.m O=imread('Lenna.bmp'); figure(1);imshow(O); NIND = 40; % Number of individuals MAXGEN =50; % Maximum ...
用matlab写的遗传算法分割程序,用的是Sheffield大学gatbs工具箱
能够运行得到真确结果!

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

  3. NIND = 40; % Number of individuals
  4. MAXGEN =50; % Maximum no. of generations
  5. PRECI = 8; % Precision of variables
  6. GGAP = 0.9; % Generation gap
  7. % Build field descriptor
  8. FieldD = [8;1;256;1;0;1;1];
  9. % Initialise population
  10. Chrom = crtbp(NIND, PRECI);
  11. gen = 0;% Counter
  12. phen=bs2rv(Chrom,FieldD);% Evaluate initial population
  13. ObjV = target(O,phen);
  14. while gen < MAXGEN,% Generational loop
  15. % Assign fitness values to entire population
  16. FitnV = ranking(-ObjV);
  17. SelCh = select('sus', Chrom, FitnV, GGAP);% Select individuals for breeding
  18. SelCh = recombin('xovsp',SelCh,0.7);% Recombine individuals (crossover)
  19. SelCh = mut(SelCh);% Apply mutation
  20. phenSel = bs2rv(SelCh,FieldD);% Evaluate offspring, call objective function
  21. ObjVSel=target(O,phenSel);
  22. [Chrom ObjV]=reins(Chrom,SelCh,1,1,ObjV,ObjVSel);% Reinsert offspring into population
  23. gen = gen+1;% Increment counter
  24. end
  25. [Y,I]=max(ObjV);
  26. M=bs2rv(Chrom(I,:),FieldD)
  27. [m,n]=size(O);
  28. for i=1:m
  29.     for j=1:n
  30.         if O(i,j)>M
  31.             O(i,j)=256;
  32.         end
  33.     end
  34. end
  35. figure(2)
  36. imshow(O);
复制代码



target.m
  1. function f=target(T,M)
  2. dT=double(T);
  3. [U,V]=size(T); W=length(M);f=zeros(W,1);
  4. for k=1:W
  5. s1=0;I=0;s2=0;J=0;
  6. for i=1:U
  7.     for j=1:V
  8.         if T(i,j)<=M(k)
  9.             s1=s1+dT(i,j);I=I+1;
  10.         end
  11.         if T(i,j)>M(k)
  12.             s2=s2+dT(i,j);J=J+1;
  13.         end
  14.     end
  15. end
  16. p1=s1/I;p2=s2/J;
  17. f(k)=I*J*(p1-p2)*(p1-p2)/(256*256*256*256);
  18. end
复制代码


本程序由研学hwh4103

本文内容由 frogfish 提供

最新评论

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-11-24 18:34 , Processed in 0.030098 second(s), 15 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部