Semester assignments for the course "Digital Image Processing" of THMMY in AUTH university.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.5 KiB

function clusterIdx = recursiveNCuts(anAffinityMat)
%Implementation of the recursive N-cuts algorithm
% Usage clusters = recursiveNCuts (anAffinityMat), where:
% Inputs
% - anAffinityMat is a rectangular, symmetrical affinity matrix
% representation of an image
% Output
% - clusterIdx is a vector storing the cluster Id of each node
% Makes sure preconditions are met
if ~issymmetric(anAffinityMat)
error('The affinity matrix provided is not symmetric.');
end
% Executes clustering using N-Cuts algorithm
clusterIdx = myNCuts(anAffinityMat, 2);
% Checks stop conditions
if (nnz(clusterIdx == 1) < 5 || nnz(clusterIdx == 2) < 5)
return;
end
NCut = calculateNcut(anAffinityMat, clusterIdx);
if (NCut > 0.85)
return;
end
% Finds indices of each cluster
clusterOneIndices = (clusterIdx == 1);
clusterTwoIndices = (clusterIdx == 2);
% Recursively calls itself for each part of the matrix
firstSubClusters = recursiveNCuts(anAffinityMat(clusterOneIndices, ...
clusterOneIndices));
secondSubClusters = recursiveNCuts(anAffinityMat(clusterTwoIndices, ...
clusterTwoIndices));
% Makes sure the IDs of the clusters of each half are unique
secondSubClusters = secondSubClusters + size(unique(firstSubClusters), 1);
% Re-merges the sub-cluster arrays
clusterIdx(clusterOneIndices) = firstSubClusters;
clusterIdx(clusterTwoIndices) = secondSubClusters;
end