Browse Source

Init myNCuts, Fixes, Add script for easier testing

master
Apostolos Fanakis 6 years ago
parent
commit
4ae9823ab8
  1. 22
      Assignment_2/Image2Graph.m
  2. 19
      Assignment_2/myNCuts.m
  3. 7
      Assignment_2/mySpectralClustering.m
  4. 35
      Assignment_2/tester.m

22
Assignment_2/Image2Graph.m

@ -9,25 +9,11 @@ function myAffinityMat = Image2Graph (imIn)
% Initializes helper variables % Initializes helper variables
imageWidth = size(imIn, 2); imageWidth = size(imIn, 2);
imageHeight = size(imIn, 1); imageHeight = size(imIn, 1);
imageChannels = size(imIn, 3);
% 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
som = reshape(imIn, 1, [], 3); singleLineTransform = reshape(imIn, 1, [], imageChannels);
diff = repmat(som, imageWidth * imageHeight, 1, 1) - permute(som, [2 1 3]); diff = repmat(singleLineTransform, imageWidth * imageHeight, 1, 1) - ...
permute(singleLineTransform, [2 1 3]);
dist = sum(diff .^ 2, 3) .^ 0.5; dist = sum(diff .^ 2, 3) .^ 0.5;
myAffinityMat = 1 ./ exp(dist); myAffinityMat = 1 ./ exp(dist);
end end

19
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

7
Assignment_2/mySpectralClustering.m

@ -8,8 +8,11 @@ function clusterIdx = mySpectralClustering (anAffinityMat, k)
% Output % Output
% - clusterIdx is a vector storing the cluster Id of each node % - 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; L = diag(sum(anAffinityMat, 2)) - anAffinityMat;
[eigenvectorsMatrix, ~] = eigs(L, k, 'sm'); [eigenvectorsMatrix, ~] = eigs(double(L), k, 'sm');
clusterIdx = kmeans(eigenvectorsMatrix, k); clusterIdx = kmeans(eigenvectorsMatrix, k);
end end

35
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
Loading…
Cancel
Save