【图像分割】基于电磁算法优化多级阈值实现图像分割附matlab代码
✅作者简介:热爱科研的Matlab仿真开发者,修心和技术同步精进,matlab项目合作可私信。
🍎个人主页:Matlab科研工作室
🍊个人信条:格物致知。
更多Matlab仿真内容点击👇
智能优化算法 神经网络预测 雷达通信 无线传感器
信号处理 图像处理 路径规划 元胞自动机 无人机
⛄ 内容介绍
针对传统Kapur熵和oust在多阈值图像分割算法中存在运算量大、计算效率低以及精度不高等问题,提出了一种基于电磁算法的多级阈值图像分割方法,该方法采用Kapur熵作为计算适应度的目标函数,通过引入电磁算法求解目标函数最大化时的全局最优问题.实验结果表明:相对于其他方法,本文方法在多个评价指标上都有很好的性能体现,并且本文方法在保证较好分割效果的同时,计算效率明显提升.
⛄ 部分代码
%Diego Oliva, Erik Cuevas, Gonzalo Pajares, Daniel Zaldivar, Valent韓 Osuna.
%A Multilevel Thresholding algorithm using electromagnetism optimization
%Universidad Complutense de Madrid / Universidad de Guadalajara
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%The algorithm was published as:
%Diego Oliva, Erik Cuevas, Gonzalo Pajares, Daniel Zaldivar, Valent韓 Osuna.
%A Multilevel Thresholding algorithm using electromagnetism optimization,
%Journal of Neurocomputing, 139, (2014), 357-381.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [fitR, fitBestR, fitG, fitBestG, fitB, fitBestB] = fitnessIMG(I, N, Lmax, level, xR, probR, xG, probG, xB, probB)
%Metodo de Otsu
%Evalua poblaciones xR, xG, xB, en la funcion objetivo para obtener
%fitR, fitG, fitB, dependiendo si la imagen es RGB o escala de grises
for j = 1:N
if size(I,3) == 1
%grayscale image
fitR(j) = sum(probR(1:xR(j,1))) * (sum((1:xR(j,1)) .* probR(1:xR(j,1)) / sum(probR(1:xR(j,1)))) - sum((1:Lmax) .* probR(1:Lmax)) ) ^ 2;
for jlevel = 2:level - 1
fitR(j) = fitR(j) + sum(probR(xR(j,jlevel - 1) + 1:xR(j,jlevel))) * (sum((xR(j,jlevel - 1) + 1:xR(j,jlevel)) .* probR(xR(j,jlevel - 1) + 1:xR(j,jlevel)) / sum(probR(xR(j,jlevel - 1) + 1:xR(j,jlevel)))) - sum((1:Lmax) .* probR(1:Lmax))) ^ 2;
end
fitR(j) = fitR(j) + sum(probR(xR(j,level-1) + 1:Lmax)) * (sum((xR(j,level - 1) + 1:Lmax) .* probR(xR(j,level - 1) + 1:Lmax) / sum(probR(xR(j,level - 1) + 1:Lmax))) - sum((1:Lmax) .* probR(1:Lmax))) ^ 2;
% if isnan(fitR(j))
% fitR(j)=eps;
% end
fitBestR(j) = fitR(j);
elseif size(I,3) == 3
%RGB image
fitR(j) = sum(probR(1:xR(j,1))) * (sum((1:xR(j,1)) .* probR(1:xR(j,1)) / sum(probR(1:xR(j,1)))) - sum((1:Lmax) .* probR(1:Lmax))) ^ 2;
for jlevel = 2:level - 1
fitR(j) = fitR(j) + sum(probR(xR(j,jlevel - 1) + 1:xR(j,jlevel))) * (sum((xR(j,jlevel - 1) + 1:xR(j,jlevel)) .* probR(xR(j,jlevel - 1) + 1:xR(j,jlevel)) / sum(probR(xR(j,jlevel - 1) + 1:xR(j,jlevel)))) - sum((1:Lmax) .* probR(1:Lmax))) ^ 2;
end
fitR(j) = fitR(j) + sum(probR(xR(j,level-1) + 1:Lmax)) * (sum((xR(j,level - 1) + 1:Lmax) .* probR(xR(j,level - 1) + 1:Lmax) / sum(probR(xR(j,level - 1) + 1:Lmax))) - sum((1:Lmax) .* probR(1:Lmax))) ^ 2;
fitBestR(j) = fitR(j);
fitG(j) = sum(probG(1:xG(j,1))) * (sum((1:xG(j,1)) .* probG(1:xG(j,1)) / sum(probG(1:xG(j,1)))) - sum((1:Lmax) .* probG(1:Lmax))) ^ 2;
for jlevel = 2:level - 1
fitG(j) = fitG(j) + sum(probG(xG(j,jlevel - 1) + 1:xG(j,jlevel))) * (sum((xG(j,jlevel - 1) + 1:xG(j,jlevel)) .* probG(xG(j,jlevel - 1) + 1:xG(j,jlevel)) / sum(probG(xG(j,jlevel - 1) + 1:xG(j,jlevel)))) - sum((1:Lmax) .* probG(1:Lmax))) ^ 2;
end
fitG(j) = fitG(j) + sum(probG(xG(j,level - 1) + 1:Lmax)) * (sum((xG(j,level-1) + 1:Lmax) .* probG(xG(j,level - 1) + 1:Lmax) / sum(probG(xG(j,level - 1) + 1:Lmax))) - sum((1:Lmax) .* probG(1:Lmax))) ^ 2;
fitBestG(j) = fitG(j);
fitB(j) = sum(probB(1:xB(j,1))) * (sum((1:xB(j,1)) .* probB(1:xB(j,1)) / sum(probB(1:xB(j,1)))) - sum((1:Lmax) .* probB(1:Lmax))) ^ 2;
for jlevel = 2:level - 1
fitB(j) = fitB(j) + sum(probB(xB(j,jlevel - 1) + 1:xB(j,jlevel))) * (sum((xB(j,jlevel - 1) + 1:xB(j,jlevel)) .* probB(xB(j,jlevel - 1) + 1:xB(j,jlevel)) / sum(probB(xB(j,jlevel - 1) + 1:xB(j,jlevel)))) - sum((1:Lmax) .* probB(1:Lmax))) ^ 2;
end
fitB(j) = fitB(j) + sum(probB(xB(j,level - 1) + 1:Lmax)) * (sum((xB(j,level - 1) + 1:Lmax) .* probB(xB(j,level - 1) + 1:Lmax) / sum(probB(xB(j,level - 1) + 1:Lmax))) - sum((1:Lmax) .* probB(1:Lmax))) ^ 2;
fitBestB(j) = fitB(j);
end
end
if size(I,3) == 1
%Imagen escala de Grises
fitR = fitR';
fitBestR = fitBestR';
elseif size(I,3) == 3
% Imagen RGB
fitR = fitR';
fitBestR = fitBestR';
fitG = fitG';
fitBestG = fitBestG';
fitB = fitB';
fitBestB = fitBestB';
end
⛄ 运行结果
⛄ 参考文献
[1]康丽锋, 吴锋. 基于乌鸦搜索优化算法的多级阈值图像分割方法[J]. 西南师范大学学报:自然科学版, 2021, 46(1):6.
[2]孙研. 基于智能优化算法的多阈值图像分割技术及其并行加速[D]. 南京理工大学, 2014.
❤️ 关注我领取海量matlab电子书和数学建模资料
❤️部分理论引用网络文献,若有侵权联系博主删除