clustering image, display output
1 view (last 30 days)
Show older comments
I have function
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/158587/image.bmp)
clc,clear all
[I,map]=imread('test','bmp');
I = ~I;
imshow(I,map);
[m n]=size(I);
P = [];
for i=1:m
for j=1:n
if I(i,j)==1
P = [P ; i j];
end
end
end
size(P);
MON=P;
k = 3; % zadanie k
TAZ=MON(1:k,:); % 3 centroids
%%2 krok - pomocne premenne
[m,n]=size(MON); %rozmery MON
itr=1; %aktualna iteracia
E = inf;% kvalita poslednej iter
Enew = 0;
PEO=0;
PEO1=0;
while any(E~=Enew) && PEO>=PEO1
Enew=E;
PEO1=PEO;
%%4 krok - priradenie bodov do zhlukov podla tazisk
idx=1;
[r,s]=size(TAZ); %rozmery TAZ
ED=[];%euklidovska vzdialenost medzi premennou B a typickym bodom
Z=cell(1,k); %vytvorenie zhlukov
z = 1;
for i=1:m % pre pocet objektov (pre kazdy riadok MON)
B = MON(i,:); % vloz i-ty riadok z MON do premennej B
for j=1:r % pre kazdy riadok TAZ
ED(j)= euklid6(TAZ(j,:),B); %euklidovska vzdialenost
[minD, minI(idx)] = min([ED]); %minimalna hodnota a index
end
[e,f]=size(ED);
for h=1:f
if (ED(h) == ED(minI(idx)))
Z{h}{z}=B;
z=z+1;
end
end
idx=idx+1;
end
dZ=length(Z); % dlzka Z
%odstranenie prazdnych miest
for i=1:size(Z,2)
for j=length(Z{i})
if j~=0
Z{i}(cellfun(@isempty,Z{i})) = [];
else continue;
end
end
end
Z(cellfun('isempty',Z))=[];%odstranenie prazdnych miest
k=length(Z);
minI';
%new centroid
TAZ_new=[];
for j=1:k
TAZ_new(j,:)=tazisko6(Z(j));%vypocet noveho taziska
end
TAZ=TAZ_new;
E=sum(E);
TAZ=TAZ_new;
k=length(Z);
itr = itr+1;
PEO=E;
disp(Z);
end
disp('Koniec')
%% k means clustering
function [d] = tazisko6(A)
%%[d] = tazisko6(A)
%
% Tazisko jednotlivych zhlukov(centroid)
d1=length(A);
d2 =length(A{1});
s=0;
for i=1:d1
for j=1:d2
s=s+(A{i}{j});
end
if d2~=1
d=s/d2;
else
d=s;
end
end
function [ d] = euklid6( A,B )
%%[d] = euklid6(A,B)
%
% Euklidovska vzdialenost bodov
[hA,tA]=size(A);
[hB,wB]=size(B);
z=1;
if hA==1 & hB==1
for i=1:hA
d(i)=sqrt(sum((A(i,:) - B(i,:)).^2));
end
else
if hA==1 & hB==1 & tA==1 & wB==1
for i=1:hA
for j=1:hB
d(z)=sqrt(sum((A(i,:) - B(j,:)).^2));
z=z+1;
end
end
else
for i=1:hA
for j=1:hB
d(z)=sqrt(sum((A(i,:) - B(j,:)).^2));
z=z+1;
end
end
end
end
end
My output is Z={1x1 cell} {1x5 cell} {1x4 cell}.
as I show the individual cell in image?
Z{1}= first cluster Z{2}= second cluster Z{3}= third cluster
each cluster must be different colors.
Thanks.
0 Comments
Answers (0)
See Also
Categories
Find more on Statistics and Machine Learning Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!