⛄一、获取代码方式
获取代码方式1:
完整代码已上传我的资源:【边缘检测】基于matlab蚁群算法图像边缘检测【含Matlab源码 1189期】
获取代码方式2:
通过订阅紫极神光博客付费专栏,凭支付凭证,私信博主,可获得此代码。
备注:
订阅紫极神光博客付费专栏,可免费获得1份代码(有效期为订阅日起,三天内有效);
⛄二、蚁群算法图像边缘检测简介
1 蚁群优化算法原理
蚁群优化算法(ACO) 由意大利学者 Macro Dori- go于1992年在他的博士论文中首次提出,是一种用来在图中寻找最优化路径的概率型算法。生物学研究表明: 蚂蚁在运动时会在所经过的路径上释放出一种称为“信息素”的化学物质,后续的蚂蚁可根据此选择路径。前面的蚂蚁遇到一个路口时,会随机选择其中的一条路径并释放出相应的信息素,路径越长信
息素越少。后面的蚂蚁来到相同的路口时,就会根据信息素的多少做出路径选择,信息量大的路径被选择的概率相对较大,同时释放信息素,这样就形成了信息正反馈。最优路径上的信息量越来越大,而其他路径上的信息量越来越少,最终整个蚁群会找到最佳路径。采用蚁群优化算法的边缘检测N-S流程图,如图1所示。
2 蚁群优化算法图像边缘检测过程
基于蚁群优化算法的图像边缘检测方法,将图像中的每个像素看作一个点,利用若干只蚂蚁在一副二维图像上运动来构建信息素矩阵,其中矩阵的每个元代表了图像每个像素点位置的边缘信息。此外,蚂蚁们的移动方向可由图像强度值的局部变化来调整。
下面详细介绍各个过程。
2.1 初始化过程
2.2 执行过程
2.3 更新过程
2.4 决策过程
⛄三、部分源代码
function edge_ACO
%参考文献:"An Ant Colony Optimization Algorithm For Image Edge
close all; clear all; clc;
% 读入图像
filename = ‘ant128’;
raw=imread(‘ant.jpg’);
img=rgb2gray(raw);
img = double(img)./255;
[nrow, ncol] = size(img);
subplot(2,3,1);
imshow(raw); %显示源图像
%公式(3.24.4)初始化
for nMethod = 2:5;
%四种不同的核函数, 参见式 (3.24.7)-(3.24.10)
%E: exponential; F: flat; G: gaussian; S:Sine; T:Turkey; W:Wave
fprintf(‘Image edge detection using ant colony.\nPlease wait…\n’);
v = zeros(size(img));
v_norm = 0;
for rr =1:nrow
for cc=1:ncol
%定义像素团
temp1 = [rr-2 cc-1; rr-2 cc+1; rr-1 cc-2; rr-1 cc-1; rr-1 cc; rr-1 cc+1; rr-1 cc+2; rr cc-1];
temp2 = [rr+2 cc+1; rr+2 cc-1; rr+1 cc+2; rr+1 cc+1; rr+1 cc; rr+1 cc-1; rr+1 cc-2; rr cc+1];
temp0 = find(temp1(:,1)>=1 & temp1(:,1)<=nrow & temp1(:,2)>=1 & temp1(:,2)<=ncol & temp2(:,1)>=1 & temp2(:,1)<=nrow & temp2(:,2)>=1 & temp2(:,2)<=ncol);
temp11 = temp1(temp0, 😃;
temp22 = temp2(temp0, 😃;
temp00 = zeros(size(temp11,1));
for kk = 1:size(temp11,1)
temp00(kk) = abs(img(temp11(kk,1), temp11(kk,2))-img(temp22(kk,1), temp22(kk,2)));
end
if size(temp11,1) == 0
v(rr, cc) = 0;
v_norm = v_norm + v(rr, cc);
else
lambda = 10;
switch nMethod
case 1%‘F’
temp00 = lambda .* temp00;
case 2%‘Q’
temp00 = lambda .* temp00.^2;
case 3%‘S’
temp00 = sin(pi .* temp00./2./lambda);
case 4%‘W’
temp00 = sin(pi.temp00./lambda).pi.temp00./lambda;
end
v(rr, cc) = sum(sum(temp00.^2));
v_norm = v_norm + v(rr, cc);
end
end
end
% 归一化
v = v./v_norm;
v = v.100;
p = 0.0001 . ones(size(img)); % 信息素函数初始化
%参数设置。
alpha = 1; %式(3.24.4)中的参数
beta = 0.1; %式(3.24.4)中的参数
rho = 0.1; %式(3.24.11)中的参数
%式(3.24.12)中的参数
phi = 0.05; %equation (12), i.e., (9) in IEEE-CIM-06
ant_total_num = round(sqrt(nrowncol));
% 记录蚂蚁的位置
ant_pos_idx = zeros(ant_total_num, 2);
% 初始化蚂蚁的位置
rand(‘state’, sum(clock));
temp = rand(ant_total_num, 2);
ant_pos_idx(:,1) = round(1 + (nrow-1) * temp(:,1)); %行坐标
ant_pos_idx(:,2) = round(1 + (ncol-1) * temp(:,2)); %列坐标
search_clique_mode = ‘8’; %Figure 1
% 定义存储空间容量
if nrowncol == 128128
A = 40;
memory_length = round(rand(1).(1.15A-0.85A)+0.85A);
elseif nrowncol == 256256
A = 30;
memory_length = round(rand(1).(1.15A-0.85A)+0.85A);
elseif nrowncol == 512512
A = 20;
memory_length = round(rand(1).(1.15A-0.85A)+0.85A);
end
ant_memory = zeros(ant_total_num, memory_length);
% 实施算法
if nrowncol == 128128
% 迭代的次数
total_step_num = 300;
elseif nrowncol == 256256
total_step_num = 900;
elseif nrowncol == 512512
total_step_num = 1500;
end
total_iteration_num = 3;
for iteration_idx = 1: total_iteration_num
delta_p = zeros(nrow, ncol);
for step_idx = 1: total_step_num
delta_p_current = zeros(nrow, ncol);
for ant_idx = 1:ant_total_num
ant_current_row_idx = ant_pos_idx(ant_idx,1);
ant_current_col_idx = ant_pos_idx(ant_idx,2);
% 找出当前位置的邻域
if search_clique_mode == ‘4’
rr = ant_current_row_idx;
cc = ant_current_col_idx;
ant_search_range_temp = [rr-1 cc; rr cc+1; rr+1 cc; rr cc-1];
elseif search_clique_mode == ‘8’
rr = ant_current_row_idx;
cc = ant_current_col_idx;
ant_search_range_temp = [rr-1 cc-1; rr-1 cc; rr-1 cc+1; rr cc-1; rr cc+1; rr+1 cc-1; rr+1 cc; rr+1 cc+1];
end
%移除图像外的位置
temp = find(ant_search_range_temp(:,1)>=1 & ant_search_range_temp(:,1)<=nrow & ant_search_range_temp(:,2)>=1 & ant_search_range_temp(:,2)<=ncol);
ant_search_range = ant_search_range_temp(temp, 😃;
%计算概率转换函数
ant_transit_prob_v = zeros(size(ant_search_range,1),1);
ant_transit_prob_p = zeros(size(ant_search_range,1),1);
for kk = 1:size(ant_search_range,1)
temp = (ant_search_range(kk,1)-1)ncol + ant_search_range(kk,2);
if length(find(ant_memory(ant_idx,:)==temp))==0 ant_transit_prob_v(kk) = v(ant_search_range(kk,1), ant_search_range(kk,2));
ant_transit_prob_p(kk) = p(ant_search_range(kk,1), ant_search_range(kk,2));
else
ant_transit_prob_v(kk) = 0;
ant_transit_prob_p(kk) = 0;
end
end
if (sum(sum(ant_transit_prob_v))==0) | (sum(sum(ant_transit_prob_p))==0)
for kk = 1:size(ant_search_range,1)
temp = (ant_search_range(kk,1)-1)ncol + ant_search_range(kk,2);
ant_transit_prob_v(kk) = v(ant_search_range(kk,1), ant_search_range(kk,2));
ant_transit_prob_p(kk) = p(ant_search_range(kk,1), ant_search_range(kk,2));
end
end
ant_transit_prob = (ant_transit_prob_v.^alpha) . (ant_transit_prob_p.^beta) ./ ((sum(sum((ant_transit_prob_v.^alpha) . (ant_transit_prob_p.^beta))))+eps);
% 产生一个随机数来确定下一个位置
rand(‘state’, sum(100*clock));
temp = find(cumsum(ant_transit_prob)>=rand(1), 1);
ant_next_row_idx = ant_search_range(temp,1);
ant_next_col_idx = ant_search_range(temp,2);
if length(ant_next_row_idx) == 0
ant_next_row_idx = ant_current_row_idx;
ant_next_col_idx = ant_current_col_idx;
end
ant_pos_idx(ant_idx,1) = ant_next_row_idx;
ant_pos_idx(ant_idx,2) = ant_next_col_idx;
delta_p_current(ant_pos_idx(ant_idx,1), ant_pos_idx(ant_idx,2)) = 1;
if step_idx <= memory_length
ant_memory(ant_idx,step_idx) = (ant_pos_idx(ant_idx,1)-1)*ncol + ant_pos_idx(ant_idx,2);
elseif step_idx > memory_length
ant_memory(ant_idx,:) = circshift(ant_memory(ant_idx,:),[0 -1]);
ant_memory(ant_idx,end) = (ant_pos_idx(ant_idx,1)-1)*ncol + ant_pos_idx(ant_idx,2);
end
%更新信息素函数
p = ((1-rho).*p + rho.*delta_p_current.*v).delta_p_current + p.(abs(1-delta_p_current));
end
delta_p = (delta_p + (delta_p_current>0))>0;
p = (1-phi).*p;
end
end
⛄四、运行结果
⛄五、matlab版本及参考文献
1 matlab版本
2014a
2 参考文献
[1]陈浩,方勇,朱大洲,王成,陈子龙.基于蚁群算法的玉米植株热红外图像边缘检测[J].农机化研究. 2015,37(06)
3 备注
简介此部分摘自互联网,仅供参考,若侵权,联系删除