图像处理粒子群算法结合模糊聚类分割算法实现图像的分割
Posted MatlabQQ1575304183
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了图像处理粒子群算法结合模糊聚类分割算法实现图像的分割相关的知识,希望对你有一定的参考价值。
%pso-fcm。算法思路借鉴网上的某一帖子。
tic;
close all;
clear;
clc;
pic=imread('2.png');
[a,b,k]=size(pic);
t=a*b;
data=reshape(double(pic),t,k);
Maxiter=4;%设定最大迭代次数
n=100;
c1=0.4;
c2=0.4;%设定个体经验系数和群体经验系数
w=0.3;%设定惯性系数
vmax=1.5;%设定最大速度
cmax=4;%设定最大粒子(聚类中心)数目
e=1e+4;%设定阈值
ref=2;%设定fcm的系数
result=zeros(cmax-1,1);
u=cell(cmax,n);
vit=cell(cmax,n);
particle=cell(cmax,n);
dist=cell(cmax,n);
obj=zeros(cmax,n);
pbest=cell(cmax,1);
pbest_pos=cell(cmax,n);
gbest=zeros(cmax,1);
gbest_index=zeros(cmax,1);
gbest_pos=cell(cmax,1);
for c=2:cmax
u_new=zeros(t,c);
for i=1:n
x=randperm(t);
for j=1:c
particle{c,i}(j,:)=data(x(j),:);%随机取c个样本作为初始粒子
end
vit{c,i}=rand(c,3).*vmax;
u{c,i}=zeros(t,c);
dist{c,i}=distfcm(particle{c,i},data);
dist{c,i}=dist{c,i}+0.01;
tmp=dist{c,i}.^(-2/(ref-1));
u{c,i}=tmp./(ones(c,1)*sum(tmp));
%更新隶属度矩阵(初始)
[u_new,particle{c,i},obj(c,i)]=stepfcm(data,u{c,i},c,ref);
u{c,i}=u_new;
%更新粒子和隶属度矩阵(fcm)
end
iter=1;
fit(c)=1e+9;
while(iter<=Maxiter&fit(c)>e)
if(iter==1)
for i=1:n
pbest_pos{c,i}=particle{c,i};
end
pbest{c}=obj(c,1:n);
gbest_index(c)=find(pbest{c}==min(pbest{c}));
gbest_pos{c}=pbest_pos{c,gbest_index(c)};
gbest(c)=pbest{c}(gbest_index(c));
else
replace=0;
replace=find(obj(c,:)<pbest{c});
if(size(replace)>0)
for i=1:size(replace)
pbest_pos{c,replace(i)}=particle{c,replace(i)};
pbest{c}(replace(i))=obj(c,replace(i));
end
end
mins=find(pbest{c}==min(pbest{c}));
gbest_index(c)=mins(1);
gbest_pos{c}=pbest_pos{c,gbest_index(c)};
gbest(c)=pbest{c}(gbest_index(c));
end
for i=1:n
vit{c,i}=vit{c,i}.*w+(pbest_pos{c,i}-particle{c,i}).*c1+(-particle{c,i}+gbest_pos{c}).*c2;
for j=1:c
if (norm(vit{c,i}(j,:))>=vmax)
vit{c,i}(j,:)=(vit{c,i}(j,:).*vmax)./norm(vit{c,i}(j,:));
end
end
end%超过最大速度则限制为最大速度保持方向不变
for i=1:n
particle{c,i}=particle{c,i}+vit{c,i};
end
for i=1:n
[u{c,i},particle{c,i},obj(c,i)]=stepfcm(data,u{c,i},c,ref);
end
fit(c)=min(obj(c,:));
iter=iter+1
end
result(c-1)=gbest(c)
end
bestc=find(result==min(result))+1
bestresult=min(result)
bestparticle=gbest_pos{bestc};
bestu=u{bestc,gbest_index(bestc)};
cluster = cell(1,bestc);
cluster_pic = cell(1,bestc);
pic = cell(1,bestc);
result_pic=cell(1,bestc);
for i = 1:bestc
cluster{i} = find( u{bestc,gbest_index(bestc)}(i,:)==max(u{bestc,gbest_index(bestc)}));
cluster{i} = cluster{i}';
cluster_pic{i} = data(1:t,1:3);
temp = data(1:t,1:3);
count=0;
for j = 1:bestc
if bestparticle(j,1)>bestparticle(i,1)
count=count+1;
end
end
end
toc;
完整代码添加QQ1575304183
以上是关于图像处理粒子群算法结合模糊聚类分割算法实现图像的分割的主要内容,如果未能解决你的问题,请参考以下文章
图像分割基于matlab Kmean聚类分水岭oust粒子群算法优化脂肪肝图像分割含Matlab源码 2277期
图像分割基于matlab粒子群算法和OSTU和分水岭和K-means脂肪肝水平识别含Matlab源码 2397期