Hello Mat

 找回密码
 立即注册
查看: 7665|回复: 4

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

[复制链接]

1294

主题

1520

帖子

110

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22633
发表于 2019-10-10 21:53:23 | 显示全部楼层 |阅读模式
PSO的二维最大熵法阈值分割
链接:https://pan.baidu.com/s/1F3DToLeT1JuAGg7a2wgkhQ&shfl=sharepset 提取码:9p1k
具体链接在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. % 问题背景
  5. I =imread('car.bmp');
  6. I = imresize( I, [256,256] );
  7. if(size(I, 3)>1)
  8.     im = rgb2gray(I);
  9. else
  10.     im = I;
  11. end
  12. h = fspecial('average', [2,2]);  % 2x2均值滤波
  13. meanI = imfilter(im, h);         % 均值图像
  14. figure(1), imshow(im, []);
  15. %% PSO 参数
  16. c1 = 1.4995;  
  17. c2 = 1.4995;
  18. Vmin = -5;
  19. Vmax = 5;
  20. maxiter = 10;  % 迭代次数
  21. sizepop = 10;  % 种群数量
  22. Lmax = 256;    % 最大灰阶
  23. popmin = 1;  popmax = Lmax-1;   % 未知量个数,决策变量个数,阈值个数 x  取值范围
  24. % 2个阈值,实现3分割
  25. level = 3;
  26. nvar = level-1;   % 未知量个数,决策变量个数,阈值个数

  27. % 初始化种群
  28. for i=1:sizepop
  29.     pop(i,1) = popmin + fix( (popmax-popmin)*rand );
  30.     pop(i,2) = popmin + fix( (popmax-popmin)*rand );
  31.     pop(i, :) = sort(pop(i, :));
  32.     fitness(i) = fun2(  im, meanI, pop(i,:) );
  33.     V(i,:) = zeros(1, nvar);
  34. end
  35. % 记录一组最优值
  36. [bestfitness,bestindex]=max(fitness);
  37. zbest=pop(bestindex,:);   %全局最佳
  38. gbest=pop;                %个体最佳
  39. fitnessgbest=fitness;     %个体最佳适应度值
  40. fitnesszbest=bestfitness; %全局最佳适应度值
  41. % 迭代寻优
  42. for i=1:maxiter
  43.    disp(['当前迭代次数: ', num2str(i)])
  44.     for j=1:sizepop
  45.         % 速度更新
  46.         V(j,:) = V(j,:) + c1*rand*(gbest(j,:) - pop(j,:)) + c2*rand*(zbest - pop(j,:));
  47.         V(j, find(V(j,:)>Vmax)) = Vmax;
  48.         V(j, find(V(j,:)<Vmin)) = Vmin;
  49.         
  50.         % 个体更新
  51.         pop(j,:) = fix( pop(j,:) + 0.5 * V(j,:) );
  52.         pop(j, find(pop(j,:)>popmax)) = popmax;
  53.         pop(j, find(pop(j,:)<popmin)) = popmin;
  54.         
  55.         % 适应度更新
  56.         pop(j, :) = sort(pop(j, :));
  57.         fitness(j) = fun2(im, meanI, pop(j,:));
  58.         
  59.         % 比较  个体间比较
  60.         if fitness(j)>fitnessgbest(j)
  61.             fitnessgbest(j) = fitness(j);
  62.             gbest(j,:) = pop(j,:);
  63.         end
  64.         if fitness(j)>bestfitness
  65.             bestfitness = fitness(j);
  66.             zbest =  pop(j,:);
  67.         end
  68.         
  69.     end
  70.     fitness_iter(i) = bestfitness;
  71.    
  72. end
  73. %% 结果显示
  74. disp('最优解')
  75. zbest = sort(zbest);
  76. disp(zbest)
  77. fprintf('\n')

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

  80. % 显示
  81. Iout = imageGray(im, zbest);
  82. figure('color',[1,1,1])
  83. imshow(Iout,[])
  84. colormap jet;
  85. axis equal
复制代码
适应度函数如下:
游客,如果您要查看本帖隐藏内容请回复



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


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

使用道具 举报

0

主题

59

帖子

1

金钱

注册会员

Rank: 2

积分
72
发表于 2019-11-5 19:35:22 | 显示全部楼层
非常好的代码
回复 支持 反对

使用道具 举报

0

主题

4

帖子

18

金钱

新手上路

Rank: 1

积分
22
发表于 2020-1-29 09:39:55 | 显示全部楼层
我来看看大佬的代码
回复 支持 反对

使用道具 举报

1

主题

16

帖子

1

金钱

新手上路

Rank: 1

积分
17
发表于 2020-8-8 17:39:50 | 显示全部楼层
好资源,谢谢分享!
回复 支持 反对

使用道具 举报

0

主题

54

帖子

1

金钱

注册会员

Rank: 2

积分
55
发表于 2021-1-10 11:58:26 | 显示全部楼层
111111111111111111111
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-26 22:13 , Processed in 0.211930 second(s), 21 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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