意见箱
恒创运营部门将仔细参阅您的意见和建议,必要时将通过预留邮箱与您保持联络。感谢您的支持!
意见/建议
提交建议

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

来源:恒创科技 编辑:恒创科技编辑部
2023-12-29 17:22:59
1 内容介绍

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

2 部分代码

%% Firefly Algorithm (FA) Image Segmentation Using Clustering

clear;


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

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 运行结果

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

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

4 参考文献

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

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

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


上一篇: opencv直方图处理的常见处理操作有哪些 下一篇: 【OFDM】多径信道下OFDM通信系统误码率仿真附matlab代码(OFDM抗多径干扰的方法)