www.pudn.com > FCM.zip > FCM1.m, change:2012-03-06,size:1649b


clear 
close all 
 
cluster_n=3; 
I=imread('PeppersRGB.tif'); 
 
%the feature vector is hsv 
I_hsv=rgb2hsv(I); 
data1=double(I_hsv(:,:,1)); 
data2=double(I_hsv(:,:,2)); 
data3=double(I_hsv(:,:,3)); 
 
[m,n,z]=size(I); 
result=zeros(m,n,z); 
r=double(I(:,:,1)); 
g=double(I(:,:,2)); 
b=double(I(:,:,3)); 
 
%The feature vector is rgb 
% data1=r; 
% data2=g; 
% data3=b; 
 
 %The feature vector is lst 
% data1=(r+g+b)/3; 
% data2=(r-b)/2; 
% data3=(2*g-r-b)/4; 
% I(:,:,1)=data1; 
% I(:,:,2)=data2; 
% I(:,:,3)=data3; 
 
 
data=[data1(:),data2(:),data3(:)]; 
data_n=size(data,1); 
max_iter=100; 
expo=2; 
min_impro=1; 
U = initfcm(cluster_n, data_n); % initialize the membership matrix 
% Main loop 
for i = 1:max_iter, 
%update the membership matrix U and the clustering center; 
[U, center, obj_fcn(i)] = stepfcm(data, U, cluster_n, expo); 
% decide whether to stop the algorithm 
if i > 1, 
    if abs(obj_fcn(i) - obj_fcn(i-1)) < min_impro, 
    break; 
    end, 
end 
end 
iter_n = i; % the actual number of iteration 
% obj_fcn(iter_n+1:max_iter) = []; 
maxU = max(U); 
% data = data'; 
 
% output=zeros(m,n,z); 
for k = 1:cluster_n  
    indexk = (U(k,:) == maxU); 
    for t=1:3 
    Ik = indexk.*(data(:,t))'; 
    result(:,:,t) = reshape(Ik,m,n); 
    end 
% result(:,:,k)=reshape(indexk,m,n); 
% output=output+result; 
    output{k} = mat2gray(result); 
end 
 
%display the whole image after segmentaion 
figure(); 
subplot(221),imagesc(I_hsv),title('Original image (h,s,v)'); 
subplot(222),imagesc(output{1}),title('Group one'); 
subplot(223),imagesc(output{2}),title('Group two'); 
subplot(224),imagesc(output{3}),title('Group three');