## 2 部分代码

```clc;
clear;
close all;
%% Problem Definition
figure(1)
subplot(121)
imshow(uint8(img));
title('ԭͼ')
[s1,s2,s3]=size(img);
Rplane = img(:,:,1);
Gplane = img(:,:,2);
Bplane = img(:,:,3);
X1 = (Rplane-min(Rplane(:)))/(max(Rplane(:))-min(Rplane(:)));
X2 = (Gplane-min(Gplane(:)))/(max(Gplane(:))-min(Gplane(:)));
X3 = (Bplane-min(Bplane(:)))/(max(Bplane(:))-min(Bplane(:)));
% taking R-plane, B-plane, G-plane values as features
X = [X1(:) X2(:) X3(:)];
k = 4; % no. of clusters
CostFunction=@(m) ClusteringCost2(m, X);     % Cost Function m = [3x2] cluster centers
VarSize=[k size(X,2)];  % Decision Variables Matrix Size = [4 3]
nVar=prod(VarSize);     % Number of Decision Variables = 12
VarMin= repmat(min(X),1,k);      % Lower Bound of Variables [4x1] of[1x3] = [4x3]
VarMax= repmat(max(X),1,k);      % Upper Bound of Variables [4x1] of[1x3] = [4x3]
% running the sine cosine algorithm with desired options
SearchAgents_no=30; % Number of search agents
Max_iteration=1000; % Maximum numbef of iterations
[err_ga, centers] = SCA(SearchAgents_no,Max_iteration,VarMin,VarMax,nVar,CostFunction)
m=centers;
% Calculate Distance Matrix
g=reshape(m,3,4)'; % create a cluster center matrix(4(clusters) points in 3(features) dim plane)=[4x3]
d = pdist2(X, g); % create a distance matrix of each data points in input to each centers = [(s1*s2)x4]
% Assign Clusters and Find Closest Distances
[dmin, ind] = min(d, [], 2);
% ind value gives the cluster number assigned for the input = [(s1*s2)x1]

% Sum of Within-Cluster Distance
WCD = sum(dmin);

z=WCD; % fitness function contain sum of each data point to their corresponding center value set (aim to get it minimum)
% z = [1 x 1]
outimg=reshape(ind,s1,s2);
for i=1:s1
for j=1:s2
if outimg(i,j)== 1
outimg(i,j)= 0;
elseif outimg(i,j)== 2
outimg(i,j)= 85;
elseif outimg(i,j)== 3
outimg(i,j)= 170;
elseif outimg(i,j)== 4
outimg(i,j)= 255;
end
end
end
subplot(122);imshow(uint8(outimg));```

## 4 参考文献

[1]邹刚, 孙即祥, and 敖永红. “粒子群优化的聚类方法在图像分割中的应用.” 电光与控制 2(2009):3.

[2]万瑜廷, 马爱龙, and 钟燕飞. “一种基于多目标正余弦算法的遥感影像空谱聚类方法.”, CN111126467A. 2020.​