基于改进萤火虫算法的图像分割的应用(Matlab代码实现)

  🍒🍒🍒欢迎关注🌈🌈🌈

📝个人主页:我爱Matlab


👍点赞➕评论➕收藏 == 养成习惯(一键三连)🌻🌻🌻

🍌希望大家多多支持🍓~一起加油 🤗

💬语录:将来的我一定会感谢现在奋斗的自己!

🍁🥬🕒摘要🕒🥬🍁

图像分割将图像细分为构成它的子区域或物体。分割的目的是把特定的目标从复杂的图像中抽取出来,是图像识别,图像理解以及图像分析的重要依据。随着技术的发展图像分割已经广泛的使用在众多领域,如医学图像处理,人脸识别,交通道路分析等,分割的好坏直接影响后续工作的有效性。因此,越来越多的学者研究各种相关的图像分割算法,模糊理论能很好的描述图像的特征,在图像分割中得到深入研究。

萤火虫算法是受自然界中的萤火虫通过荧光进行信息交流这种群体行为的启发演变而来。作为一种新颖的仿生群智能优化算法,分析了萤火虫算法的仿生原理,从数学角度对算法实现优化过程进行了定义。通过典型的函数优化和组合优化问题对算法进行了仿真测试,测试结果表明了萤火虫算法在连续空间和离散空间优化的可行性和有效性,具有良好的应用前景。 

✨🔎⚡部分运行结果⚡🔎✨

 

💂♨️👨‍🎓Matlab代码👨‍🎓♨️💂

%% Firefly Algorithm (FA) Image Segmentation Using Clustering
clear;
clc;
warning('off');
% Loading
img=imread('f.jpg');
img=im2double(img);
gray=rgb2gray(img);
gray=imadjust(gray);
% Reshaping image to vector
X=gray(:);

%% Starting FA Clustering
k = 6; % Number of clusters

%---------------------------------------------------
CostFunction=@(m) ClusterCost(m, X);     % Cost Function
VarSize=[k size(X,2)];           % Decision Variables Matrix Size
nVar=prod(VarSize);              % Number of Decision Variables
VarMin= repmat(min(X),k,1);      % Lower Bound of Variables
VarMax= repmat(max(X),k,1);      % Upper Bound of Variables

% Firefly Algorithm Parameters
MaxIt = 100;         % Maximum Number of Iterations
nPop = 10;            % Number of Fireflies (Swarm Size)
gamma = 1;            % Light Absorption Coefficient
beta0 = 2;            % Attraction Coefficient Base Value
alpha = 0.2;          % Mutation Coefficient
alpha_damp = 0.98;    % Mutation Coefficient Damping Ratio
delta = 0.05*(VarMax-VarMin);     % Uniform Mutation Range
m = 2;
if isscalar(VarMin) && isscalar(VarMax)
dmax = (VarMax-VarMin)*sqrt(nVar);
else
dmax = norm(VarMax-VarMin);
end

% Start
% Empty Firefly Structure
firefly.Position = [];
firefly.Cost = [];
firefly.Out = [];
% Initialize Population Array
pop = repmat(firefly, nPop, 1);
% Initialize Best Solution Ever Found
BestSol.Cost = inf;
% Create Initial Fireflies
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
% Array to Hold Best Cost Values
BestCost = zeros(MaxIt, 1);

%% Firefly Algorithm Main Loop
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
% Merge
pop = [pop
newpop];  
% Sort
[~, SortOrder] = sort([pop.Cost]);
pop = pop(SortOrder);
% Truncate
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))]);
% Damp Mutation Coefficient
alpha = alpha*alpha_damp;
end
FAlbl=BestSol.Out.ind;
% Plot FA Train
figure;
plot(BestRes,'--k','linewidth',1);
title('FA Train');
xlabel('FA Iteration Number');
ylabel('FA Best Cost Value');

%% Converting cluster centers and its indexes into image 
gray2=reshape(FAlbl(:,1),size(gray));
segmented = label2rgb(gray2); 
% Plot Results 
figure;
subplot(1,2,1);
imshow(img);title('Original');
subplot(1,2,2);
imshow(segmented,[]);title('Segmented Image');

📜📢🌈参考文献🌈📢📜

[1]刘长平,叶春明.一种新颖的仿生群智能优化算法:萤火虫算法[J].计算机应用研究,2011,28(09):3295-3297.

[2]陈恺,陈芳,戴敏,张志胜,史金飞.基于萤火虫算法的二维熵多阈值快速图像分割[J].光学精密工程,2014,22(02):517-523.

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值