改进的蚁群算法

%%%基于改进蚁群算法的图像分割

%%%%%%%%%%%初始部分,读取图像及计算相关信息%%%%%%%%%%%%%%%%%

clear;

clc;

x=imread(‘F:\photo图片\Photo\220.jpg’);          %读取图像

imshow(x),title(‘原图’);

x=rgb2gray(x);

figure;imhist(x),title(‘灰度直方图’);   %显示图像灰度直方图

alpha=1;            %表征信息素重要程度的参数

beta=1;             %表征启发式因子重要程度的参数

maxant=numel(x);    %最大蚂蚁数目

r=10;               %聚类半径

rho=0.9;            %挥发系数

ranta=0.9;          %隶属度

c=105;              %食物源中心(初始聚类中心)

new_m=0;            %类集合中所有像素灰度值总和

new_n=0;            %类集合中所有像素个数

x=double(x);

%%%%%%%%%%%%%计算蚂蚁i到食物源c的距离%%%%%%%%%%%%%%%%%%%%%%%%%

for i=1:maxant

    distance(i)=sqrt((x(i)-c)^2);

end

%%%%%%%%%%%%%%更新类集合中所有像素与食物源的距离%%%%%%%%%%%%%%%%%%

j=1;

for i=1:maxant

   if distance(i)<=r;

     distance3(j)=distance(i);

     j=j+1;

 end

end 

%%%%%%%%%%%%计算蚂蚁i在路径上放置的信息浓度%%%%%%%%%%%%%%%%%%%%%%

for i=1:maxant

   if distance(i)<=r;

       ph(i)=1;

   else

       ph(i)=0;

   end

end

%%%%%%%%%%%%%%计算各个像素的灰度值与聚类中心的相似度%%%%%%%%%%%%%%%%%

for i=1:maxant

    if distance(i)==0;

        similar(i)=1;

    else

    similar(i)=r/sqrt((x(i)-c)^2);

end

end

%%%%%%%%%%%%%%计算更新类中所有像素的信息素浓度的调整%%%%%%%%%%%%%%%%%

for i=1:maxant

    if distance(i)<=r;

        new_n=new_n+1;

        newph(new_n)=ph(i)*(1-rho^new_n)/(1-rho);

        newph1(new_n)=1;

    end

end

%%%%%%%%%%%%%%%%计算更新蚂蚁i的相似度的调整%%%%%%%%%%%%%%%%%%%%

 

for i=1:maxant

    if distance(i)<=r;

        similar1(i)=similar(i);

    end

end

similar2=similar1(find(similar1));

%%%%%%%%%%计算更新类集合中所有像素的信息浓度与相似度的调整%%%%%%%%%%%%%%

 

allsum=newph1.*similar2;

for n=2:new_n

    allsum(n)=allsum(n-1)+allsum(n);

end

 for n=1:new_n

     newallsum(n)=allsum(n)/n;

 end

%%%%%%%%%%%%%%%%%%计算概率%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

for n=1:new_n

   if distance3(n)==0;

        newp(n)=1;

    else

       newp(n)=((newph1(n)^alpha)*(similar2(n)^beta))/newallsum(n);

   end

end

%%%%%%%%%%%%%%%计算类间所有像素的灰度值的平均值%%%%%%%%%%%%%%%%%%%

for i=1:maxant

    if distance(i)<=r;

        new_m(i)=x(i);

    end 

end

 new_m1=new_m(find(new_m));  

 new_m2=0;

  new_n2=0;

for n=1:new_n

    if newp(n)>ranta;

        new_m2=new_m2+new_m1(n);

        new_n2=new_n2+1;

    end

end

  T=new_m2/new_n2;

  new_m3=0;

  for n=1:new_n

  new_m3=new_m3+new_m1(n);

  T1=new_m3/new_n;

end

  %%%%%%%%%%%%%%%%利用改进蚁群算法进行图像分割%%%%%%%%%%%%%%%%%%%

   

  x=uint8(x);

   x1=im2bw(x,T/255);

   x1=double(x1);

    for j=2:maxant

     distance1(j)=sqrt((x1(j)-x1(j-1))^2);

 end

 for j=1:maxant 

 if distance1(j)==1;

       x1(j)=1;

   else

       x1(j)=0;

   end

end

 x1=logical(x1);

 figure;imshow(x1),title(‘改进蚁群算法’);

     %%%%%%%%%%%%%%%%%利用传统蚁群算法进行图像分割%%%%%%%%%%%%%%%%%%%%

 x2=im2bw(x,T1/255);

   x2=double(x2);

    for j=2:maxant

     distance2(j)=sqrt((x2(j)-x2(j-1))^2);

 end

 for j=1:maxant 

 if distance2(j)==1;

       x2(j)=1;

   else

       x2(j)=0;

   end

end

x2=logical(x2);

 figure;imshow(x2),title(‘基本蚁群算法’);

  %%%%%%%%%%%%%%%%%利用其他算法进行图像分割%%%%%%%%%%%%%%%%%%%%%

 

 bw1=edge(x,’roberts’);

 figure;imshow(bw1),title(‘roberts算子’);

 bw2=edge(x,’sobel’);

 figure;imshow(bw2),title(‘sobel算子’);

 bw3=edge(x,’prewitt’);

 figure;imshow(bw3),title(‘prewitt算子’);

    原文作者:蚁群算法
    原文地址: https://blog.csdn.net/hao_shu/article/details/80233217
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞