【18】【图像处理】粒子群算法结合模糊聚类分割算法实现图像的分割

 【18】【图像处理】粒子群算法结合模糊聚类分割算法实现图像的分割

 

 【18】【图像处理】粒子群算法结合模糊聚类分割算法实现图像的分割

 

 

 

%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)endbestc=find(result==min(result))+1bestresult=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     endtoc;    

 【18】【图像处理】粒子群算法结合模糊聚类分割算法实现图像的分割

 

 【18】【图像处理】粒子群算法结合模糊聚类分割算法实现图像的分割

 

 【18】【图像处理】粒子群算法结合模糊聚类分割算法实现图像的分割

 

 

 

 

注:完整代码或者代写添加QQ1575304183

上一篇:干货 | 【算法】粒子群算法Particle Swarm Optimization超详细解析+代码实例讲解


下一篇:CSS3弹性盒模型flexbox完整版教程