|
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\
- clc,clear,close all
- warning off
- format longG
- % 问题背景
- I =imread('car.bmp');
- I = imresize( I, [256,256] );
- if(size(I, 3)>1)
- im = rgb2gray(I);
- else
- im = I;
- end
- h = fspecial('average', [2,2]); % 2x2均值滤波
- meanI = imfilter(im, h); % 均值图像
- figure(1), imshow(im, []);
- %% PSO 参数
- c1 = 1.4995;
- c2 = 1.4995;
- Vmin = -5;
- Vmax = 5;
- maxiter = 10; % 迭代次数
- sizepop = 10; % 种群数量
- Lmax = 256; % 最大灰阶
- popmin = 1; popmax = Lmax-1; % 未知量个数,决策变量个数,阈值个数 x 取值范围
- % 2个阈值,实现3分割
- level = 3;
- nvar = level-1; % 未知量个数,决策变量个数,阈值个数
- % 初始化种群
- for i=1:sizepop
- pop(i,1) = popmin + fix( (popmax-popmin)*rand );
- pop(i,2) = popmin + fix( (popmax-popmin)*rand );
- pop(i, :) = sort(pop(i, :));
- fitness(i) = fun2( im, meanI, pop(i,:) );
- V(i,:) = zeros(1, nvar);
- end
- % 记录一组最优值
- [bestfitness,bestindex]=max(fitness);
- zbest=pop(bestindex,:); %全局最佳
- gbest=pop; %个体最佳
- fitnessgbest=fitness; %个体最佳适应度值
- fitnesszbest=bestfitness; %全局最佳适应度值
- % 迭代寻优
- for i=1:maxiter
- disp(['当前迭代次数: ', num2str(i)])
- for j=1:sizepop
- % 速度更新
- V(j,:) = V(j,:) + c1*rand*(gbest(j,:) - pop(j,:)) + c2*rand*(zbest - pop(j,:));
- V(j, find(V(j,:)>Vmax)) = Vmax;
- V(j, find(V(j,:)<Vmin)) = Vmin;
-
- % 个体更新
- pop(j,:) = fix( pop(j,:) + 0.5 * V(j,:) );
- pop(j, find(pop(j,:)>popmax)) = popmax;
- pop(j, find(pop(j,:)<popmin)) = popmin;
-
- % 适应度更新
- pop(j, :) = sort(pop(j, :));
- fitness(j) = fun2(im, meanI, pop(j,:));
-
- % 比较 个体间比较
- if fitness(j)>fitnessgbest(j)
- fitnessgbest(j) = fitness(j);
- gbest(j,:) = pop(j,:);
- end
- if fitness(j)>bestfitness
- bestfitness = fitness(j);
- zbest = pop(j,:);
- end
-
- end
- fitness_iter(i) = bestfitness;
-
- end
- %% 结果显示
- disp('最优解')
- zbest = sort(zbest);
- disp(zbest)
- fprintf('\n')
- figure('color',[1,1,1])
- plot(fitness_iter,'ro-','linewidth',2)
- % 显示
- Iout = imageGray(im, zbest);
- figure('color',[1,1,1])
- imshow(Iout,[])
- colormap jet;
- axis equal
复制代码 适应度函数如下:
参考:【32】一维最大熵法进行2分割和3分割(MATLAB)
|
|