From 3ab295318bddca29ca02fffd58d912901979061b Mon Sep 17 00:00:00 2001 From: Apostolof Date: Mon, 13 May 2019 21:26:46 +0300 Subject: [PATCH] Init calculateNcut, Init demos --- Assignment_2/calculateNcut.m | 25 ++++++++++++ Assignment_2/demo1.m | 25 ++++++++++++ Assignment_2/demo2.m | 61 +++++++++++++++++++++++++++++ Assignment_2/demo3a.m | 61 +++++++++++++++++++++++++++++ Assignment_2/demo3b.m | 38 ++++++++++++++++++ Assignment_2/demo3c.m | 33 ++++++++++++++++ Assignment_2/meanClustersColorRGB.m | 28 +++++++++++++ Assignment_2/recursiveNCuts.m | 26 ++++++++++++ 8 files changed, 297 insertions(+) create mode 100644 Assignment_2/calculateNcut.m create mode 100644 Assignment_2/demo1.m create mode 100644 Assignment_2/demo2.m create mode 100644 Assignment_2/demo3a.m create mode 100644 Assignment_2/demo3b.m create mode 100644 Assignment_2/demo3c.m create mode 100644 Assignment_2/meanClustersColorRGB.m create mode 100644 Assignment_2/recursiveNCuts.m diff --git a/Assignment_2/calculateNcut.m b/Assignment_2/calculateNcut.m new file mode 100644 index 0000000..1a88340 --- /dev/null +++ b/Assignment_2/calculateNcut.m @@ -0,0 +1,25 @@ +function nCutValue = calculateNcut (anAffinityMat , clusterIdx) +%Implementation the NCut metric calculation +% Usage nCutValue = calculateNcut (anAffinityMat , clusterIdx), where: +% Inputs +% - anAffinityMat is a rectagular, 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 should be two'); + end + + clusterOneIndices = (clusterIdx == clusterIds(1)); + clusterTwoIndices = (clusterIdx == clusterIds(2)); + + nCutValue = 2 - ... + (sum(sum(anAffinityMat(clusterOneIndices, clusterOneIndices'))) / ... + sum(sum(anAffinityMat(clusterOneIndices, :))) + ... + sum(sum(anAffinityMat(clusterTwoIndices, clusterTwoIndices'))) / ... + sum(sum(anAffinityMat(clusterTwoIndices, :)))); +end diff --git a/Assignment_2/demo1.m b/Assignment_2/demo1.m new file mode 100644 index 0000000..e31e291 --- /dev/null +++ b/Assignment_2/demo1.m @@ -0,0 +1,25 @@ +clear +clear all + +% For reproducibility +rng(1); + +load('dip_hw_2.mat'); + +clusters = mySpectralClustering(d1a, 2); +clusters = clusters ./ 2; +%clusters = reshape(clusters, 4, []); +figure(); +imshow(clusters); + +clusters = mySpectralClustering(d1a, 3); +clusters = clusters ./ 3; +%clusters = reshape(clusters, 4, []); +figure(); +imshow(clusters); + +clusters = mySpectralClustering(d1a, 4); +clusters = clusters ./ 4; +%clusters = reshape(clusters, 4, []); +figure(); +imshow(clusters); diff --git a/Assignment_2/demo2.m b/Assignment_2/demo2.m new file mode 100644 index 0000000..445153a --- /dev/null +++ b/Assignment_2/demo2.m @@ -0,0 +1,61 @@ +clear +clear all + +% For reproducibility +rng(1); + +load('dip_hw_2.mat'); + +%% Produces the affinity graphs for both images +graph1 = Image2Graph(d2a); +graph2 = Image2Graph(d2b); + +%% Executes experiments for the first image +figure(); +imshow(d2a); + +clusters = mySpectralClustering(graph1, 2); +clusters = clusters ./ 2; +clusters = reshape(clusters, size(d2a, 1), []); +figure(); +imshow(clusters); + +clusters = mySpectralClustering(graph1, 3); +clusters = clusters ./ 3; +clusters = reshape(clusters, size(d2a, 1), []); +figure(); +imshow(clusters); + +clusters = mySpectralClustering(graph1, 4); +clusters = clusters ./ 4; +clusters = reshape(clusters, size(d2a, 1), []); +figure(); +imshow(clusters); + +%% Executes experiments for the second image +figure(); +imshow(d2b); + +clusters = mySpectralClustering(graph2, 2); +figure(); +imshow(meanClustersColorRGB(d2b, reshape(clusters, size(d2b, 1), []))); +% clusters = clusters ./ 2; +% clusters = reshape(clusters, size(d2a, 1), []); +% figure(); +% imshow(clusters); + +clusters = mySpectralClustering(graph2, 3); +figure(); +imshow(meanClustersColorRGB(d2b, reshape(clusters, size(d2b, 1), []))); +% clusters = clusters ./ 3; +% clusters = reshape(clusters, size(d2a, 1), []); +% figure(); +% imshow(clusters); + +clusters = mySpectralClustering(graph2, 4); +figure(); +imshow(meanClustersColorRGB(d2b, reshape(clusters, size(d2b, 1), []))); +% clusters = clusters ./ 4; +% clusters = reshape(clusters, size(d2a, 1), []); +% figure(); +% imshow(clusters); diff --git a/Assignment_2/demo3a.m b/Assignment_2/demo3a.m new file mode 100644 index 0000000..0e2f9be --- /dev/null +++ b/Assignment_2/demo3a.m @@ -0,0 +1,61 @@ +clear +clear all + +% For reproducibility +rng(1); + +load('dip_hw_2.mat'); + +%% Produces the affinity graphs for both images +graph1 = Image2Graph(d2a); +graph2 = Image2Graph(d2b); + +%% Executes non recursive experiments for the first image +figure(); +imshow(d2a); + +clusters = myNCuts(graph1, 2); +clusters = clusters ./ 2; +clusters = reshape(clusters, size(d2a, 1), []); +figure(); +imshow(clusters); + +clusters = myNCuts(graph1, 3); +clusters = clusters ./ 3; +clusters = reshape(clusters, size(d2a, 1), []); +figure(); +imshow(clusters); + +clusters = myNCuts(graph1, 4); +clusters = clusters ./ 4; +clusters = reshape(clusters, size(d2a, 1), []); +figure(); +imshow(clusters); + +%% Executes non recursive experiments for the second image +figure(); +imshow(d2b); + +clusters = myNCuts(graph2, 2); +figure(); +imshow(meanClustersColorRGB(d2b, reshape(clusters, size(d2b, 1), []))); +% clusters = clusters ./ 2; +% clusters = reshape(clusters, size(d2a, 1), []); +% figure(); +% imshow(clusters); + +clusters = myNCuts(graph2, 3); +figure(); +imshow(meanClustersColorRGB(d2b, reshape(clusters, size(d2b, 1), []))); +% clusters = clusters ./ 3; +% clusters = reshape(clusters, size(d2a, 1), []); +% figure(); +% imshow(clusters); + +clusters = myNCuts(graph2, 4); +figure(); +imshow(meanClustersColorRGB(d2b, reshape(clusters, size(d2b, 1), []))); +% clusters = clusters ./ 4; +% clusters = reshape(clusters, size(d2a, 1), []); +% figure(); +% imshow(clusters); diff --git a/Assignment_2/demo3b.m b/Assignment_2/demo3b.m new file mode 100644 index 0000000..11da403 --- /dev/null +++ b/Assignment_2/demo3b.m @@ -0,0 +1,38 @@ +clear +clear all + +% For reproducibility +rng(1); + +load('dip_hw_2.mat'); + +%% Produces the affinity graphs for both images +graph1 = Image2Graph(d2a); +graph2 = Image2Graph(d2b); + +%% Executes recursive experiments for the first image +figure(); +imshow(d2a); + +clusters = myNCuts(graph1, 2); +NCut1 = calculateNcut(graph1, clusters); + +clusters = clusters - 1; +clusters = reshape(clusters, size(d2a, 1), []); +figure(); +imshow(clusters); + +%% Executes recursive experiments for the second image +figure(); +imshow(d2b); + +clusters = myNCuts(graph2, 2); +NCut2 = calculateNcut(graph2, clusters); + +figure(); +imshow(meanClustersColorRGB(d2b, reshape(clusters, size(d2b, 1), []))); + +clusters = clusters - 1; +clusters = reshape(clusters, size(d2a, 1), []); +figure(); +imshow(clusters); diff --git a/Assignment_2/demo3c.m b/Assignment_2/demo3c.m new file mode 100644 index 0000000..db02198 --- /dev/null +++ b/Assignment_2/demo3c.m @@ -0,0 +1,33 @@ +clear +clear all + +% For reproducibility +rng(1); + +load('dip_hw_2.mat'); + +%% Produces the affinity graphs for both images +graph1 = Image2Graph(d2a); +graph2 = Image2Graph(d2b); + +%% Executes recursive experiments for the first image +figure(); +imshow(d2a); + +clusters = recursiveNCuts(graph1); +clusters = clusters ./ 2; +clusters = reshape(clusters, size(d2a, 1), []); +figure(); +imshow(clusters); + +%% Executes recursive experiments for the second image +figure(); +imshow(d2b); + +clusters = recursiveNCuts(graph2); +figure(); +imshow(meanClustersColorRGB(d2b, reshape(clusters, size(d2b, 1), []))); +% clusters = clusters ./ 2; +% clusters = reshape(clusters, size(d2a, 1), []); +% figure(); +% imshow(clusters); diff --git a/Assignment_2/meanClustersColorRGB.m b/Assignment_2/meanClustersColorRGB.m new file mode 100644 index 0000000..e46d055 --- /dev/null +++ b/Assignment_2/meanClustersColorRGB.m @@ -0,0 +1,28 @@ +function segIm = meanClustersColorRGB(image, clusters) +%MEANCLUSTERSCOLOR Summary of this function goes here +% Detailed explanation goes here + + redChannel = image(:, :, 1); + greenChannel = image(:, :, 2); + blueChannel = image(:, :, 3); + + segImR = clusters; + segImG = clusters; + segImB = clusters; + + for cluster = 1:max(max(clusters)) + 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; +end + diff --git a/Assignment_2/recursiveNCuts.m b/Assignment_2/recursiveNCuts.m new file mode 100644 index 0000000..6d78641 --- /dev/null +++ b/Assignment_2/recursiveNCuts.m @@ -0,0 +1,26 @@ +function clusters = recursiveNCuts(graph) +%RECURSIVENCUTS Summary of this function goes here +% Detailed explanation goes here + + clusters = myNCuts(graph, 2); + + if (nnz(clusters == 1) < 5 || nnz(clusters == 2) < 5) + return; + end + + NCut = calculateNcut(graph, clusters); + if (NCut > 0.85) + return; + end + + clusterOneIndices = (clusters == 1); + clusterTwoIndices = (clusters == 2); + + firstSubClusters = recursiveNCuts(graph(clusterOneIndices, clusterOneIndices')); + secondSubClusters = recursiveNCuts(graph(clusterTwoIndices, clusterTwoIndices')); + + secondSubClusters = secondSubClusters + size(unique(firstSubClusters), 1); + + clusters(clusterOneIndices) = firstSubClusters; + clusters(clusterTwoIndices) = secondSubClusters; +end