1 引言

2 粒子群优化算法概述

## 二、部分源代码

%              用于多级图像阈值的 CPSOGSA%   clear allclose allclc% 参数初始化%      I = imread('Aeroplane.tiff');   I = imread('Cameraman.tiff');   level = 5; %% Threshold = level-1 %  N_PAR = level;                          %阈值数（级别数-1）（维度） dim = N_PAR;  %  n = 15;                                  % 群的大小“对象数”%%% 默认值 (n = 15) Max_Iteration  = 300;                    % “迭代”的最大数量 %%% 默认（Max_Iteration = 300）% if size(I,3) == 1 %灰度图像[n_countR, x_valueR] = imhist(I(:,:,1));endNt = size(I,1) * size(I,2);  % %Lmax 表示颜色段 0 - 256Lmax = 256;   %图像中考虑了 256 个不同的最大级别（即 0 到 255）for i = 1:Lmax    if size(I,3) == 1          %灰度图像        probR(i) = n_countR(i) / Nt;    endendif size(I,3) == 1    up = ones(n,dim) * Lmax;    low = ones(n,dim);end tic RunNo  = 1;       for k = [ 1 : RunNo ]         [CPSOGSA_bestit,CPSOGSA_bestF,CPSOGSA_Fit_bests]= CPSOGSA(I, Lmax, n,Max_Iteration,low,up,dim, level, probR);       BestSolutions1(k) = CPSOGSA_bestF;  disp(['Run # ' , num2str(k),'::' 'Best estimates =',num2str(CPSOGSA_bestit)]);         % CPSOGSA    end  % /* Boxplot Analysis */   figure   boxplot([BestSolutions1'],{'CPSOGSA'});   color = [([1 0 0])];   h = findobj(gca,'Tag','Box');    for j=1:length(h)    patch(get(h(j),'XData'),get(h(j),'YData'),color(j));   end    title ('\fontsize{15}\bf 飞机 (k=2)');   % %  title ('\fontsize{15}\bf  Cameraman (k=2)');   xlabel('\fontsize{15}\bf 算法');   ylabel('\fontsize{15}\bf 最优适度值');   box on% % % % /* Graphical Analysis*/figure plot(CPSOGSA_Fit_bests,'DisplayName','CPSOGSA','Color','b','LineStyle','-','LineWidth',3); disp( ['Time_CPSOGSA =', num2str(toc)]);  title ('\fontsize{15}\bf Aeroplane (k=2)'); % k=2,4,6,8,10 % %  title ('\fontsize{15}\bf Cameraman (k=2)'); xlabel('\fontsize{15}\bf 迭代'); ylabel('\fontsize{15}\bf 适度值'); legend('\fontsize{12}\bf CPSOGSA'); % % gBestR = sort(CPSOGSA_bestit); Iout = imageGRAY(I,gBestR); Iout2 = mat2gray(Iout);  % % Show results on images  figureimshow(Iout)    figureimshow(I)    % % 结果显示intensity = gBestR(1:dim-1);  STDR  = std(CPSOGSA_Fit_bests)              %Standard deviation of fitness values       MSEV = MSE(I, Iout)                         %Mean Square ErrorPSNRV = PSNR(I, Iout)                       %PSNR between original image I and the segmented image IoutSSIMV = ssim (I, Iout)                      %SSIM Quality MeasureFSIMV = FeatureSIM (I, Iout)                %FSIM Quality MeasureBest_Fitness_Value= CPSOGSA_Fit_bests(k)    %Best fitness     % % 在直方图上绘制阈值figure plot(probR)    hold onvmax = max(probR);for i = 1:dim-1    line([intensity(i), intensity(i)],[0 vmax],[1 1],'Color','r','Marker','.','LineStyle','-')    title ('\fontsize{15}\bf 飞机 (k=2)');% title ('\fontsize{15}\bf  Cameraman (k=2)');    xlabel('\fontsize{15}\bf 灰度级');    ylabel('\fontsize{15}\bf 频率');    hold offend

## 四、matlab版本及参考文献

1 matlab版本
2014a

2 参考文献
[1] 蔡利梅.MATLAB图像处理——理论、算法与实例分析[M].清华大学出版社，2020.
[2]杨丹,赵海滨,龙哲.MATLAB图像处理实例详解[M].清华大学出版社，2013.
[3]周品.MATLAB图像处理与图形用户界面设计[M].清华大学出版社，2013.
[4]刘成龙.精通MATLAB图像处理[M].清华大学出版社，2015.
[5]吕福起,李霄民.基于粒子群优化算法和模糊熵的多级阈值图像分割算法[J].计算机应用研究. 2019,36(09)

3 备注