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.

28 lines
1.1 KiB

function nCutValue = calculateNcut (anAffinityMat , clusterIdx)
%Calculation of the NCut metric
% Usage nCutValue = calculateNcut (anAffinityMat , clusterIdx), where:
% Inputs
% - anAffinityMat is a rectangular, symmetrical affinity matrix
% representation of an image
% - clusterIdx is a vector storing the cluster Id of each node
% Output
% - nCutValue is a vector storing the cluster Id of each node
% Gets the unique cluster IDs
clusterIds = unique(clusterIdx);
if size(clusterIds, 1) ~= 2
error('Too many different clusters! Number of clusters must be two');
end
% Finds the indices of the samples of each cluster
clusterOneIndices = (clusterIdx == clusterIds(1));
clusterTwoIndices = (clusterIdx == clusterIds(2));
% Calculates the N-Cut metric
nCutValue = 2 - ...
(sum(sum(anAffinityMat(clusterOneIndices, clusterOneIndices))) / ...
sum(sum(anAffinityMat(clusterOneIndices, :))) + ...
sum(sum(anAffinityMat(clusterTwoIndices, clusterTwoIndices))) / ...
sum(sum(anAffinityMat(clusterTwoIndices, :))));
end