Apostolos Fanakis
6 years ago
8 changed files with 297 additions and 0 deletions
@ -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 |
@ -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); |
@ -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); |
@ -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); |
@ -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); |
@ -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); |
@ -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 |
|||
|
@ -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 |
Loading…
Reference in new issue