Press "Enter" to skip to content

【图像分割】基于正弦余弦算法实现图像聚类分割matlab代码

1 简介

 

图像分割和对象提取是从图像处理到图像分析的关键步骤.K-均值聚类算法和正余弦优化方法结合,即将K-均值方法的结果作为一个正余弦因子并采用正余弦优化的方法,通过适应度函数,利用新的分类中心调整粒子位置,产生新的聚类中心.并将此方法应用于图像的分割.最后,将两种方法的处理结果进行了比较,结果表示基于SCA聚类方法对图像的分割效果比原算法有所改进.

 

2 部分代码

 

clc;
clear;
close all;
%% Problem Definition
img= double(imread('309.bmp'));
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));

 

3 仿真结果

 

 

4 参考文献

 

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

 

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

 

部分理论引用网络文献,若有侵权联系博主删除。

 

Be First to Comment

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注