Apostolos Fanakis
6 years ago
4 changed files with 63 additions and 20 deletions
@ -0,0 +1,19 @@ |
|||
function clusterIdx = myNCuts (anAffinityMat, k) |
|||
%Implementation of the N-cuts algorithm |
|||
% Usage clusterIdx = myNCuts (anAffinityMat, k), where: |
|||
% Inputs |
|||
% - anAffinityMat is a rectagular, symmetrical affinity matrix |
|||
% representation of an image |
|||
% - k is the desired number of clusters |
|||
% Output |
|||
% - clusterIdx is a vector storing the cluster Id of each node |
|||
|
|||
if ~issymmetric(anAffinityMat) |
|||
error('The affinity matrix provided is not symmetric.'); |
|||
end |
|||
|
|||
D = diag(sum(anAffinityMat, 2)); |
|||
L = D - anAffinityMat; |
|||
[eigenvectorsMatrix, ~] = eigs(double(L), double(D), k, 'sm'); |
|||
clusterIdx = kmeans(eigenvectorsMatrix, k); |
|||
end |
@ -0,0 +1,35 @@ |
|||
image = imageT; |
|||
k = 3; |
|||
|
|||
graph = Image2Graph(image); |
|||
%clusters = mySpectralClustering(graph, k); |
|||
clusters = myNCuts(graph, k); |
|||
|
|||
clusters = reshape(clusters, size(image, 1), []); |
|||
|
|||
redChannel = image(:, :, 1); |
|||
greenChannel = image(:, :, 2); |
|||
blueChannel = image(:, :, 3); |
|||
|
|||
segImR = clusters; |
|||
segImG = clusters; |
|||
segImB = clusters; |
|||
|
|||
for cluster = 1:k |
|||
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; |
|||
imshow(segIm) |
|||
|
|||
clearvars segImR segImG segImB meanR meanG meanB graph redChannel ... |
|||
greenChannel blueChannel clusters k image cluster segIm |
Loading…
Reference in new issue