%% Firefly Algorithm image color quantization using clustering
clear;
clc;
warning('off');
img=imread('r.jpg');
img=im2double(img);
% Separating color channels
R=img(:,:,1);
G=img(:,:,2);
B=img(:,:,3);
% Reshaping each channel into a vector and combine all three channels
X=[R(:) G(:) B(:)];
%% Starting FA Clustering
k = 6; % Number of Colors (cluster centers)
%---------------------------------------------------
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 = 7; % 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;
FACenters=Res(X, BestSol);
end
FAlbl=BestSol.Out.ind;
% Plot FA Train
figure;
plot(BestRes,'--k','linewidth',2);
title('FA Train');
xlabel('FA Iteration Number');
ylabel('FA Best Cost Value');
%% Converting cluster centers and its indexes into image
Z=FACenters(FAlbl',:);
R2=reshape(Z(:,1),size(R));
G2=reshape(Z(:,2),size(G));
B2=reshape(Z(:,3),size(B));
% Attaching color channels
quantized=zeros(size(img));
quantized(:,:,1)=R2;
quantized(:,:,2)=G2;
quantized(:,:,3)=B2;
% Plot Results
figure;
subplot(1,2,1);
imshow(img);title('Original');
subplot(1,2,2);
imshow(quantized);title('Quantized Image');
没有合适的资源?快使用搜索试试~ 我知道了~
【图像分割】萤火虫算法图像聚类分割【含Matlab源码 2106期】.zip
共9个文件
jpg:5个
m:4个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 109 浏览量
2024-06-20
16:36:12
上传
评论
收藏 1.58MB ZIP 举报
温馨提示
Matlab领域上传的代码均可运行,亲测可用,直接替换数据即可,适合小白; 1、代码压缩包内容 主函数:main.m; 调用函数:其他m文件;无需运行 运行结果效果图; 2、代码运行版本 Matlab 2019b;若运行有误,根据提示修改;若不会,私信博主; 3、运行操作步骤 步骤一:将所有文件放到Matlab的当前文件夹中; 步骤二:双击打开main.m文件; 步骤三:点击运行,等程序运行完得到结果; 4、仿真咨询 如需其他服务,可私信博主或扫描博客文章底部QQ名片; 4.1 博客或资源的完整代码提供 4.2 期刊或参考文献复现 4.3 Matlab程序定制 4.4 科研合作 能量泛函分割:DBSCAN、GAC水平集、snake、分水岭、 阙值分割: 特征增强分割:超像素SFFCM图像分割、关键像素点FLICM图像分割 智能算法图像分割:遗传算法、灰狼算法、人工鱼群、贝叶斯、北方苍鹰、萤火虫、和声搜索、粒子群、蚁群算法
资源推荐
资源详情
资源评论
收起资源包目录
【图像分割】萤火虫算法图像聚类分割【含Matlab源码 2106期】.zip (9个子文件)
【图像分割】基于matlab萤火虫算法图像聚类分割【含Matlab源码 2106期】
运行结果1.jpg 20KB
ant.jpg 15KB
Res.m 179B
main.m 3KB
运行结果2.jpg 21KB
Firefly Algorithm image color quantization using clustering.jpg 1.47MB
ClusterCost.m 280B
r.jpg 88KB
Firefly Image Quantization.m 3KB
共 9 条
- 1
资源评论
Matlab领域
- 粉丝: 3w+
- 资源: 3117
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功