【图像分割】基于matlab蚁群优化模糊聚类图像分割【含Matlab源码 130期】
【摘要】
一、获取代码方式
获取代码方式1: 完整代码已上传我的资源:【图像分割】基于matlab蚁群优化模糊聚类图像分割【含Matlab源码 130期】
获取代码方式2: 通过订阅紫极神光博客付费专栏,凭支付...
一、获取代码方式
获取代码方式1:
完整代码已上传我的资源:【图像分割】基于matlab蚁群优化模糊聚类图像分割【含Matlab源码 130期】
获取代码方式2:
通过订阅紫极神光博客付费专栏,凭支付凭证,私信博主,可获得此代码。
备注:
订阅紫极神光博客付费专栏,可免费获得1份代码(有效期为订阅日起,三天内有效);
二、图像分割简介
理论知识参考:【基础教程】基于matlab图像处理图像分割【含Matlab源码 191期】
三、部分源代码
clc
clear all
close all
%The input image should have square size
%All parameters are set to be exactly same as that of the paper
%Image Loading
%filename = 'flower';
I=imread('1.jpg');
%img = double(imread(['C:\Users\yxw\Desktop.jpg']));
img=double(I);
figure(1)
imshow(img/255);
[nrow, ncol, channel] = size(img);
R=img(:, :, 1);
G=img(:, :, 2);
B=img(:, :, 3);
%将彩色图像转化成灰度图像
intensity_img=zeros(nrow, ncol);
for rr = 1 : nrow
for cc = 1 : ncol
intensity_img(rr, cc)=(((R(rr, cc)).^2+(G(rr, cc)).^2+(B(rr, cc)).^2).^0.5)/(3.^0.5);
end
end
figure(2)
imshow(intensity_img/255);
%使用canny算子进行边缘检测
edge_img = edge(intensity_img, 'canny');
figure(3)
imshow(edge_img);
%average = mean(mean(img))/255;
%参数设置
alpha=1;
beta=1;
num=0;%监督聚类中心的初始数目,初始化为0
statistic=60;
radius=50;% 聚类半径
lumda=0.40;
rho=0.95;
p1=1;
p2=1;
p3=1;
d=50;
%ACA图像分割程序
%初始化带有分类信息的图像矩阵
cluster_img = zeros(nrow, ncol, 4);
for rr=1:nrow
for cc=1:ncol
cluster_img(rr, cc, 1) = img(rr, cc, 1);
cluster_img(rr, cc, 2) = img(rr, cc, 2);
cluster_img(rr, cc, 3) = img(rr, cc, 3);
cluster_img(rr, cc, 4) = 0;
end
end
%初始化蚂蚁归类操作矩阵
ant_matrix=zeros(rr, cc, 1);
for rr=1:nrow
for cc=1:ncol
if ant_matrix(rr, cc, 1) == 0
ant_matrix(rr, cc, 1) = 2;%ant_matrix(rr, cc, 1)代表该蚂蚁的状态,"0"表示未被归类,"1"表示已经被归类,"2"表示等待被归类, "3"表示在本次循环中成为类, "4"表示该点为边缘像素点
end
end
end
for rr = 1 : nrow
for cc = 1 : ncol
if edge_img(rr, cc)==1
ant_matrix(rr, cc, 1) = 4;%划分为边缘像素点
cluster_img(rr, cc, 1)=255;
cluster_img(rr, cc, 2)=255;
cluster_img(rr, cc, 3)=255;%边缘像素全部设为白色;
end
end
end
%初始化监督聚类中心
%对图像颜色进行统计
color_statistic = zeros(1331, 5);%color_statistic(i, 1)存储像素数目,
%color_statistic(i, 2)、color_statistic(i, 3)、color_statistic(i, 4)分别存储颜色的三个分量,color_statistic(i, 5)存储是否被作为监督聚类中心的信息
for rr = 1 : nrow
for cc = 1 : ncol
%对每个颜色分量进行分段处理
if R(rr, cc) < 12.75
x=1;
elseif R(rr, cc) >= 12.75 && R(rr, cc) < 38.25
x=2;
elseif R(rr, cc) >= 38.25 && R(rr, cc) < 63.75
x=3;
elseif R(rr, cc) >= 63.75 && R(rr, cc) < 89.25
x=4;
elseif R(rr, cc) >= 89.25 && R(rr, cc) < 114.75
x=5;
elseif R(rr, cc) >= 114.75 && R(rr, cc) < 140.25
x=6;
elseif R(rr, cc) >= 140.25 && R(rr, cc) < 165.75
x=7;
elseif R(rr, cc) >= 165.75 && R(rr, cc) < 191.25
x=8;
elseif R(rr, cc) >= 191.25 && R(rr, cc) < 216.75
x=9;
elseif R(rr, cc) >= 216.75 && R(rr, cc) < 241.25
x=10;
elseif R(rr, cc) >= 241.25
x=11;
end
if G(rr, cc) < 12.75
y=1;
elseif G(rr, cc) >= 12.75 && G(rr, cc) < 38.25
y=2;
elseif G(rr, cc) >= 38.25 && G(rr, cc) < 63.75
y=3;
elseif G(rr, cc) >= 63.75 && G(rr, cc) < 89.25
y=4;
elseif G(rr, cc) >= 89.25 && G(rr, cc) < 114.75
y=5;
elseif G(rr, cc) >= 114.75 && G(rr, cc) < 140.25
y=6;
elseif G(rr, cc) >= 140.25 && G(rr, cc) < 165.75
y=7;
elseif G(rr, cc) >= 165.75 && G(rr, cc) < 191.25
y=8;
elseif G(rr, cc) >= 191.25 && G(rr, cc) < 216.75
y=9;
elseif G(rr, cc) >= 216.75 && G(rr, cc) < 241.25
y=10;
elseif G(rr, cc) >= 241.25
y=11;
end
if B(rr, cc) < 12.75
z=1;
elseif B(rr, cc) >= 12.75 && B(rr, cc) < 38.25
z=2;
elseif B(rr, cc) >= 38.25 && B(rr, cc) < 63.75
z=3;
elseif B(rr, cc) >= 63.75 && B(rr, cc) < 89.25
z=4;
elseif B(rr, cc) >= 89.25 && B(rr, cc) < 114.75
z=5;
elseif B(rr, cc) >= 114.75 && B(rr, cc) < 140.25
z=6;
elseif B(rr, cc) >= 140.25 && B(rr, cc) < 165.75
z=7;
elseif B(rr, cc) >= 165.75 && B(rr, cc) < 191.25
z=8;
elseif B(rr, cc) >= 191.25 && B(rr, cc) < 216.75
z=9;
elseif B(rr, cc) >= 216.75 && B(rr, cc) < 241.25
z=10;
elseif B(rr, cc) >= 241.25
z=11;
end
%更新统计信息
color_statistic(((x-1)*121+(y-1)*11+z), 2) = (color_statistic(((x-1)*121+(y-1)*11+z), 2) * color_statistic(((x-1)*121+(y-1)*11+z), 1) + R(rr, cc)) / (color_statistic(((x-1)*121+(y-1)*11+z), 1) + 1);
color_statistic(((x-1)*121+(y-1)*11+z), 3) = (color_statistic(((x-1)*121+(y-1)*11+z), 3) * color_statistic(((x-1)*121+(y-1)*11+z), 1) + G(rr, cc)) / (color_statistic(((x-1)*121+(y-1)*11+z), 1) + 1);
color_statistic(((x-1)*121+(y-1)*11+z), 4) = (color_statistic(((x-1)*121+(y-1)*11+z), 4) * color_statistic(((x-1)*121+(y-1)*11+z), 1) + B(rr, cc)) / (color_statistic(((x-1)*121+(y-1)*11+z), 1) + 1);
color_statistic(((x-1)*121+(y-1)*11+z), 1) = color_statistic(((x-1)*121+(y-1)*11+z), 1) + 1;
end
end
for i = 1 : 1331
if color_statistic(i, 1) >= statistic
num = num +1;%确定监督聚类中心的数目
color_statistic(i, 5) = 1;
end
end
center_ACA=zeros(num, 5);
for i=1 : num
for j = 1 : 1331
if (color_statistic(j, 1) >= statistic) && (color_statistic(j, 5)==1)
center_ACA(i, 1) = color_statistic(j, 2);
center_ACA(i, 2) = color_statistic(j, 3);
center_ACA(i, 3) = color_statistic(j, 4);
color_statistic(j, 5) = 0;%已被作为监督聚类中心
center_ACA(i, 5)=1;%center_ACA(i, 4)用于储存该类具有的像素数,初始化为0,center_ACA(i, 5)用于储存该类具有的信息素浓度
break
end
end
end
ant_info = zeros (num, 5);
%ant_info((r-1)*ncol+c, 1) 储存到该点的距离;ant_info((r-1)*ncol+c, 2) 储存该点的信息素;ant_info((r-1)*ncol+c,3) 储存到该点的启发函数
%ant_info((r-1)*ncol+c, 4) 储存到该点的概率;ant_info((r-1)*ncol+c, 5)储存类别
%主程序
sum_ph=0;%概率公式的分母
do=0;%判断主程序是否应继续循环
do2=1;%判断类合并程序是否应继续循环
judge=zeros(nrow, ncol);%判断该类是否与别的类可以合并的矩阵
n=0;%判断ACA主程序循环次数
m=0;%判断类合并程序循环次数
while (do<=500)
%像素点聚类
for rr = 1 : nrow
for cc = 1 : ncol
if ant_matrix(rr, cc, 1)==0
ant_matrix(rr, cc, 1) = 2;%将前一个循环里未被归类的蚂蚁状态都改为待聚类
end
end
end
for rr = 1 : nrow
for cc = 1 : ncol
%计算像素(rr, cc)到各聚类中心的距离、信息素等多项信息
if ant_matrix(rr, cc, 1)==2
ant_info = zeros(num, 6);
sum_ph=0;
for i = 1 : num
ant_info(i, 1) = sqrt(p1 * (R(rr, cc) - center_ACA(i, 1)).^2 + p2 * (G(rr, cc) - center_ACA(i, 2)).^2 + p3 * (B(rr, cc) - center_ACA(i, 3)).^2);
ant_info(i, 2) = center_ACA(i, 5);
ant_info(i, 3)=radius/(ant_info(i, 1)+0.00001);%保证距离为0的时候,启发函数很大但不为无穷。
sum_ph = sum_ph + ant_info(i, 2).^alpha + ant_info(i, 3).^beta;
ant_info(i, 5) = i;
end
%计算到各聚类中心的概率
for i=1:num
ant_info(i, 4) = (ant_info(i, 2).^alpha + ant_info(i, 3).^beta)/sum_ph;
end
rand('state', sum(100*clock));
temp = find(cumsum(ant_info(:, 4)) >= rand(1), 1);
%路径的概率选择计算
if ant_info(temp, 4) >= lumda
ant_matrix(rr, cc, 1) = 1;%该像素已经被归类
cluster_img(rr, cc, 4) = temp;%记录该像素的类别
center_ACA(temp, 4) = center_ACA(temp, 4) + 1;%该聚类中包含的像素数加1
if ant_info(temp, 1) <= radius
center_ACA(temp, 5) = center_ACA(temp, 5) + 1;%若距离小于radius,则信息素加1
end
else
ant_matrix(rr, cc, 1) = 0;%像素未被归类,状态变为0
end
end
end
end
%更新聚类中心信息
for i = 1 : num
if ~(center_ACA(i, 4)==0)
sum1=0;
sum2=0;
sum3=0;
for rr = 1 : nrow
for cc = 1 : ncol
if cluster_img(rr, cc, 4)==i
sum1 = sum1 + cluster_img(rr, cc, 1);
sum2 = sum2 + cluster_img(rr, cc, 2);
sum3 = sum3 + cluster_img(rr, cc, 3);
end
end
end
center_ACA(i, 1) = sum1 / center_ACA(i, 4);
center_ACA(i, 2) = sum2 / center_ACA(i, 4);
center_ACA(i, 3) = sum3 / center_ACA(i, 4);
end
end
%类间合并
%初始化判断矩阵
while(do2==1)
judge=zeros(num, 1);
for i = 1 : num
if ~(center_ACA(i, 4)==0)
judge(i, 1)=1;
end
end
for i = 1 : num
if ~(center_ACA(i, 4)==0)
cluster_info = zeros(num, 2);%记录类间距离
temp=[d; 0];%第一个记录上次的距离值,第二个记录类别
for j = 1 : num
if (~(j==i))&&(~(center_ACA(j, 4)==0))
cluster_info(j, 1) = sqrt((center_ACA(i, 1) - center_ACA(j, 1)).^2 + (center_ACA(i, 2) - center_ACA(j, 2)).^2 + (center_ACA(i, 3) - center_ACA(j, 3)).^2);
cluster_info(j, 2) = j;
end
end
for j = 1 : num
if cluster_info(j, 1)<temp(1,1) && (~(j==i)) && (~(center_ACA(j, 4)==0))
temp(1, 1)=cluster_info(j, 1);
temp(2, 1)=cluster_info(j, 2);
%计算最相近的类
end
end
if temp(1,1)<d
for rr = 1 : nrow
for cc = 1 : ncol
if cluster_img(rr, cc, 4)==i;
cluster_img(rr, cc, 4) = temp(2,1);
%像素分类矩阵中,(rr, cc)点的像素被归类
end
end
end
center_ACA(temp(2, 1), 1) = (center_ACA(temp(2, 1), 1) * center_ACA(temp(2, 1), 4) + center_ACA(i, 1) * center_ACA(i, 4)) / (center_ACA(temp(2, 1), 4) + center_ACA(i, 4));
center_ACA(temp(2, 1), 2) = (center_ACA(temp(2, 1), 2) * center_ACA(temp(2, 1), 4) + center_ACA(i, 2) * center_ACA(i, 4)) / (center_ACA(temp(2, 1), 4) + center_ACA(i, 4));
center_ACA(temp(2, 1), 3) = (center_ACA(temp(2, 1), 3) * center_ACA(temp(2, 1), 4) + center_ACA(i, 3) * center_ACA(i, 4)) / (center_ACA(temp(2, 1), 4) + center_ACA(i, 4));
center_ACA(temp(2, 1), 4) = center_ACA(temp(2, 1), 4) + center_ACA(i, 4);
center_ACA(temp(2, 1), 5) = center_ACA(temp(2, 1), 5) + center_ACA(i, 5);%max(center_ACA(temp(2, 1), 5), center_ACA(i, 5)) + 0.3 * min(center_ACA(temp(2, 1), 5), center_ACA(i, 5));
center_ACA(i, 4) = 0;
center_ACA(i, 5) = 0;
judge(i, 1)=0;
judge(temp(2, 1), 1)=1;
else
judge(i, 1)=0;
end
end
end
do2=0;
for i = 1 : num
if judge(i, 1) == 1
do2=1;
break
end
end
end
%信息素挥发
for i = 1 : num
center_ACA(i, 5) = center_ACA(i, 5) * rho;
end
do = do + 1 %每循环一次,会显示一次do
end
for rr = 1 : nrow
for cc = 1 : ncol
if ant_matrix(rr, cc, 1)==1
cluster_img(rr, cc, 1) = center_ACA(cluster_img(rr, cc, 4), 1);
cluster_img(rr, cc, 2) = center_ACA(cluster_img(rr, cc, 4), 2);
cluster_img(rr, cc, 3) = center_ACA(cluster_img(rr, cc, 4), 3);
elseif ant_matrix(rr, cc, 1)==0
cluster_img(rr, cc, 1) = 0;
cluster_img(rr, cc, 2) = 0;
cluster_img(rr, cc, 3) = 0;
end
end
end
%figure(),imshow(cluster_img(:, :, 1:3)./255);
imwrite(cluster_img(:, :, 1:3)./255, 'Result1.bmp', 'bmp');
%FCM主程序
C = num;%类的数目为C
m = 2;%类参数设置
e = nrow * ncol * C * (0.01).^2;
sum_d = e+1;
%初始化类矩阵
center_FCM = zeros(C, 3);
for i = 1 : C
center_FCM(i, 1) = center_ACA(i, 1);
center_FCM(i, 2) = center_ACA(i, 2);
center_FCM(i, 3) = center_ACA(i, 3);
end
%初始化距离矩阵
distance=zeros(C, 1);
%利用ACA运行结果初始化隶属度矩阵
subjection = zeros(nrow, ncol, C);
subjection_temp = zeros(nrow, ncol, C);
for rr = 1 : nrow
for cc = 1 : ncol
if ~(ant_matrix(rr, cc, 1)==4)
for i = 1 : C
distance(i, 1) = sqrt((R(rr, cc)-center_FCM(i, 1)).^2 + (G(rr, cc)-center_FCM(i, 2)).^2 + (B(rr, cc)-center_FCM(i, 3)).^2);
end
do = 1;
for i = 1 : C
if distance(i, 1) == 0
subjection(rr, cc, i) = 1;%若某个像素到聚类中心的距离为0,则其隶属度为1
for j = 1 : C
if ~(j==i)
subjection(rr, cc, j) = 0;
do = 0;
end
end
end
break
end
if do == 1
normalize = 0;
for i = 1 : C
sum_distance = 0;
for j = 1 : C
sum_distance = sum_distance + (distance(i, 1)/distance(j, 1)).^(2/(m-1));
end
subjection(rr, cc, i) = 1 / sum_distance;
normalize = normalize + subjection (rr, cc, i);
end
for i = 1 : C
subjection(rr, cc, i) = (1 / normalize) * subjection(rr, cc, i);%隶属度归一化
end
end
end
end
end
end
figure
imshow(cluster_img2(:, :, 1:3)./255)
% center_FCM
%cluster_img2(:, :, 4)
imwrite(cluster_img2(:, :, 1:3)./255, 'Result2.bmp', 'bmp');
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291
- 292
- 293
- 294
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
- 305
- 306
- 307
- 308
- 309
- 310
- 311
- 312
- 313
- 314
- 315
- 316
- 317
- 318
- 319
- 320
- 321
- 322
- 323
- 324
- 325
- 326
- 327
- 328
- 329
- 330
- 331
- 332
- 333
- 334
- 335
- 336
- 337
- 338
- 339
- 340
- 341
- 342
- 343
- 344
- 345
- 346
- 347
- 348
- 349
- 350
- 351
- 352
- 353
- 354
- 355
- 356
- 357
- 358
- 359
- 360
- 361
- 362
- 363
- 364
- 365
- 366
- 367
- 368
- 369
- 370
- 371
- 372
- 373
- 374
- 375
- 376
- 377
- 378
- 379
- 380
- 381
- 382
- 383
- 384
- 385
- 386
- 387
- 388
- 389
- 390
- 391
- 392
- 393
- 394
- 395
- 396
- 397
- 398
- 399
- 400
- 401
- 402
- 403
- 404
- 405
- 406
- 407
- 408
- 409
- 410
- 411
- 412
- 413
- 414
- 415
- 416
- 417
- 418
- 419
- 420
- 421
- 422
- 423
- 424
- 425
- 426
- 427
- 428
- 429
- 430
- 431
- 432
- 433
- 434
- 435
- 436
- 437
- 438
- 439
- 440
- 441
- 442
- 443
四、运行结果
五、matlab版本及参考文献
1 matlab版本
2014a
2 参考文献
[1] 蔡利梅.MATLAB图像处理——理论、算法与实例分析[M].清华大学出版社,2020.
[2]杨丹,赵海滨,龙哲.MATLAB图像处理实例详解[M].清华大学出版社,2013.
[3]周品.MATLAB图像处理与图形用户界面设计[M].清华大学出版社,2013.
[4]刘成龙.精通MATLAB图像处理[M].清华大学出版社,2015.
[5]赵勇,方宗德,庞辉,王侃伟.基于量子粒子群优化算法的最小交叉熵多阈值图像分割[J].计算机应用研究. 2008,(04)
文章来源: qq912100926.blog.csdn.net,作者:海神之光,版权归原作者所有,如需转载,请联系作者。
原文链接:qq912100926.blog.csdn.net/article/details/113097542
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)