Apostolos Fanakis
6 years ago
11 changed files with 205 additions and 77 deletions
@ -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 |
||||
|
|
||||
|
@ -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…
Reference in new issue