how to pass image to function after k-means?

4 ビュー (過去 30 日間)
Teshan Rezel
Teshan Rezel 2020 年 1 月 28 日
コメント済み: Teshan Rezel 2020 年 1 月 29 日
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!
  3 件のコメント
Teshan Rezel
Teshan Rezel 2020 年 1 月 28 日
@Adam, "cluster20" is the value of clusterID after the final iteration of the for loop, which is set to 20.
The error I get is that it is not a recognised variable.
Adam
Adam 2020 年 1 月 28 日
Well, it's a parfor loop so the final iteration is not guaranteed to be k = 20.
I can't remember off the top of my head, but if you declare clusterID before the parfoor loop then it may still exist after it, though I doubt in a satisfactory way if it does, as there is no guarantee which result will complete last and I suspect you would get a warning or error on the variable.
Simplest option would be to store the results in a 3d array so you have all of them after the parfor loop.

サインインしてコメントする。

採用された回答

Image Analyst
Image Analyst 2020 年 1 月 29 日
There is a watershed() function. Use it.
Example of watershed: Image Processing Blog
  1 件のコメント
Teshan Rezel
Teshan Rezel 2020 年 1 月 29 日
I've tried, but as my question stated, the problem was less about the watershed function itself and more about passing an argument to it. Thanks for your response though

サインインしてコメントする。

その他の回答 (0 件)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by