Fuzzy CMeans algorithm (FCM) image
segmentation MATLAB program
Tag: the matlab, OUTput, image, c the
Category: C
Author: linxiyyz
Date: 20131124
Fuzzy CMeans algorithm (FCM) image segmentation MATLAB program
The subject used Fuzzy CMeans (FCM) algorithm image segmentation, write a MATLAB program to friends in need, for reference only. If wrong with that, and hope to be enlighten.
% File name: FCM.m
% Function: Fuzzy CMeans (FCM) clustering algorithm for image segmentation %: Rain curtain
% Time: 11/17/2011
Image%%%%%%%%%%%%%%%%%%%%%%%%%%%%% FCM algorithm for segmentation function clusterResult = FCM (imagePath, C, V, M, iter, epsm)
Fuzzy CMeans (FCM) clustering algorithm for image segmentation % ClusterResult = FCM (imagePath, C, V, M, iter, epsm)
% Example: clusterResult = FCM ('E: \ Image \ lena.bmp') % ClusterResult = FCM ('E: \ Image \ lena.bmp', 3, [0 127 255]) % Input:
% ImagePath: image path
% C: the number of categories, the default value of 2 % V: initialize the cluster center, the default value [0255] % M: weighted index, the default value of 2
% Iter: number of iterations, the default value is 100
For% epsm: iteration stops threshold, the default value is 1.0E2 % Output:
The% clusterResult: clustering results % Note:
The value of% C and V initialization of cluster centers % Set the default value
if nargin <6 epsm = 1.0e2; end
if nargin <5 iter = 100; end
if nargin <4 M = 2; end
if nargin <3 V = [255]; end
if nargin <2 C = 2; end
I = imread (ImagePath); figure, imshow (I); title ('original image'); [Row col] = size (I); grayHist = imhist (I); the figure, imhist (I); title ('histogram');
histProb = grayHist / (row * col); len = length (histProb);
tic
% FCM iterative process cnt = 0;
while (cnt <iter)
% Of the membership function (note that special consideration to the case of a pixel and the cluster center) for i = 1: len
flag = 0; for j = 1: C if i == V (j), U (j, i) = 1.0; if j == 1
U (j + 1: C, i) = 0.0; elseif j == C
U (1: C 1, i) = 0.0; else
U (1: j 1, i) = 0.0; U (j + 1: C, i) = 0.0; end
flag = 1; break; end end
if flag == 0
u = (1.0. / ((i V). ^ 2)). ^ (1.0 / (M 1)); uSum = sum (u);
U (1: C, i) = u '/ uSum; end
end
% Updating various cluster center for j = 1: C
i = linspace (1, len, len);
v = sum (histProb '. * i. * (U (j, :). ^ M)); vSum = sum (histProb '. * (U (j, :). ^ M)); if vSum == 0
clusterResult (j) = 0; else
clusterResult (j) = v / vSum; end
end
if diff <= epsm break;
else
V = clusterResult; end
cnt = cnt + 1; end
toc
% Image segmentation for i = 1: row
for j = 1: col
temp = (double (I (i, j)) clusterResult). ^ 2; [Fmin pos] = min (temp);
I (i, j) = pos * 255 / C; end
end
figure, imshow (uint8 (I));
title ('after the segmentation of the image'); disp ('the number of iterations of: iterTimes ='); disp (CNT);