欢迎来到本博客❤️❤️
博主优势:博客内容尽量做到思维缜密,逻辑清晰,为了方便读者。
⛳️座右铭:行百里者,半于九十。
目录
1 概述
2 运行结果
3 参考文献
4 Matlab代码实现
图像分割是图像处理中最重要的环节,利用聚类算法进行图像分割被广泛应用。为进一步提高图像分割精度,改善传统多阈值图像分割方法计算量大、分割慢的问题。本文利用PSO + SA 算法对图像进行分割,并且通过渐进式增强传统聚类技术。
部分代码:
%% Cleaning the Stage
clc;
clear;
close all;
warning('off');
%% Reading Image
MainOrg=imread('tst.jpg');
Gray=rgb2gray(MainOrg);
InpMat= double(MainOrg);
%% Basics
[s1,s2,s3]=size(InpMat);
R = InpMat(:,:,1);
G = InpMat(:,:,2);
B = InpMat(:,:,3);
X1 = (R-min(R(:)))/(max(R(:))-min(R(:)));
X2 = (G-min(G(:)))/(max(G(:))-min(G(:)));
X3 = (B-min(B(:)))/(max(B(:))-min(B(:)));
X = [X1(:) X2(:) X3(:)];
%% Cluster Numbers
clusteres = 7;
%% Cost Function and Parameters
% Cost Function
CostFunction=@(m) CLuCosPSOSA(m, X, clusteres);
% Decision Variables
VarSize=[clusteres size(X,2)];
% Number of Decision Variables
nVar=prod(VarSize);
% Lower Bound of Variables
VarMin= repmat(min(X),1,clusteres);
% Upper Bound of Variables
VarMax= repmat(max(X),1,clusteres);
%% PSO-SA Clustering Option and Run
% PSO-SA Options
% Iterations (more value means: slower runtime but, better result)
Itr=50;
% SA solver + PSO body
SA_opts = optimoptions('simulannealbnd','display','iter','MaxTime',Itr,'PlotFcn',@pswplotbestf);
options.SwarmSize = 250;
% PSO-SA Run
disp(['SA-PSO Segmentation Is Started ... ']);
[centers, Error] = particleswarm(CostFunction, nVar,VarMin,VarMax,SA_opts);
disp(['SA-PSO Segmentation Is Ended. ']);
%% Calculate Distance Matrix
% Create the Cluster Center
g=reshape(centers,3,clusteres)';
% Create a Distance Matrix
d = pdist2(X, g);
% Assign Clusters and Find Closest Distances
[dmin, ind] = min(d, [], 2);
% Sum of Cluster Distance
WCD = sum(dmin);
% Fitness Function of Centers Sum
z=WCD;
% Final Segmented Image
SA_Segmented=reshape(ind,s1,s2);
PSOSAuint=uint8(SA_Segmented);
ColorSeg = labeloverlay(Gray,PSOSAuint);
%
medgray = medfilt2(SA_Segmented,[5 5]);
%
redChannel = ColorSeg(:,:,1); % Red channel
greenChannel = ColorSeg(:,:,2); % Green channel
blueChannel = ColorSeg(:,:,3); % Blue channel
medcolor1 = medfilt2(redChannel,[4 6]);
medcolor2 = medfilt2(greenChannel,[4 6]);
medcolor3 = medfilt2(blueChannel,[4 6]);
medrgb = cat(3, medcolor1, medcolor2, medcolor3);
[1]林国宇.基于聚类的图像分割研究综述[J].电脑知识与技术,2022,18(26):17-18+24.DOI:10.14004/j.cnki.ckt.2022.1713.