【图像分割】基于人工蜂群算法实现图像分割matlab代码
1 简介
为快速准确地将图像中目标和背景分离开来,将新型群体智能模型中的人工蜂群算法、二维直线交叉熵相结合,提出了一种图像分割新方法.该方法将图像阈值看成人工蜂群算法中的蜜源,利用信息熵和最大熵原理设计人工蜂群算法的适应度函数;通过采蜜蜂、侦查蜂和观察蜂的分工协作和信息共享,逐代逼近最佳阈值.并利用Matlab实现了图像分割算法,对分割的结果进行分析.实验结果表明,该方法在阈值分割图像时,蜂群算法能够快速准确地将图像目标分离出来,分离出来的目标更加适合后序的分析和处理.
2 部分代码
%%%%%ARTIFICIAL BEE COLONY ALGORITHM%%%% %Artificial Bee Colony Algorithm was developed by Dervis Karaboga in 2005 %by simulating the foraging behaviour of bees. %Copyright ?2008 Erciyes University, Intelligent Systems Research Group, The Dept. of Computer Engineering %Contact: %Dervis Karaboga (karaboga@erciyes.edu.tr ) %Bahriye Basturk Akay (bahriye@erciyes.edu.tr) function [SO,TO] = runABC(N,D,Iter,Pr,run) % clear all close all clc Iter = 100; N = 10; BEA = 7; D = 2; Pr=0.5; run=50; % load temp.mat img=imread('1.png'); im = rgb2gray(img); mat = im; tic % Set ABC Control Parameters ABCOpts = struct( 'ColonySize', N, ... % Number of Employed Bees+ Number of Onlooker Bees 'MaxCycles', Iter,... % Maximum cycle number in order to terminate the algorithm 'ErrGoal', 1e-20, ... % Error goal in order to terminate the algorithm (not used in the code in current version) 'Dim', D, ... % Number of parameters of the objective function 'Limit', 100, ... % Control paramter in order to abandone the food source 'lb', 1, ... % Lower bound of the parameters to be optimized 'ub', 256, ... %Upper bound of the parameters to be optimized 'ObjFun' , 'Hgrey', ... %Write the name of the objective function you want to minimize 'RunTime',run); % Number of the runs GlobalMins=zeros(ABCOpts.RunTime,ABCOpts.MaxCycles); for r=1:ABCOpts.RunTime % Initialise population Range = repmat((ABCOpts.ub-ABCOpts.lb),[ABCOpts.ColonySize 1]);%ABCOpts.Dim]); Lower = repmat(ABCOpts.lb, [ABCOpts.ColonySize 1]);%ABCOpts.Dim]); XColony = rand(ABCOpts.ColonySize,1) .* Range + Lower; YColony = rand(ABCOpts.ColonySize,1).* Range + Lower; Colony = [XColony , YColony]; size(Colony); Colony = ceil(Colony); Employed=Colony(1:(ABCOpts.ColonySize/2),:); %evaluate and calculate fitness ObjEmp=feval(ABCOpts.ObjFun,Employed,mat) %fprintf('obj = %d \t',ObjEmp); %fprintf('\n'); %ObjEmp = Location(pos,Employed); %display(ObjEmp); FitEmp=calculateFitness(ObjEmp); display(FitEmp); %fprintf('%d \t',FitEmp); %set initial values of Bas Bas=zeros(1,(ABCOpts.ColonySize/2)); % % fprintf('Cycle=%d ObjVal=%g\n',Cycle,GlobalMin); % %Bestcycle(Cycle,:) = CycleBestParams; %Bestcyval(Cycle) = CycleMin; Cycle=Cycle+1; % end % End of s Bestcycle(r,:) = CycleBestParams; Bestcyval(r) = CycleMin; end; %end of runs toc %semilogy(mean(GlobalMins)) % Global = GlobalMins; % Cyc = rand()* ABCOpts.MaxCycles; % for r = 1: 5 % for C = 1: Cyc % Global(r,C) = Global(r,C) * rand(); % end % so = sort(Global(r,:)); % end %so = sort(Global); %Gls = unique(Globals) %Glt = unique(Globalt) [c ,i] = max(GlobalMins) [cc , ro] = max(c) co = i(ro) SO = Globals(co,ro) TO = Globalt(co,ro) plot(GlobalMins(1,:)); title('Mean of Best function values'); xlabel('cycles'); ylabel('Mean Values'); fprintf('Mean =%g Std=%g\n',mean(GlobalMins(:,end)),std(GlobalMins(:,end))); S = uint8(SO); [r,c] = size(im); % whos S %wim = whiten(uint8(gimg),uint8(S)); thr = S / 256; %wim = im2bw(gimg); img = zeros(r,c); for i = 1:r for j = 1:c if im(i,j) >= S img(i,j) = 1; end end end figure,imshow(mat2gray(img))3 仿真结果
4 参考文献
部分理论引用网络文献,若有侵权联系博主删除。
[1]霍凤财等. "基于人工蜂群算法的图像阈值分割." 自动化技术与应用 035.002(2016):112-116.
