✅作者簡介:熱愛科研的Matlab仿真開發者,修心和技術同步精進,matlab項目合作可私信。
🍎個人首頁:Matlab科研工作室
🍊個人信條:格物緻知。
⛄ 内容介紹
本文提出了一種麻雀優化Tsallis相對熵的圖像多門檻值分割算法.首先分析了Tsallis相對熵門檻值分割原理,并将其推廣到多門檻值分割.利用高斯分布拟合分割後的圖像直方圖資訊,利用Tsallis相對熵作為衡量最佳分割門檻值的度量函數.将麻雀優化算法與Tsallis相對熵度量函數結合,求解Tsallis相對熵函數的最優解,提高門檻值分割算法的速度.最後将所提算法并且與經典的Otsu算法和基于二維熵的多門檻值分割法進行對比.實驗結果表明所提算法速度快,準确性高能夠用于圖像的多門檻值分割.
⛄ 部分代碼
%_________________________________________________________________________%
% 麻雀優化算法 %
%_________________________________________________________________________%
function [Best_pos,Best_score,curve]=SSA(pop,Max_iter,lb,ub,dim,fobj)
ST = 0.6;%預警值
PD = 0.7;%發現者的比列,剩下的是加入者
SD = 0.2;%意識到有危險麻雀的比重
PDNumber = round(pop*PD); %發現者數量
SDNumber = round(pop*SD);%意識到有危險麻雀數量
if(max(size(ub)) == 1)
ub = ub.*ones(1,dim);
lb = lb.*ones(1,dim);
end
%種群初始化
X0=initialization(pop,dim,ub,lb);
X = X0;
%計算初始适應度值
fitness = zeros(1,pop);
for i = 1:pop
fitness(i) = fobj(X(i,:));
end
[fitness, index]= sort(fitness);%排序
BestF = fitness(1);
WorstF = fitness(end);
GBestF = fitness(1);%全局最優适應度值
for i = 1:pop
X(i,:) = X0(index(i),:);
end
curve=zeros(1,Max_iter);
GBestX = X(1,:);%全局最優位置
X_new = X;
for i = 1: Max_iter
BestF = fitness(1);
WorstF = fitness(end);
R2 = rand(1);
for j = 1:PDNumber
if(R2<ST)
X_new(j,:) = X(j,:).*exp(-j/(rand(1)*Max_iter));
else
X_new(j,:) = X(j,:) + randn()*ones(1,dim);
end
end
for j = PDNumber+1:pop
% if(j>(pop/2))
if(j>(pop - PDNumber)/2 + PDNumber)
X_new(j,:)= randn().*exp((X(end,:) - X(j,:))/j^2);
else
%産生-1,1的随機數
A = ones(1,dim);
for a = 1:dim
if(rand()>0.5)
A(a) = -1;
end
end
AA = A'*inv(A*A');
X_new(j,:)= X(1,:) + abs(X(j,:) - X(1,:)).*AA';
end
end
Temp = randperm(pop);
SDchooseIndex = Temp(1:SDNumber);
for j = 1:SDNumber
if(fitness(SDchooseIndex(j))>BestF)
X_new(SDchooseIndex(j),:) = X(1,:) + randn().*abs(X(SDchooseIndex(j),:) - X(1,:));
elseif(fitness(SDchooseIndex(j))== BestF)
K = 2*rand() -1;
X_new(SDchooseIndex(j),:) = X(SDchooseIndex(j),:) + K.*(abs( X(SDchooseIndex(j),:) - X(end,:))./(fitness(SDchooseIndex(j)) - fitness(end) + 10^-8));
end
end
%邊界控制
for j = 1:pop
for a = 1: dim
if(X_new(j,a)>ub(a))
X_new(j,a) =ub(a);
end
if(X_new(j,a)<lb(a))
X_new(j,a) =lb(a);
end
end
end
%更新位置
for j=1:pop
fitness_new(j) = fobj(X_new(j,:));
end
for j = 1:pop
if(fitness_new(j) < GBestF)
GBestF = fitness_new(j);
GBestX = X_new(j,:);
end
end
X = X_new;
fitness = fitness_new;
%排序更新
[fitness, index]= sort(fitness);%排序
BestF = fitness(1);
WorstF = fitness(end);
for j = 1:pop
X(j,:) = X(index(j),:);
end
curve(i) = GBestF;
end
Best_pos =GBestX;
Best_score = curve(end);
end