-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tcut.m
48 lines (40 loc) · 1.79 KB
/
Tcut.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This is a demo for the MDEC-HC, MDEC-SC, and MDEC-BG algorithms, %
% which are proposed in the following paper: %
% %
% D. Huang, C.-D. Wang, J.-H. Lai, and C.-K. Kwoh. %
% "Toward Multi-Diversified Ensemble Clustering of High-Dimensional %
% Data: From Subspaces to Metrics and Beyond". %
% IEEE Transactions on Cybernetics, 2022, 52(11), pp.12231-12244. %
% DOI: https://doi.org/10.1109/TCYB.2021.3049633 %
% %
% The code has been tested in Matlab R2016a and Matlab R2016b. %
% GigHub: https://github.com/huangdonghere/MDEC %
% Written by Huang Dong. (huangdonghere@gmail.com) %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function labels = Tcut(B,Nseg)
[Nx,Ny] = size(B);
if Ny < Nseg
error('Need more superpixels!');
end
% build the graph
dx = sum(B,2);
dx(dx==0) = eps;
Dx = sparse(1:Nx,1:Nx,1./dx); clear dx
Wy = B'*Dx*B;
%%% compute Ncut eigenvectors
% normalized affinity matrix
d = sum(Wy,2);
d(d==0) = eps;
D = sparse(1:Ny,1:Ny,1./sqrt(d)); clear d
nWy = D*Wy*D; clear Wy
nWy = (nWy+nWy')/2;
% computer eigenvectors
[evec,eval] = eig(full(nWy)); clear nWy % use eigs for large superpixel graphs
[~,idx] = sort(diag(eval),'descend');
Ncut_evec = D*evec(:,idx(1:Nseg)); clear D
% compute the Ncut eigenvectors on the entire bipartite graph (transfer!)
evec = Dx * B * Ncut_evec; clear B Dx Ncut_evec
% normalize each row to unit norm
evec = bsxfun( @rdivide, evec, sqrt(sum(evec.*evec,2)) + 1e-10 );
labels = kmeans(evec,Nseg,'MaxIter',100,'Replicates',3);