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


    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代码问题可私信交流。

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

  • 相关阅读:
    OBS Studio 30.0 承诺在 Linux 上支持英特尔 QSV,为 DeckLink 提供 HDR 回放功能
    笔试强训——day05
    c#学习_第四弹
    深入理解 volatile 关键字
    JAVA动态代理
    【leetcode】 数组二分查找
    Ubuntu16.04-64位操作系统上安装Cadence Conformal10.1教程
    Windows使用小技巧
    力扣打卡之X的平方根
    Python 完美诠释"高内聚"概念的 IO 流 API 体系结构
  • 原文地址:https://blog.csdn.net/qq_59747472/article/details/126840039