www.pudn.com > FCM.zip > FCM2.m, change:2012-03-05,size:1319b


clear 
close all 
I=double(imread('PeppersRGB.tif')); 
result=double(zeros(size(I))); 
 
[m,n]=size(I); 
data1=double(I(:,:,1)); 
data2=double(I(:,:,2)); 
data3=double(I(:,:,3)); 
data=[data1(:),data2(:),data3(:)]; 
 
cluster_n=3; 
data_n=size(data,1);%the number of vectors 
max_iter=100;%the maximum of iteration count 
 
m=255*rand(cluster_n,size(data,2)); % initialize the centroids 
% Main loop 
U=zeros(cluster_n,size(data,1)); 
for i = 1:max_iter, 
%update the membership matrix U and the clustering center; 
[U, m,m0 ] = updateU(data,m);%calculate the membership matrix 
% decide whether to stop the algorithm 
count=0; 
for j=1:cluster_n 
if dist(m(j,:),m0(j,:)) < 0.01 
    count=count+1; 
end 
end 
if count==cluster_n 
    break; 
end 
end 
 
iter_n = i; % the actual number of iteration 
wholeD = zeros(size(data)); 
for k = 1:cluster_n  
    for t=1:3 
        Ik = U(k,:).*(data(:,t))'; 
        result(:,:,t) = reshape(Ik,m,n); 
    end 
output{k} = result; 
end 
%display the whole image after segmentaion 
figure(); 
subplot(221),imshow(I); 
set(gcf,'NextPlot','add'); 
% The first group 
subplot(222),imshow(output{1},[]); 
set(gcf,'NextPlot','add'); 
% The second group 
subplot(223),imshow(output{2},[]); 
set(gcf,'NextPlot','add'); 
 
subplot(224),imshow(output{3},[]); 
set(gcf,'NextPlot','add');