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(nrow*ncol));
% 记录蚂蚁的位置
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 nrow*ncol == 128*128
A = 40;
memory_length = round(rand(1).*(1.15*A-0.85*A)+0.85*A);
elseif nrow*ncol == 256*256
A = 30;
memory_length = round(rand(1).*(1.15*A-0.85*A)+0.85*A);
elseif nrow*ncol == 512*512
A = 20;
memory_length = round(rand(1).*(1.15*A-0.85*A)+0.85*A);
end
ant_memory = zeros(ant_total_num, memory_length);
% 实施算法
if nrow*ncol == 128*128
% 迭代的次数
total_step_num = 300;
elseif nrow*ncol == 256*256
total_step_num = 900;
elseif nrow*ncol == 512*512
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
% 产生边缘图矩阵,运用信息素函数判断是否是边缘
% 调用子函数进行二值分割
T = func_seperate_two_class(p);
%imwrite(uint8(abs((p>=T).*255-255)), gray(256), [filename '_edge_aco_' num2str(nMethod) '.jpg'], 'jpg');
subplot(2,3,nMethod)
imshow(uint8(abs((p>=T).*255-255)));
fprintf('Done!\n');
end
%%%%%%%%子函数%%%%%%%
function level = func_seperate_two_class(I)
% 功能:进行二值分割
I = I(:);
% STEP 1: 通过直方图计算灰度均值, 设定T=mean(I)
[counts, N]=hist(I,256);
i=1;
mu=cumsum(counts);
T(i)=(sum(N.*counts))/mu(end);
% STEP 2:
没有合适的资源?快使用搜索试试~ 我知道了~
【边缘检测】蚁群算法图像边缘检测【含Matlab源码 1189期】.zip
共2个文件
m:1个
jpg:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 112 浏览量
2024-06-20
16:27:54
上传
评论
收藏 24KB ZIP 举报
温馨提示
Matlab领域上传的代码均可运行,亲测可用,直接替换数据即可,适合小白; 1、代码压缩包内容 主函数:main.m; 调用函数:其他m文件;无需运行 运行结果效果图; 2、代码运行版本 Matlab 2019b;若运行有误,根据提示修改;若不会,私信博主; 3、运行操作步骤 步骤一:将所有文件放到Matlab的当前文件夹中; 步骤二:双击打开main.m文件; 步骤三:点击运行,等程序运行完得到结果; 4、仿真咨询 如需其他服务,可私信博主或扫描博客文章底部QQ名片; 4.1 博客或资源的完整代码提供 4.2 期刊或参考文献复现 4.3 Matlab程序定制 4.4 科研合作 图像边缘检测: Snake模型、八方向、CNN、积累加权、 Sobel+Prewitt+Canny+Robert算子图像边缘检测 蚁群算法、模拟退火算法、蚁群聚类图像边缘检测 元胞自动机图像边缘检测 插值法亚像素、Zernike矩亚像素边缘检测 拉普拉斯算法图像边缘检测
资源推荐
资源详情
资源评论
收起资源包目录
【边缘检测】蚁群算法图像边缘检测【含Matlab源码 1189期】.zip (2个子文件)
【边缘检测】基于matlab蚁群算法图像边缘检测【含Matlab源码 1189期】
运行结果.jpg 23KB
edge_ACO.m 8KB
共 2 条
- 1
资源评论
Matlab领域
- 粉丝: 3w+
- 资源: 3183
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功