【图像分割】基于matlab直觉模糊C均值聚类图像分割IFCM【含Matlab源码 120期】
【摘要】
一、图像分割简介
理论知识参考:【基础教程】基于matlab图像处理图像分割【含Matlab源码 191期】
二、部分源代码
clear
close all
clc
clear
close all...
一、图像分割简介
理论知识参考:【基础教程】基于matlab图像处理图像分割【含Matlab源码 191期】
二、部分源代码
clear
close all
clc
clear
close all
clc
%% %%%%%%%%%%%%%%%图像%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
I=imread('3096.jpg');
if size(I,3) == 3
I=rgb2gray(I);
else
end
figure;imshow(I);title('(a)原始图像');imwrite(I,'1.tiff','tiff','Resolution',300);%保存为tif
%I=I;%不加噪声
% I=imnoise(I,'speckle',deta_2);
I=imnoise(I,'salt & pepper',0.05); %加噪图椒盐噪声
% I=imnoise(I,'gaussian',0,0.01); % 加高斯噪声
figure;imshow(I);title('(b)加噪图像');imwrite(I,['2.','.tiff'],'tiff','Resolution',300);%保存为tif
[m,n]=size(I);
% k=4;
k=2;
r=3;
m_index=2;
beta=6;
neighbor=3;%3*3邻域去噪
lamda_s_enfcm=3;%s参数
I=im2double(I);
I4 = I(:); %% 将图像灰度按列排列
%% ------------------------ ifcm------------------------
ifcm_label=zeros(m*n,1);
t=cputime;
tic;
[O2, U2, obj_fcn2] = ifcm(I4, k);
toc;
time_fcm_spatial_mean=cputime-t;
maxU2 = max(U2); %隶属度最大
for j=1:k
index = find(U2(j, :) == maxU2); %隶属度最大对应的像素位置
ifcm_label(index) = j;
end
labels2=reshape(ifcm_label,[m n]);
%显示聚类分割图
labels2(find(labels2==1))=0;
labels2(find(labels2==2))=255;
labels2(find(labels2==3))=125;
labels2(find(labels2==4))=180;
labels2=uint8(labels2);
% figure;imshow(labels2,[]);title('(c)聚类分割图');imwrite(labels2,'3.jpg');imwrite(labels2,'33.png')%保存为jpg
figure;imshow(labels2,[]);title('(c)聚类分割图');
imwrite(labels2,['3.1.','.tiff'],'tiff','Resolution',300);%保存为tif
function [center, U, obj_fcn] = ifcm(data,cluster_n)
if nargin ~= 2 & nargin ~= 3,
error('Too many or too few input arguments!');
end
data_n = size(data, 1);
in_n = size(data, 2);
% Change the following to set default options
default_options = [2; % exponent for the partition matrix U
100; % max. number of iteration
1e-5; % min. amount of improvement
1]; % info display during iteration
if nargin == 2,
options = default_options;
else
% If "options" is not fully specified, pad it with default values.
if length(options) < 4,
tmp = default_options;
tmp(1:length(options)) = options;
options = tmp;
end
% If some entries of "options" are nan's, replace them with defaults.
nan_index = find(isnan(options)==1);
options(nan_index) = default_options(nan_index);
if options(1) <= 1,
error('The exponent should be greater than 1!');
end
end
expo = options(1); % Exponent for U
max_iter = options(2); % Max. iteration
min_impro = options(3); % Min. improvement
display = options(4); % Display info or not
obj_fcn = zeros(max_iter, 1); % Array for objective function
%U = initfcm(cluster_n, data_n); % Initial fuzzy partition
center= initifcmv(cluster_n); %初始化聚类中心
[Miuij,Vij,Paiij]=initifcmmvp(data);
for i = 1:max_iter,
[U, center, obj_fcn(i)] = ifcm_step(data, center, cluster_n, expo, Miuij,Vij,Paiij);
if display,
fprintf('Iteration count = %d, obj. fcn = %f\n', i, obj_fcn(i));
end
% check termination condition
if i>1
% if abs(obj_fcn(i) - obj_fcn(i-1)) < min_impro, break; end,
% if norm(old_U-U,2) < min_impro, break; end,
% if norm(old_U-U,'fro') < min_impro, break; end,
if norm(old_center-center,2) < min_impro, break; end,
% (old_center-center).^2
end
old_U = U;
old_center = center;
end
% iter_n = i; % Actual number of iterations
% % obj_fcn(iter_n+1:max_iter) = [];
% mf = old_U.^expo; % 隶属度矩阵进行指数运算结果
% dist = distfcm(old_center, data); % 计算距离矩阵
% obj_fcn = sum(sum((dist.^2).*mf)); % 计算目标函数值
- 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
三、运行结果
四、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/112907955
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)