%% 使用聚类的萤火虫算法 (FA) 图像分割
clear;
clc;
close all
warning('off');
% 下载
img=imread('ant.jpg');
img=im2double(img);
gray=rgb2gray(img);
gray=imadjust(gray);
% 将图像重塑为矢量
X=gray(:);
%% 启动 FA 聚类
k = 6; % 集群数
%---------------------------------------------------
CostFunction=@(m) ClusterCost(m, X); % 成本函数
VarSize=[k size(X,2)]; % 决策变量矩阵大小
nVar=prod(VarSize); % 决策变量的数量
VarMin= repmat(min(X),k,1); %变量的下界
VarMax= repmat(max(X),k,1); % 变量的上限
% 萤火虫算法参数
MaxIt = 100; % 最大迭代次数
nPop = 10; %萤火虫数量(群体大小)
gamma = 1; % 光吸收系数
beta0 = 2; % 吸引力系数基值
alpha = 0.2; % 突变系数
alpha_damp = 0.98; % 突变系数阻尼比
delta = 0.05*(VarMax-VarMin); % 统一突变范围
m = 2;
if isscalar(VarMin) && isscalar(VarMax)
dmax = (VarMax-VarMin)*sqrt(nVar);
else
dmax = norm(VarMax-VarMin);
end
%开始
% 空萤火虫结构
firefly.Position = [];
firefly.Cost = [];
firefly.Out = [];
% 初始化人口数组
pop = repmat(firefly, nPop, 1);
% 初始化有史以来最好的解决方案
BestSol.Cost = inf;
% 创建初始萤火虫
for i = 1:nPop
pop(i).Position = unifrnd(VarMin, VarMax, VarSize);
[pop(i).Cost, pop(i).Out] = CostFunction(pop(i).Position);
if pop(i).Cost <= BestSol.Cost
BestSol = pop(i);
end
end
% 保存最佳成本值的数组
BestCost = zeros(MaxIt, 1);
%%萤火虫算法主循环
for it = 1:MaxIt
newpop = repmat(firefly, nPop, 1);
for i = 1:nPop
newpop(i).Cost = inf;
for j = 1:nPop
if pop(j).Cost < pop(i).Cost
rij = norm(pop(i).Position-pop(j).Position)/dmax;
beta = beta0.*exp(-gamma.*rij^m);
e = delta.*unifrnd(-1, +1, VarSize);
%e = delta*randn(VarSize);
newsol.Position = pop(i).Position ...
+ beta.*rand(VarSize).*(pop(j).Position-pop(i).Position) ...
+ alpha.*e;
newsol.Position = max(newsol.Position, VarMin);
newsol.Position = min(newsol.Position, VarMax);
[newsol.Cost newsol.Out] = CostFunction(newsol.Position);
if newsol.Cost <= newpop(i).Cost
newpop(i) = newsol;
if newpop(i).Cost <= BestSol.Cost
BestSol = newpop(i);
end
end
end
end
end
%合并
pop = [pop
newpop];
% 种类
[~, SortOrder] = sort([pop.Cost]);
pop = pop(SortOrder);
% 截短
pop = pop(1:nPop);
% Store Best Cost Ever Found
BestCost(it) = BestSol.Cost;
BestRes(it)=BestSol.Cost;
disp(['Iteration ' num2str(it) ': Best Cost = ' num2str(BestCost(it))]);
% 湿突变系数
alpha = alpha*alpha_damp;
end
FAlbl=BestSol.Out.ind;
% 绘制萤火虫算法训练
figure;
plot(BestRes,'--k','linewidth',1);
title('萤火虫算法训练');
xlabel('萤火虫算法迭代');
ylabel('萤火虫算法最优值');
%%将聚类中心及其索引转换为图像
gray2=reshape(FAlbl(:,1),size(gray));
segmented = label2rgb(gray2);
%绘制结果
figure;
subplot(1,2,1);
imshow(img);title('原始图片');
subplot(1,2,2);
imshow(segmented,[]);title('分割图像');
没有合适的资源?快使用搜索试试~ 我知道了~
【图像分割】基于matlab萤火虫算法图像分割【含Matlab源码 2136期】.zip
共5个文件
jpg:3个
m:2个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 95 浏览量
2023-10-30
19:14:27
上传
评论
收藏 55KB ZIP 举报
温馨提示
CSDN海神之光上传的代码均可运行,亲测可用,直接替换数据即可,适合小白; 1、代码压缩包内容 主函数:main.m; 调用函数:其他m文件;无需运行 运行结果效果图; 2、代码运行版本 Matlab 2019b;若运行有误,根据提示修改;若不会,私信博主; 3、运行操作步骤 步骤一:将所有文件放到Matlab的当前文件夹中; 步骤二:双击打开main.m文件; 步骤三:点击运行,等程序运行完得到结果; 4、仿真咨询 如需其他服务,可私信博主或扫描博客文章底部QQ名片; 4.1 博客或资源的完整代码提供 4.2 期刊或参考文献复现 4.3 Matlab程序定制 4.4 科研合作 能量泛函分割:DBSCAN、GAC水平集、snake、分水岭、 阙值分割: 特征增强分割:超像素SFFCM图像分割、关键像素点FLICM图像分割 智能算法图像分割:遗传算法、灰狼算法、人工鱼群、贝叶斯、北方苍鹰、萤火虫、和声搜索、粒子群、蚁群算法
资源推荐
资源详情
资源评论
收起资源包目录
【图像分割】基于matlab萤火虫算法图像分割【含Matlab源码 2136期】.zip (5个子文件)
【图像分割】基于matlab萤火虫算法图像分割【含Matlab源码 2136期】
运行结果1.jpg 23KB
ant.jpg 15KB
main.m 3KB
运行结果2.jpg 26KB
ClusterCost.m 280B
共 5 条
- 1
资源评论
海神之光
- 粉丝: 3w+
- 资源: 2091
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功