【图像分割】基于萤火虫算法实现图像分割附matlab代码

本文探讨了二维Otsu方法的局限,并提出结合萤火虫算法的图像分割方案。通过实验证明,该方法有效提高分割效率并适用于实时处理,提升了图像阈值搜索的准确性。

1 内容介绍

对二维最大类间方差(2-D Otsu)算法和萤火虫算法研究现状进行了调查研究.为了解决二维Otsu图像阈值分割方法存在的计算复杂度高,实时性差等缺点,提出了一种将萤火虫算法与二维Otsu算法结合的图像分割算法,即通过萤火虫算法(IFA)搜寻图像分割的最佳阈值.实验结果表明,萤火虫算法能够很好的实现图像分割的效果,有效地缩短了图像分割的运行时间,可以运用于图像分割的实时处理.

2 部分代码

%% Firefly Algorithm (FA) Image Segmentation Using Clustering

clear;

clc;

close all

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');

3 运行结果

4 参考文献

[1]迪娜·加尔肯. 基于MATLAB的图像分割算法研究及实现[J]. 科学技术创新, 2021(26):3.

[2]周晨航, 田力威, 赵宏伟. 基于改进萤火虫算法的二维Otsu图像分割法[J]. 沈阳大学学报:自然科学版, 2016, 28(1):6.

博主简介:擅长智能优化算法神经网络预测信号处理元胞自动机图像处理路径规划无人机雷达通信无线传感器等多种领域的Matlab仿真,相关matlab代码问题可私信交流。

部分理论引用网络文献,若有侵权联系博主删除。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

matlab科研助手

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值