MATLAB: How to pass image to function after k-means

digital image processingimage processingImage Processing ToolboxMATLABStatistics and Machine Learning Toolboxwatershed

hi folks,
I want to perform a watershed transform on an image after performing a k-means clustering, below is my code.
img = imread('myimage.jpg');
lab_img = rgb2lab(img);
ab = lab_img(:,:,2:3);
a = lab_img(:,:,2);
b = lab_img(:,:,3);
ab = im2single(ab);
nColours = 2;
k = 20;
pixelLabels = imsegkmeans(ab, nColours, "NumAttempts", k);
imshow(pixelLabels,[])
title('Image Labeled by Cluster Index');
parfor i = 1 : k
maskID = strcat('mask', i);
clusterID = strcat('cluster', i);
maskID = pixelLabels == i
clusterID = img.*uint8(maskID);
figure(i), imshow(clusterID)
title(['Objects in Cluster ', num2str(i)]);
end
My question is: how can I pass the result of the k-means to the watershed function? I tried to pass "clusterID" and "cluster20" but it returned an error.
Any help would be appreciated, thanks in advance!

Best Answer

There is a watershed() function. Use it.
Example of watershed: Image Processing Blog
Related Question