a b/libraries/lib_FastCMeans/FastFCMeans.m
1
function [C, U, LUT, H] = FastFCMeans(im, c, q, opt)
2
% Partition N-dimensional grayscale image into c classes using a memory 
3
% efficient implementation of the fuzzy c-means (FCM) clustering algorithm. 
4
% Computational efficiency is achieved by using the histogram of image
5
% intensities during clustering instead of the raw image data.
6
%
7
% INPUT:
8
%   - im  : N-dimensional grayscale image in integer format. 
9
%   - c   : positive integer greater than 1 specifying the number of
10
%           clusters. c=2 is the default setting. Alternatively, c can be
11
%           specified as a k-by-1 array of initial cluster (aka prototype)
12
%           centroids.
13
%   - q   : fuzzy weighting exponent. q must be a real number greater than
14
%           1.1. q=2 is the default setting. Increasing q leads to an
15
%           increased amount of fuzzification, while reducing q leads to
16
%           crispier class memberships. Note that while in principle
17
%           setting q==1 is equivalent to using a classical c-means 
18
%           algorithm, this setting cannot be used in practice because it
19
%           produces an infinite exponent in the membership update formula.
20
%   - opt : optional logical argument used to indicate how to initialize
21
%           cluster centroids. If opt=true {default} then centroids are
22
%           initialized are by sampling the intensity range at uniform
23
%           intervals. If opt=false then the initial centroids are set 
24
%           using the c-means algorithm. 
25
%
26
% OUTPUT  :
27
%   - C   : 1-by-k array of cluster centroids.
28
%   - U   : L-by-k array of fuzzy class memberships, where k is the number
29
%           of classes and L is the intensity range of the input image, 
30
%           such that L=numel(min(im(:)):max(im(:))).
31
%   - LUT : L-by-1 array that specifies the intensity-class relations,
32
%           where L is the dynamic intensity range of the input image. 
33
%           Specifically, LUT(1) corresponds to the (class) label assigned to 
34
%           min(im(:)) and LUT(L) corresponds to the label assigned 
35
%           to max(im(:)). LUT is used as input to 'apply_LUT' function to
36
%           create a label image.
37
%   - H   : image histogram. If I=min(im(:)):max(im(:)) are the intensities
38
%           present in the input image, then H(i) is the number of  
39
%           pixels/voxels with intensity I(i). 
40
%
41
% AUTHOR    : Anton Semechko (a.semechko@gmail.com)
42
%
43
44
45
% Default input arguments
46
if nargin<2 || isempty(c), c=2; end
47
if nargin<3 || isempty(q), q=2; end
48
if nargin<4 || isempty(opt), opt=true; end
49
50
% Basic error checking
51
if nargin<1 || isempty(im)
52
    error('Insufficient number of input arguments')
53
end
54
msg='Revise variable used to specify class centroids. See function documentation for more info.';
55
if ~isnumeric(c) || ~isvector(c)
56
    error(msg)
57
end
58
if numel(c)==1 && (~isnumeric(c) || round(c)~=c || c<2)
59
    error(msg)
60
end
61
if ~isnumeric(q) || numel(q)~=1 || q<1.1
62
    error('3rd input argument (q) must be a real number > 1.1')
63
end
64
if ~islogical(opt) || numel(opt)>1
65
    error('4th input argument (opt) must a Boolean')
66
end    
67
68
% Check image format
69
if isempty(strfind(class(im),'int'))
70
    error('Input image must be specified in integer format (e.g. uint8, int16)')
71
end
72
73
% Intensity range
74
Imin=double(min(im(:)));
75
Imax=double(max(im(:)));
76
I=(Imin:Imax)';
77
78
% Initialize cluster centroids
79
if numel(c)>1 % user-defined centroids
80
    C=c;
81
    opt=true;
82
else % automatic initialization
83
    if opt
84
        dI=(Imax-Imin)/c;
85
        C=Imin+dI/2:dI:Imax;
86
    else
87
        [C,~,H]=FastCMeans(im,c);
88
    end
89
end
90
91
% Compute intensity histogram
92
if opt
93
    H=hist(double(im(:)),I);
94
    H=H(:);
95
end
96
clear im
97
98
% Update fuzzy memberships and cluster centroids
99
dC=Inf;
100
while dC>1E-3
101
    
102
    C0=C;
103
    
104
    % Distance to the centroids
105
    D=abs(bsxfun(@minus,I,C));
106
    D=D.^(2/(q-1))+eps;
107
    
108
    % Compute fuzzy memberships
109
    U=bsxfun(@times,D,sum(1./D,2));
110
    U=1./(U+eps);
111
    
112
    % Update the centroids
113
    UH=bsxfun(@times,U.^q,H);
114
    C=sum(bsxfun(@times,UH,I),1)./sum(UH,1);
115
    C=sort(C,'ascend'); % enforce natural order
116
    
117
    % Change in centroids 
118
    dC=max(abs(C-C0));
119
    
120
end
121
122
% Defuzzify
123
[~,LUT]=max(U,[],2);
124