MATLAB: Error when trying to create a loop for k-means

image processingimage segmentationkmeansMATLAB

Hi all,
I'm trying to loop for a given value of k for a k-means segmentation. The following is the documentation code:
mask1 = pixelLabels==1;
cluster1 = img .*uint8(mask1);
imshow(cluster1)
title ('Objects in Cluster 1');
mask2 = pixelLabels==2;
cluster2 = img .* uint8(mask2);
imshow(cluster2)
title('Objects in Cluster 2');
mask3 = pixelLabels==3;
cluster3 = img .* uint8(mask3);
imshow(cluster3)
title('Objects in Cluster 3');
And this is my attempt at turning it into a loop:
imshow(pixelLabels,[])
title('Image Labeled by Cluster Index');
for i = 1 : k
maskID = strcat('mask', k);
clusterID = strcat('cluster', k);
maskID = pixelLabels == k
clusterID = img.*uint8(maskID);
imshow(clusterID)
title('Objects in Cluster ', num2str(clusterID));
end
I not only get an error (incorrect number of arguments) but I also get an incorrect output (only 1 black image).
Any help would be much appreciated!

Best Answer

%Defined
img=
pixelLabels=
k=
for i = 1 : k
maskID = strcat('mask', 1);
clusterID = strcat('cluster', 1);
maskID = pixelLabels == k
clusterID = img.*uint8(maskID);
imshow(clusterID)
title(['Objects in Cluster ',num2str(i)]);
end
Related Question