请选择 进入手机版 | 继续访问电脑版

Hello Mat

 找回密码
 立即注册
查看: 5652|回复: 0

PSO的一维最大熵法阈值分割

[复制链接]

1294

主题

1520

帖子

110

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22633
发表于 2019-10-10 21:53:52 | 显示全部楼层 |阅读模式
PSO的一维最大熵法阈值分割
链接:https://pan.baidu.com/s/1ChO4xGObCiTMqRT_s_qDpg&shfl=sharepset 提取码:zoad
具体链接在halcom.cn论坛,联系人QQ:3283892722
该论坛是一个学习交流平台,我会逐一的和大家分享学习。
欢迎大家录制视频,你可在论坛进行打赏分享。
视频专用播放器:http://halcom.cn/forum.php?mod=viewthread&tid=258&extra=page%3D1\

  1. clc,clear,close all
  2. warning off
  3. format longG
  4. tic;
  5. % 2个阈值,实现3分类分割
  6. I = imread('car.bmp');
  7. % I = imread('screen.jpg');
  8. I = imresize(I, [256,256]);
  9. if(size(I,3)>1)
  10.     im = rgb2gray(I);
  11. else
  12.     im = I;
  13. end
  14. figure('color',[1,1,1]), imshow(im,[])
  15. %% PSO 参数
  16. c1 = 1.4995;    % 学习因子
  17. c2 = 1.4995;   % 学习因子
  18. Vmin = -5;   % 最小速度
  19. Vmax = 5;    % 最大速度
  20. maxiter = 30;  % 迭代次数
  21. sizepop = 20;  % 种群数量
  22. popmin = 1;  popmax = 255; % x1
  23. Lmax = 256;
  24. % 2个阈值,实现3分割
  25. level = 3;  
  26. nvar = level-1;

  27. %% 初始化种群
  28. for i=1:sizepop
  29.     x1 = fix( popmin + (popmax-popmin)*rand );
  30.     x2 = fix( popmin + (popmax-popmin)*rand );
  31.     pop(i,1) = x1;
  32.     pop(i,2) = x2;
  33.     pop(i,:)=sort(pop(i,:));
  34.     fitness(i) = H1_segFun2( im, pop(i,:) );
  35.     V(i,1) = 0;
  36.     V(i,2) = 0;
  37. end
  38. % 记录一组最优值
  39. [bestfitness,bestindex]=max(fitness);
  40. zbest=pop(bestindex,:);   % 全局最佳
  41. gbest=pop;                % 个体最佳
  42. fitnessgbest=fitness;     % 个体最佳适应度值
  43. fitnesszbest=bestfitness; % 全局最佳适应度值
  44. wmax = 0.9;  wmin = 0.4;
  45. % 迭代寻优
  46. for i=1:maxiter
  47.     for j=1:sizepop
  48.         % 自适应权重1
  49. %         w = wmin + (wmax-wmin)*(fitnessgbest(j)-min(fitness))/( max(fitness)-min(fitness) );
  50.         % 自适应权重2
  51. %         w = wmin - (wmax-wmin)*(fitnessgbest(j)-min(fitness))/( mean(fitness)-min(fitness) );
  52.         % 自适应权重3
  53.         if fitnessgbest(j)<=mean(fitness)
  54.             w = wmin - (wmax-wmin)*(fitnessgbest(j)-min(fitness))/( mean(fitness)-min(fitness) );
  55.         else
  56.             w = wmax;
  57.         end
  58.         % 速度更新
  59.         V(j,:) = w*V(j,:) + c1*rand*(gbest(j,:) - pop(j,:)) + c2*rand*(zbest - pop(j,:));
  60.         % V--范围约束
  61.         V(j, find(V(j,:)>Vmax)) = Vmax;
  62.         V(j, find(V(j,:)<Vmin)) = Vmin;
  63.         
  64.         % 个体更新
  65.         pop(j,:) = fix( pop(j,:) + 0.5 * V(j,:) );
  66.         
  67.         % pop  越界限制
  68.         pop(j, find(pop(j,:)>popmax)) = popmax;
  69.         pop(j, find(pop(j,:)<popmin)) = popmin;
  70.         
  71.         pop(j,:) = sort(pop(j, :));
  72.         
  73.         % 适应度更新
  74.         fitness(j) = H1_segFun2(im, pop(j,:));
  75.         
  76.         % 比较  个体间比较
  77.         if fitness(j)>fitnessgbest(j)
  78.             fitnessgbest(j) = fitness(j);
  79.             gbest(j,:) = pop(j,:);
  80.         end
  81.         if fitness(j)>bestfitness
  82.             bestfitness = fitness(j);
  83.             zbest =  pop(j,:);
  84.         end
  85.         
  86.     end
  87.     fitness_iter(i) = bestfitness;
  88.    
  89. end
  90. toc ;
  91. times = toc;
  92. fprintf('\n')
  93. disp(['计算时间 Time =    ',   num2str(times) ])
  94. fprintf('\n')
  95. zbest = sort(zbest);
  96. disp(['最优解   ', num2str(zbest)])
  97. fprintf('\n')

  98. figure('color',[1,1,1])
  99. plot(fitness_iter,'ro-','linewidth',2)

  100. % 显示
  101. Iout = imageGray( im, zbest );
  102. figure('color',[1,1,1])
  103. imshow(Iout,[])
  104. colormap jet;
  105. axis equal
复制代码
适应度函数:
  1. function maxH = H1_segFun2(I, t)
  2. % 最原始的一维最大熵法,进行图像的3分割,level=3,将图像分为3部分

  3. [n_count, x_value] = imhist(I(:,:,1));  % 第一通道来分割
  4. Nt = size(I,1)*size(I,2);
  5. Lmax = 256;  % 最大灰阶, uin8
  6. for i=1:Lmax
  7.     p(i) = n_count(i)/Nt;   % 概率分布
  8. end
  9. % H0 = -1* sum( p(1:t).*log(p(1:t) + eps)  );
  10. % HB = -1* sum( p(t+1:Lmax).*log(p(t+1:Lmax) + eps) );
  11. % maxH = H0+HB;  % 一维最大熵
  12. % 改进
  13. % p1 = log( sum(p(1:t)) + eps );
  14. % H0 = sum( p(1:t).*log(p(1:t) + eps)  )./ ( sum(p(1:t)) + eps );
  15. % p2 = log( sum(p(t+1:Lmax)) + eps );
  16. % HB = sum( p(t+1:Lmax).*log(p(t+1:Lmax) + eps) )./ ( sum(p(t+1:Lmax)) + eps );
  17. % maxH = p1+p2-H0-HB;  % 一维最大熵

  18. % % 3分割
  19. % t1 = t(1);
  20. % t2 = t(2);
  21. % p1 = log( sum(p(1:t1)) + eps );
  22. % p2 = log( sum(p(t1+1:t2)) + eps );
  23. % p3 = log( sum(p(t2+1:Lmax)) + eps );
  24. % p4 = sum( p(1:t1).*log(p(1:t1) + eps)  )./ ( sum(p(1:t1)) + eps );
  25. % p5 =  sum( p(t1+1:t2).*log(p(t1+1:t2) + eps) )./ ( sum(p(t1+1:t2)) + eps );
  26. % p6 = sum( p(t2+1:Lmax).*log(p(t2+1:Lmax) + eps) )./ ( sum(p(t2+1:Lmax)) + eps );
  27. % maxH = p1+p2+p3-p4-p5-p6;  % 一维最大熵

  28. % 3分割
  29. % t1 = t(1);
  30. % t2 = t(2);
  31. p1 = log( sum(p(1:t(1))) + eps );
  32. p2 = log( sum(p(t(1)+1:t(2))) + eps );
  33. p3 = log( sum(p(t(2)+1:Lmax)) + eps );
  34. p4 = sum( p(1:t(1)).*log(p(1:t(1)) + eps)  )./ ( sum(p(1:t(1))) + eps );
  35. p5 =  sum( p(t(1)+1:t(2)).*log(p(t(1)+1:t(2)) + eps) )./ ( sum(p(t(1)+1:t(2))) + eps );
  36. p6 = sum( p(t(2)+1:Lmax).*log(p(t(2)+1:Lmax) + eps) )./ ( sum(p(t(2)+1:Lmax)) + eps );
  37. maxH = p1+p2+p3-p4-p5-p6;  % 一维最大熵
复制代码


参考:【32】一维最大熵法进行2分割和3分割(MATLAB)


算法QQ  3283892722
群智能算法链接http://halcom.cn/forum.php?mod=forumdisplay&fid=73
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Python|Opencv|MATLAB|Halcom.cn ( 蜀ICP备16027072号 )

GMT+8, 2024-4-19 08:19 , Processed in 0.196183 second(s), 21 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表