a b/libraries/lib_FastCMeans/FM2map.m
1
function Umap=FM2map(im,U,H)
2
% Unpack fuzzy-membership functions to produce membership maps.
3
% 
4
% INPUT:
5
%   - im    : N-dimensional grayscale image in integer format. 
6
%   - U     : L-by-c array of fuzzy class memberships, where c is the 
7
%             number of classes and L is the intensity range of the input 
8
%             image, such that L=numel(min(im(:)):max(im(:))). See 
9
%             'FastFCMeans' function for more info.
10
%   - H     : image histogram returned by 'FastFCMeans' function.
11
%
12
% OUTPUT:
13
%   - Umap  : membership maps. Umap has the same size as the input image
14
%             plus an additional dimension to account for c classes. For
15
%             example, if im is a 2D M-by-N image then U will be 
16
%             M-by-N-by-c array where U(:,:,i) is a membership map for the
17
%             i-th class.
18
%
19
% AUTHOR    : Anton Semechko (a.semechko@gmail.com)
20
%
21
22
23
if nargin<3 || isempty(H)
24
25
    % Intensity range
26
    Imin=double(min(im(:)));
27
    Imax=double(max(im(:)));
28
    I=(Imin:Imax)';
29
    
30
    % Intensity histogram
31
    H=hist(double(im(:)),I);
32
    H=H(:);
33
34
end
35
36
% Unpack memberships
37
Umap=zeros(sum(H),size(U,2));
38
i1=1; i2=0;
39
for i=1:numel(H)
40
    i2=i2+H(i);
41
    Umap(i1:i2,:)=repmat(U(i,:),[H(i) 1]);
42
    i1=i2+1;
43
end
44
45
% Find positional mapping
46
[~,idx]=sort(im(:), 'ascend');
47
[~,idx]=sort(idx(:),'ascend');
48
49
% Reshape membership maps to match image dimensions
50
Umap=reshape(Umap(idx,:),[size(im) size(U,2)]);
51