From 7b3a3df01c4ddb84a8abe45480ef1bc5e726edb1 Mon Sep 17 00:00:00 2001 From: Apostolof Date: Sat, 11 May 2019 20:03:15 +0300 Subject: [PATCH] Init mySpectralClustering --- Assignment_2/Image2Graph.m~ | 36 ----------------------------- Assignment_2/mySpectralClustering.m | 15 ++++++++++++ 2 files changed, 15 insertions(+), 36 deletions(-) delete mode 100644 Assignment_2/Image2Graph.m~ create mode 100644 Assignment_2/mySpectralClustering.m diff --git a/Assignment_2/Image2Graph.m~ b/Assignment_2/Image2Graph.m~ deleted file mode 100644 index aed0513..0000000 --- a/Assignment_2/Image2Graph.m~ +++ /dev/null @@ -1,36 +0,0 @@ -function myAffinityMat = Image2Graph (imIn) -%Image2Graph produces a graph, in the form of a matrix, from an image -% Usage myAffinityMat = Image2Graph (imIn), where: -% Inputs -% - imIn is the input image -% Output -% - myAffinityMat is the graph produced - - imageWidth = size(imIn, 2); - imageHeight = size(imIn, 1); - imageChannels = size(imIn, 3); - - %myAffinityMat = 1 / exp(norm(imIn(:, :))); - myAffinityMat = zeros(imageWidth * imageHeight, imageWidth * imageHeight); - - for row = 1:imageHeight - for column = 1:imageWidth - diff = imIn - imIn(row, column, :); - %diffT = permute(diff, [2 1 3]); - - dist = sum(diff .^ 2, 3) .^ 0.5; - -% dist = zeros(imageWidth, imageHeight); -% for channel = 1:imageChannels -% dist = dist + sqrt(sum (diff(:, :, channel) * diffT(:, :, channel))); -% dist = dist + sqrt(diff(:, :, channel) * diffT(:, :, channel)); -% end - - som = 1 ./ exp(dist); - som2 = reshape(som, 1, []); - som2 = reshape(som.', 1, []); - - myAffinityMat((row - 1) * imageWidth + column, :) = reshape((1 ./ exp(dist), 1, []); - end - end -end diff --git a/Assignment_2/mySpectralClustering.m b/Assignment_2/mySpectralClustering.m new file mode 100644 index 0000000..2e5834e --- /dev/null +++ b/Assignment_2/mySpectralClustering.m @@ -0,0 +1,15 @@ +function clusterIdx = mySpectralClustering (anAffinityMat, k) +%Implementation of spectral clustering +% Usage clusterIdx = mySpectralClustering (anAffinityMat, k), where: +% Inputs +% - anAffinityMat is a rectagular, symmetrical affinity matrix +% representation of an image +% - k is the desired number of clusters +% Output +% - clusterIdx is a vector storing the cluster Id of each node + + L = diag(sum(anAffinityMat, 2)) - anAffinityMat; + [eigenvectorsMatrix, ~] = eigs(L, k, 'sm'); + clusterIdx = kmeans(eigenvectorsMatrix, k); +end +