I have a code for K-means clustering help me correcting error
1 次查看(过去 30 天)
显示 更早的评论
%grayscale image segmentation using k-means algorithm
function kmeansegmentation
clc;close all; clear all;
[im,map]=imread('cameraman.tif');
im=im2double(im);
[row,col]=size(im);
%number of clusters
nc =4;
%Initial random cluster centroids
cs =rand(nc,1);
pcs=cs;
%number of Iteration
T= 50; t=0;
D=zeros(row,col,nc);
tsmld =[];
eps=1.e-5; cmx=1;
while ( t<T && cmx>eps )
%Distance between centroids and image's pixel
for c=1: nc
D(:,:,c)= (im - cs(c)).^2 ;
end
%assign members (image pixels) to minimum distance clusters
[mv,ML]=min(D,[],3);
%update cluster centroid
for c=1: nc
I = (ML==c);
cs(c) = mean( mean(im(I)) );
end
%find maximum absolute difference between current
% and previous iteration cluster centroids
cmx = max( abs(cs-pcs) );
pcs = cs;
t= t+1;
%sum difference between centroid and their members
% and store it for plotting energy minimization functions
tsmld= [tsmld; sum(mv(:)) ];
end
%assign a colour to each cluster
colors = hsv(nc);
sim= colors(ML,:);
sim=reshape(sim,row,col,3);
figure,subplot(1,2,1), imshow(im,map);
title('Input Image: cameraman');
subplot(1,2,2);imshow(sim,map);
title('segmented Image: cameraman');
figure;plot(tsmld,'*-b');
xlabel('Iteration');ylabel('Energy');
title('K-means energy minimization - cameraman.tif');
回答(1 个)
Sriram Tadavarty
2020-3-20
Hi Sandhiya,
I think i know what the issue is. You have written the code for grayscale image and you might be trying to provide the RGB image. Thus, leading to matrix dimensions error, as placed in your error tag. The following modifications to the code will help to resolve this. I included some code comments starting with '%%' as where the modifications are,
%grayscale image segmentation using k-means algorithm
function kmeansegmentation
clc;close all; clear all;
[im,map]=imread('ngc6543a.jpg');%imread('cameraman.tif');
im=im2double(im);
[row,col,d]=size(im); %% it could be 3D image (RGB) or grayscale (2D), So include the third output d
%number of clusters
nc =4;
%Initial random cluster centroids
cs =rand(nc,1);
pcs=cs;
%number of Iteration
T= 50; t=0;
D=zeros(row,col,d,nc); %% inclue 3rd dimension even here
tsmld =[];
eps=1.e-5; cmx=1;
while ( t<T && cmx>eps )
%Distance between centroids and image's pixel
for c=1: nc
D(:,:,:,c)= (im - cs(c)).^2 ; %% update with colon for third dimension too
end
%assign members (image pixels) to minimum distance clusters
[mv,ML]=min(D,[],4); %% update 4, as D is 4D
%update cluster centroid
for c=1: nc
I = (ML==c);
cs(c) = mean( mean(im(I)) );
end
%find maximum absolute difference between current
% and previous iteration cluster centroids
cmx = max( abs(cs-pcs) );
pcs = cs;
t= t+1;
%sum difference between centroid and their members
% and store it for plotting energy minimization functions
tsmld= [tsmld; sum(mv(:)) ];
end
%assign a colour to each cluster
colors = hsv(nc);
sim= colors(ML,:);
sim=reshape(sim,row,col,d,3);
%% Reshape sim to sim1, as imshow only accepts 3 column image
sim1 = reshape(sim,row,[],3);
figure,subplot(1,2,1), imshow(im,map);
title('Input Image: cameraman');
subplot(1,2,2);imshow(sim1,map);
title('segmented Image: cameraman');
figure;plot(tsmld,'*-b');
xlabel('Iteration');ylabel('Energy');
title('K-means energy minimization - cameraman.tif');
Hope this helps.
Regards,
Sriram
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Cluster Analysis and Anomaly Detection 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!