Apostolos Fanakis
6 years ago
11 changed files with 205 additions and 77 deletions
@ -1,28 +1,37 @@ |
|||
function segIm = meanClustersColorRGB(image, clusters) |
|||
%MEANCLUSTERSCOLOR Summary of this function goes here |
|||
% Detailed explanation goes here |
|||
function coloredIm = meanClustersColorRGB(image, clusters) |
|||
%meanClustersColorRGB assigns the mean color of each cluster to the cluster |
|||
% 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); |
|||
greenChannel = image(:, :, 2); |
|||
blueChannel = image(:, :, 3); |
|||
|
|||
segImR = clusters; |
|||
segImG = clusters; |
|||
segImB = clusters; |
|||
coloredImR = clusters; |
|||
coloredImG = clusters; |
|||
coloredImB = clusters; |
|||
|
|||
% Assigns the mean color to each cluster |
|||
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; |
|||
coloredImR(clusters == cluster) = meanR; |
|||
coloredImG(clusters == cluster) = meanG; |
|||
coloredImB(clusters == cluster) = meanB; |
|||
end |
|||
|
|||
segIm = zeros(size(image, 1), size(image, 2), 3); |
|||
segIm(:, :, 1) = segImR; |
|||
segIm(:, :, 2) = segImG; |
|||
segIm(:, :, 3) = segImB; |
|||
coloredIm = zeros(size(image, 1), size(image, 2), 3); |
|||
coloredIm(:, :, 1) = coloredImR; |
|||
coloredIm(:, :, 2) = coloredImG; |
|||
coloredIm(:, :, 3) = coloredImB; |
|||
end |
|||
|
|||
|
@ -1,26 +1,43 @@ |
|||
function clusters = recursiveNCuts(graph) |
|||
%RECURSIVENCUTS Summary of this function goes here |
|||
% Detailed explanation goes here |
|||
function clusterIdx = recursiveNCuts(anAffinityMat) |
|||
%Implementation of the recursive N-cuts algorithm |
|||
% 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; |
|||
end |
|||
|
|||
NCut = calculateNcut(graph, clusters); |
|||
NCut = calculateNcut(anAffinityMat, clusterIdx); |
|||
if (NCut > 0.85) |
|||
return; |
|||
end |
|||
|
|||
clusterOneIndices = (clusters == 1); |
|||
clusterTwoIndices = (clusters == 2); |
|||
% Finds indices of each cluster |
|||
clusterOneIndices = (clusterIdx == 1); |
|||
clusterTwoIndices = (clusterIdx == 2); |
|||
|
|||
firstSubClusters = recursiveNCuts(graph(clusterOneIndices, clusterOneIndices')); |
|||
secondSubClusters = recursiveNCuts(graph(clusterTwoIndices, clusterTwoIndices')); |
|||
% Recursively calls itself for each part of the matrix |
|||
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); |
|||
|
|||
clusters(clusterOneIndices) = firstSubClusters; |
|||
clusters(clusterTwoIndices) = secondSubClusters; |
|||
% Re-merges the sub-cluster arrays |
|||
clusterIdx(clusterOneIndices) = firstSubClusters; |
|||
clusterIdx(clusterTwoIndices) = secondSubClusters; |
|||
end |
|||
|
Loading…
Reference in new issue