Browse Source

Various fixes and improvements

master
Apostolos Fanakis 6 years ago
parent
commit
1730231300
  1. 11
      Assignment_2/Image2Graph.m
  2. 12
      Assignment_2/calculateNcut.m
  3. 20
      Assignment_2/demo1.m
  4. 43
      Assignment_2/demo2.m
  5. 43
      Assignment_2/demo3a.m
  6. 3
      Assignment_2/demo3b.m
  7. 46
      Assignment_2/demo3c.m
  8. 35
      Assignment_2/meanClustersColorRGB.m
  9. 13
      Assignment_2/myNCuts.m
  10. 13
      Assignment_2/mySpectralClustering.m
  11. 43
      Assignment_2/recursiveNCuts.m

11
Assignment_2/Image2Graph.m

@ -11,9 +11,12 @@ function myAffinityMat = Image2Graph (imIn)
imageHeight = size(imIn, 1); imageHeight = size(imIn, 1);
imageChannels = size(imIn, 3); imageChannels = size(imIn, 3);
% Produces the matrix
singleLineTransform = reshape(imIn, 1, [], imageChannels); singleLineTransform = reshape(imIn, 1, [], imageChannels);
diff = repmat(singleLineTransform, imageWidth * imageHeight, 1, 1) - ... myAffinityMat = 1 ./ exp( ...
permute(singleLineTransform, [2 1 3]); sum(( ...
dist = sum(diff .^ 2, 3) .^ 0.5; repmat(singleLineTransform, imageWidth * imageHeight, 1, 1) - ...
myAffinityMat = 1 ./ exp(dist); permute(singleLineTransform, [2 1 3])) ...
.^ 2, 3) ...
.^ 0.5);
end end

12
Assignment_2/calculateNcut.m

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

20
Assignment_2/demo1.m

@ -1,3 +1,4 @@
%% Initialization
clear clear
clear all clear all
@ -6,20 +7,27 @@ rng(1);
load('dip_hw_2.mat'); load('dip_hw_2.mat');
%% Executes experiments for the affinity matrix
% Clustering into two clusters
clusters = mySpectralClustering(d1a, 2); clusters = mySpectralClustering(d1a, 2);
% Presents results
clusters = clusters ./ 2; clusters = clusters ./ 2;
%clusters = reshape(clusters, 4, []);
figure(); figure();
imshow(clusters); imshow(clusters');
% Clustering into three clusters
clusters = mySpectralClustering(d1a, 3); clusters = mySpectralClustering(d1a, 3);
% Presents results
clusters = clusters ./ 3; clusters = clusters ./ 3;
%clusters = reshape(clusters, 4, []);
figure(); figure();
imshow(clusters); imshow(clusters');
% Clustering into four clusters
clusters = mySpectralClustering(d1a, 4); clusters = mySpectralClustering(d1a, 4);
% Presents results
clusters = clusters ./ 4; clusters = clusters ./ 4;
%clusters = reshape(clusters, 4, []);
figure(); figure();
imshow(clusters); imshow(clusters');

43
Assignment_2/demo2.m

@ -1,3 +1,4 @@
%% Initialization
clear clear
clear all clear all
@ -14,19 +15,28 @@ graph2 = Image2Graph(d2b);
figure(); figure();
imshow(d2a); imshow(d2a);
% Clustering into two clusters
clusters = mySpectralClustering(graph1, 2); clusters = mySpectralClustering(graph1, 2);
% Presents results
clusters = clusters ./ 2; clusters = clusters ./ 2;
clusters = reshape(clusters, size(d2a, 1), []); clusters = reshape(clusters, size(d2a, 1), []);
figure(); figure();
imshow(clusters); imshow(clusters);
% Clustering into three clusters
clusters = mySpectralClustering(graph1, 3); clusters = mySpectralClustering(graph1, 3);
% Presents results
clusters = clusters ./ 3; clusters = clusters ./ 3;
clusters = reshape(clusters, size(d2a, 1), []); clusters = reshape(clusters, size(d2a, 1), []);
figure(); figure();
imshow(clusters); imshow(clusters);
% Clustering into four clusters
clusters = mySpectralClustering(graph1, 4); clusters = mySpectralClustering(graph1, 4);
% Presents results
clusters = clusters ./ 4; clusters = clusters ./ 4;
clusters = reshape(clusters, size(d2a, 1), []); clusters = reshape(clusters, size(d2a, 1), []);
figure(); figure();
@ -36,26 +46,35 @@ imshow(clusters);
figure(); figure();
imshow(d2b); imshow(d2b);
% Clustering into two clusters
clusters = mySpectralClustering(graph2, 2); clusters = mySpectralClustering(graph2, 2);
% Presents results
figure(); figure();
imshow(meanClustersColorRGB(d2b, reshape(clusters, size(d2b, 1), []))); imshow(meanClustersColorRGB(d2b, reshape(clusters, size(d2b, 1), [])));
% clusters = clusters ./ 2; clusters = clusters ./ 2;
% clusters = reshape(clusters, size(d2a, 1), []); clusters = reshape(clusters, size(d2a, 1), []);
% figure(); figure();
% imshow(clusters); imshow(clusters);
% Clustering into three clusters
clusters = mySpectralClustering(graph2, 3); clusters = mySpectralClustering(graph2, 3);
% Presents results
figure(); figure();
imshow(meanClustersColorRGB(d2b, reshape(clusters, size(d2b, 1), []))); imshow(meanClustersColorRGB(d2b, reshape(clusters, size(d2b, 1), [])));
% clusters = clusters ./ 3; clusters = clusters ./ 3;
% clusters = reshape(clusters, size(d2a, 1), []); clusters = reshape(clusters, size(d2a, 1), []);
% figure(); figure();
% imshow(clusters); imshow(clusters);
% Clustering into four clusters
clusters = mySpectralClustering(graph2, 4); clusters = mySpectralClustering(graph2, 4);
% Presents results
figure(); figure();
imshow(meanClustersColorRGB(d2b, reshape(clusters, size(d2b, 1), []))); imshow(meanClustersColorRGB(d2b, reshape(clusters, size(d2b, 1), [])));
% clusters = clusters ./ 4; clusters = clusters ./ 4;
% clusters = reshape(clusters, size(d2a, 1), []); clusters = reshape(clusters, size(d2a, 1), []);
% figure(); figure();
% imshow(clusters); imshow(clusters);

43
Assignment_2/demo3a.m

@ -1,3 +1,4 @@
%% Initialization
clear clear
clear all clear all
@ -14,19 +15,28 @@ graph2 = Image2Graph(d2b);
figure(); figure();
imshow(d2a); imshow(d2a);
% Clustering into two clusters
clusters = myNCuts(graph1, 2); clusters = myNCuts(graph1, 2);
% Presents results
clusters = clusters ./ 2; clusters = clusters ./ 2;
clusters = reshape(clusters, size(d2a, 1), []); clusters = reshape(clusters, size(d2a, 1), []);
figure(); figure();
imshow(clusters); imshow(clusters);
% Clustering into three clusters
clusters = myNCuts(graph1, 3); clusters = myNCuts(graph1, 3);
% Presents results
clusters = clusters ./ 3; clusters = clusters ./ 3;
clusters = reshape(clusters, size(d2a, 1), []); clusters = reshape(clusters, size(d2a, 1), []);
figure(); figure();
imshow(clusters); imshow(clusters);
% Clustering into four clusters
clusters = myNCuts(graph1, 4); clusters = myNCuts(graph1, 4);
% Presents results
clusters = clusters ./ 4; clusters = clusters ./ 4;
clusters = reshape(clusters, size(d2a, 1), []); clusters = reshape(clusters, size(d2a, 1), []);
figure(); figure();
@ -36,26 +46,35 @@ imshow(clusters);
figure(); figure();
imshow(d2b); imshow(d2b);
% Clustering into two clusters
clusters = myNCuts(graph2, 2); clusters = myNCuts(graph2, 2);
% Presents results
figure(); figure();
imshow(meanClustersColorRGB(d2b, reshape(clusters, size(d2b, 1), []))); imshow(meanClustersColorRGB(d2b, reshape(clusters, size(d2b, 1), [])));
% clusters = clusters ./ 2; clusters = clusters ./ 2;
% clusters = reshape(clusters, size(d2a, 1), []); clusters = reshape(clusters, size(d2a, 1), []);
% figure(); figure();
% imshow(clusters); imshow(clusters);
% Clustering into three clusters
clusters = myNCuts(graph2, 3); clusters = myNCuts(graph2, 3);
% Presents results
figure(); figure();
imshow(meanClustersColorRGB(d2b, reshape(clusters, size(d2b, 1), []))); imshow(meanClustersColorRGB(d2b, reshape(clusters, size(d2b, 1), [])));
% clusters = clusters ./ 3; clusters = clusters ./ 3;
% clusters = reshape(clusters, size(d2a, 1), []); clusters = reshape(clusters, size(d2a, 1), []);
% figure(); figure();
% imshow(clusters); imshow(clusters);
% Clustering into four clusters
clusters = myNCuts(graph2, 4); clusters = myNCuts(graph2, 4);
% Presents results
figure(); figure();
imshow(meanClustersColorRGB(d2b, reshape(clusters, size(d2b, 1), []))); imshow(meanClustersColorRGB(d2b, reshape(clusters, size(d2b, 1), [])));
% clusters = clusters ./ 4; clusters = clusters ./ 4;
% clusters = reshape(clusters, size(d2a, 1), []); clusters = reshape(clusters, size(d2a, 1), []);
% figure(); figure();
% imshow(clusters); imshow(clusters);

3
Assignment_2/demo3b.m

@ -1,3 +1,4 @@
%% Initialization
clear clear
clear all clear all
@ -17,6 +18,7 @@ imshow(d2a);
clusters = myNCuts(graph1, 2); clusters = myNCuts(graph1, 2);
NCut1 = calculateNcut(graph1, clusters); NCut1 = calculateNcut(graph1, clusters);
% Presents results
clusters = clusters - 1; clusters = clusters - 1;
clusters = reshape(clusters, size(d2a, 1), []); clusters = reshape(clusters, size(d2a, 1), []);
figure(); figure();
@ -29,6 +31,7 @@ imshow(d2b);
clusters = myNCuts(graph2, 2); clusters = myNCuts(graph2, 2);
NCut2 = calculateNcut(graph2, clusters); NCut2 = calculateNcut(graph2, clusters);
% Presents results
figure(); figure();
imshow(meanClustersColorRGB(d2b, reshape(clusters, size(d2b, 1), []))); imshow(meanClustersColorRGB(d2b, reshape(clusters, size(d2b, 1), [])));

46
Assignment_2/demo3c.m

@ -1,3 +1,4 @@
%% Initialization
clear clear
clear all clear all
@ -15,19 +16,52 @@ figure();
imshow(d2a); imshow(d2a);
clusters = recursiveNCuts(graph1); clusters = recursiveNCuts(graph1);
clusters = clusters ./ 2;
% Presents results
clusters = reshape(clusters, size(d2a, 1), []); clusters = reshape(clusters, size(d2a, 1), []);
clustersR = clusters;
clustersG = clusters;
clustersB = clusters;
for cluster = 1:size(unique(clusters), 1)
clustersR(clusters == cluster) = rand;
clustersG(clusters == cluster) = rand;
clustersB(clusters == cluster) = rand;
end
clusters(:, :, 1) = clustersR;
clusters(:, :, 2) = clustersG;
clusters(:, :, 3) = clustersB;
figure(); figure();
imshow(clusters); imshow(clusters);
clearvars clustersR clustersG clustersB cluster
%% Executes recursive experiments for the second image %% Executes recursive experiments for the second image
figure(); figure();
imshow(d2b); imshow(d2b);
clusters = recursiveNCuts(graph2); clusters = recursiveNCuts(graph2);
% Presents results
clusters = reshape(clusters, size(d2b, 1), []);
figure(); figure();
imshow(meanClustersColorRGB(d2b, reshape(clusters, size(d2b, 1), []))); imshow(meanClustersColorRGB(d2b, clusters));
% clusters = clusters ./ 2;
% clusters = reshape(clusters, size(d2a, 1), []); clustersR = clusters;
% figure(); clustersG = clusters;
% imshow(clusters); clustersB = clusters;
for cluster = 1:size(unique(clusters), 1)
clustersR(clusters == cluster) = rand;
clustersG(clusters == cluster) = rand;
clustersB(clusters == cluster) = rand;
end
clusters(:, :, 1) = clustersR;
clusters(:, :, 2) = clustersG;
clusters(:, :, 3) = clustersB;
figure();
imshow(clusters);
clearvars clustersR clustersG clustersB cluster

35
Assignment_2/meanClustersColorRGB.m

@ -1,28 +1,37 @@
function segIm = meanClustersColorRGB(image, clusters) function coloredIm = meanClustersColorRGB(image, clusters)
%MEANCLUSTERSCOLOR Summary of this function goes here %meanClustersColorRGB assigns the mean color of each cluster to the cluster
% Detailed explanation goes here % Usage coloredIm = meanClustersColorRGB (image, clusters), where:
% Inputs
% - image is the original image
% - clusters is a matrix of the same dimensions as the image that
% holds the cluster ID of each pixel
% Output
% - coloredIm is the image produced by assigning the mean color of
% each cluster to the cluster's pixels
% Initializes helper variables
redChannel = image(:, :, 1); redChannel = image(:, :, 1);
greenChannel = image(:, :, 2); greenChannel = image(:, :, 2);
blueChannel = image(:, :, 3); blueChannel = image(:, :, 3);
segImR = clusters; coloredImR = clusters;
segImG = clusters; coloredImG = clusters;
segImB = clusters; coloredImB = clusters;
% Assigns the mean color to each cluster
for cluster = 1:max(max(clusters)) for cluster = 1:max(max(clusters))
meanR = mean(redChannel(clusters == cluster)); meanR = mean(redChannel(clusters == cluster));
meanG = mean(greenChannel(clusters == cluster)); meanG = mean(greenChannel(clusters == cluster));
meanB = mean(blueChannel(clusters == cluster)); meanB = mean(blueChannel(clusters == cluster));
segImR(clusters == cluster) = meanR; coloredImR(clusters == cluster) = meanR;
segImG(clusters == cluster) = meanG; coloredImG(clusters == cluster) = meanG;
segImB(clusters == cluster) = meanB; coloredImB(clusters == cluster) = meanB;
end end
segIm = zeros(size(image, 1), size(image, 2), 3); coloredIm = zeros(size(image, 1), size(image, 2), 3);
segIm(:, :, 1) = segImR; coloredIm(:, :, 1) = coloredImR;
segIm(:, :, 2) = segImG; coloredIm(:, :, 2) = coloredImG;
segIm(:, :, 3) = segImB; coloredIm(:, :, 3) = coloredImB;
end end

13
Assignment_2/myNCuts.m

@ -2,18 +2,25 @@ function clusterIdx = myNCuts (anAffinityMat, k)
%Implementation of the N-cuts algorithm %Implementation of the N-cuts algorithm
% Usage clusterIdx = myNCuts (anAffinityMat, k), where: % Usage clusterIdx = myNCuts (anAffinityMat, k), where:
% Inputs % Inputs
% - anAffinityMat is a rectagular, symmetrical affinity matrix % - anAffinityMat is a rectangular, symmetrical affinity matrix
% representation of an image % representation of an image
% - k is the desired number of clusters % - k is the desired number of clusters
% Output % Output
% - clusterIdx is a vector storing the cluster Id of each node % - clusterIdx is a vector storing the cluster Id of each node
% Makes sure preconditions are met
if ~issymmetric(anAffinityMat) if ~issymmetric(anAffinityMat)
error('The affinity matrix provided is not symmetric.'); error('The affinity matrix provided is not symmetric.');
end end
if k < 2
error('The number of clusters must be greater than two.');
end
% Calculates the eigenvectors
D = diag(sum(anAffinityMat, 2)); D = diag(sum(anAffinityMat, 2));
L = D - anAffinityMat; [eigenvectorsMatrix, ~] = eigs(double(D - anAffinityMat), double(D), ...
[eigenvectorsMatrix, ~] = eigs(double(L), double(D), k, 'sm'); k, 'sm');
% Does the clustering using K-Means
clusterIdx = kmeans(eigenvectorsMatrix, k); clusterIdx = kmeans(eigenvectorsMatrix, k);
end end

13
Assignment_2/mySpectralClustering.m

@ -2,17 +2,24 @@ function clusterIdx = mySpectralClustering (anAffinityMat, k)
%Implementation of spectral clustering %Implementation of spectral clustering
% Usage clusterIdx = mySpectralClustering (anAffinityMat, k), where: % Usage clusterIdx = mySpectralClustering (anAffinityMat, k), where:
% Inputs % Inputs
% - anAffinityMat is a rectagular, symmetrical affinity matrix % - anAffinityMat is a rectangular, symmetrical affinity matrix
% representation of an image % representation of an image
% - k is the desired number of clusters % - k is the desired number of clusters
% Output % Output
% - clusterIdx is a vector storing the cluster Id of each node % - clusterIdx is a vector storing the cluster Id of each node
% Makes sure preconditions are met
if ~issymmetric(anAffinityMat) if ~issymmetric(anAffinityMat)
error('The affinity matrix provided is not symmetric.'); error('The affinity matrix provided is not symmetric.');
end end
if k < 2
error('The number of clusters must be greater than two.');
end
% Calculates the eigenvectors
[eigenvectorsMatrix, ~] = eigs(double( ...
diag(sum(anAffinityMat, 2)) - anAffinityMat), k, 'sm');
L = diag(sum(anAffinityMat, 2)) - anAffinityMat; % Does the clustering using K-Means
[eigenvectorsMatrix, ~] = eigs(double(L), k, 'sm');
clusterIdx = kmeans(eigenvectorsMatrix, k); clusterIdx = kmeans(eigenvectorsMatrix, k);
end end

43
Assignment_2/recursiveNCuts.m

@ -1,26 +1,43 @@
function clusters = recursiveNCuts(graph) function clusterIdx = recursiveNCuts(anAffinityMat)
%RECURSIVENCUTS Summary of this function goes here %Implementation of the recursive N-cuts algorithm
% Detailed explanation goes here % 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
clusters = myNCuts(graph, 2); % 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);
if (nnz(clusters == 1) < 5 || nnz(clusters == 2) < 5) % Checks stop conditions
if (nnz(clusterIdx == 1) < 5 || nnz(clusterIdx == 2) < 5)
return; return;
end end
NCut = calculateNcut(anAffinityMat, clusterIdx);
NCut = calculateNcut(graph, clusters);
if (NCut > 0.85) if (NCut > 0.85)
return; return;
end end
clusterOneIndices = (clusters == 1); % Finds indices of each cluster
clusterTwoIndices = (clusters == 2); clusterOneIndices = (clusterIdx == 1);
clusterTwoIndices = (clusterIdx == 2);
firstSubClusters = recursiveNCuts(graph(clusterOneIndices, clusterOneIndices')); % Recursively calls itself for each part of the matrix
secondSubClusters = recursiveNCuts(graph(clusterTwoIndices, clusterTwoIndices')); 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); secondSubClusters = secondSubClusters + size(unique(firstSubClusters), 1);
clusters(clusterOneIndices) = firstSubClusters; % Re-merges the sub-cluster arrays
clusters(clusterTwoIndices) = secondSubClusters; clusterIdx(clusterOneIndices) = firstSubClusters;
clusterIdx(clusterTwoIndices) = secondSubClusters;
end end

Loading…
Cancel
Save