diff --git a/Assignment_2/Image2Graph.m b/Assignment_2/Image2Graph.m index 5d81366..7d4e27e 100644 --- a/Assignment_2/Image2Graph.m +++ b/Assignment_2/Image2Graph.m @@ -9,25 +9,11 @@ function myAffinityMat = Image2Graph (imIn) % Initializes helper variables imageWidth = size(imIn, 2); imageHeight = size(imIn, 1); - - % Initializes output matrix -% myAffinityMat = zeros(imageWidth * imageHeight, imageWidth * ... -% imageHeight); - -% for row = 1:imageHeight -% for column = 1:imageWidth -% % Calculates distance matrix for the current element -% diff = imIn - imIn(row, column, :); -% dist = sum(diff .^ 2, 3) .^ 0.5; -% -% % Calculates affinity matrix -% myAffinityMat((row - 1) * imageWidth + column, :) = ... -% reshape((1 ./ exp(dist)).', 1, []); -% end -% end + imageChannels = size(imIn, 3); - som = reshape(imIn, 1, [], 3); - diff = repmat(som, imageWidth * imageHeight, 1, 1) - permute(som, [2 1 3]); + singleLineTransform = reshape(imIn, 1, [], imageChannels); + diff = repmat(singleLineTransform, imageWidth * imageHeight, 1, 1) - ... + permute(singleLineTransform, [2 1 3]); dist = sum(diff .^ 2, 3) .^ 0.5; myAffinityMat = 1 ./ exp(dist); end diff --git a/Assignment_2/myNCuts.m b/Assignment_2/myNCuts.m new file mode 100644 index 0000000..9e2a6cf --- /dev/null +++ b/Assignment_2/myNCuts.m @@ -0,0 +1,19 @@ +function clusterIdx = myNCuts (anAffinityMat, k) +%Implementation of the N-cuts algorithm +% Usage clusterIdx = myNCuts (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 + + if ~issymmetric(anAffinityMat) + error('The affinity matrix provided is not symmetric.'); + end + + D = diag(sum(anAffinityMat, 2)); + L = D - anAffinityMat; + [eigenvectorsMatrix, ~] = eigs(double(L), double(D), k, 'sm'); + clusterIdx = kmeans(eigenvectorsMatrix, k); +end diff --git a/Assignment_2/mySpectralClustering.m b/Assignment_2/mySpectralClustering.m index 2e5834e..de999f5 100644 --- a/Assignment_2/mySpectralClustering.m +++ b/Assignment_2/mySpectralClustering.m @@ -8,8 +8,11 @@ function clusterIdx = mySpectralClustering (anAffinityMat, k) % Output % - clusterIdx is a vector storing the cluster Id of each node + if ~issymmetric(anAffinityMat) + error('The affinity matrix provided is not symmetric.'); + end + L = diag(sum(anAffinityMat, 2)) - anAffinityMat; - [eigenvectorsMatrix, ~] = eigs(L, k, 'sm'); + [eigenvectorsMatrix, ~] = eigs(double(L), k, 'sm'); clusterIdx = kmeans(eigenvectorsMatrix, k); end - diff --git a/Assignment_2/tester.m b/Assignment_2/tester.m new file mode 100644 index 0000000..24e9bfa --- /dev/null +++ b/Assignment_2/tester.m @@ -0,0 +1,35 @@ +image = imageT; +k = 3; + +graph = Image2Graph(image); +%clusters = mySpectralClustering(graph, k); +clusters = myNCuts(graph, k); + +clusters = reshape(clusters, size(image, 1), []); + +redChannel = image(:, :, 1); +greenChannel = image(:, :, 2); +blueChannel = image(:, :, 3); + +segImR = clusters; +segImG = clusters; +segImB = clusters; + +for cluster = 1:k + meanR = mean(redChannel(clusters == cluster)); + meanG = mean(greenChannel(clusters == cluster)); + meanB = mean(blueChannel(clusters == cluster)); + + segImR(clusters == cluster) = meanR; + segImG(clusters == cluster) = meanG; + segImB(clusters == cluster) = meanB; +end + +segIm = zeros(size(image, 1), size(image, 2), 3); +segIm(:, :, 1) = segImR; +segIm(:, :, 2) = segImG; +segIm(:, :, 3) = segImB; +imshow(segIm) + +clearvars segImR segImG segImB meanR meanG meanB graph redChannel ... + greenChannel blueChannel clusters k image cluster segIm \ No newline at end of file