フィルターのクリア

k means clustering shows only blank image if i loop it k times

1 回表示 (過去 30 日間)
Anirudh Kochhar
Anirudh Kochhar 2021 年 10 月 22 日
回答済み: Image Analyst 2021 年 11 月 8 日
If i loop for i = 1:2 i get clustering otherwise i just get a blank image. any idea why?
im = imread("irobot.jpg");
im_as_col = double(im(:));
cluster_membs = kmeans(im_as_col, 3)
cluster_membs = 6912000×1
2 2 2 2 2 2 2 2 2 2
labelim = zeros(size(im));
for i=1:3
inds = find(cluster_membs==i);
meanval = mean(im_as_col(inds));
labelim(inds) = meanval;
end
imshow(im)
imshow(labelim);
  2 件のコメント
KSSV
KSSV 2021 年 10 月 22 日
Error says there is no image in the path, did you specify the correct path of the image or is image present in the present working directory?
Anirudh Kochhar
Anirudh Kochhar 2021 年 10 月 22 日
hey sorry i forgot to add the image to the question. now that it is attached it should be easier to see. I dont get to see the k mean clustered image for some reason. it is a white image.

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

採用された回答

yanqi liu
yanqi liu 2021 年 11 月 5 日
clc; clear all; close all;
im = imread("https://ww2.mathworks.cn/matlabcentral/answers/uploaded_files/774998/irobot.jpg");
im_as_col = double(im(:));
cluster_membs = kmeans(im_as_col, 3);
labelim = zeros(size(im));
for i=1:3
inds = find(cluster_membs==i);
meanval = mean(im_as_col(inds));
labelim(inds) = meanval;
end
figure; imshow(im,[])
figure; imshow(mat2gray(labelim),[]);
  3 件のコメント
Image Analyst
Image Analyst 2021 年 11 月 8 日
@Anirudh Kochhar I'm not sure it does work. You wanted 3 classes and that's what I gave you in my answer below. This answer's final image looks like it shows 5 classes: blue, green, cyan, white, and gray classes, though kmeans does return 3. And the classes are not based on colors since all the gray levels are lumped together with this line:
im_as_col = double(im(:));
so you won't find unique/similar colors as in my answer. A color like pure red [1,0,0] would show up as the same class as pure green [0,1,0] or pure blue [0,0,1] because all the gray levels were put into a single column.
But I'm not sure why you wanted only 3 color classes since that image has blue, green, cyan, and a wide variety of grays. Why did you pick 3? This is not an image that has 3 clusters. I mean, just look at the gamut:
colorcloud(im);
Do you see 3 natural clusters there? No.
yanqi liu
yanqi liu 2021 年 11 月 8 日
yes,sir,its make 3 color class,and use label to display different gray level,that can be ref label2rgb

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

その他の回答 (2 件)

Image Analyst
Image Analyst 2021 年 10 月 22 日

Image Analyst
Image Analyst 2021 年 11 月 8 日
For what it's worth, attached is my Color Classifier that is based on Discrminant Analysis instead of kmeans. Basically you draw regions that are representative of the colors you want to have as your classes. Then it classifies all the pixels in the image into one of those classes.
Also attaching a KNN classifier demo.
Adapt as needed.

カテゴリ

Help Center および File ExchangeRecognition, Object Detection, and Semantic Segmentation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by