display output k-means clustering, display output clustering as a image

Hello,
I have a image, name image :test 3
I,map]=imread('test3','bmp');
I = ~I;
imshow(I,map);
[m n]=size(I)
P = [];
for i=1:m
for j=1:n
if I(i,j)==1
P = [P ; i j];
end
end
end
size(P)
MON=P;
[IDX,ctrs] = kmeans(MON,3)
as I plot the clusters in the image, resulting
I want to draw idx and ctrs in the image.
I don't know, How do I get back image with 3 new cluster(each cluster, different color in the image)
can anyone help ?
Thanks.

3 件のコメント

Salaheddin Hosseinzadeh
Salaheddin Hosseinzadeh 2014 年 3 月 13 日
Image Analyst should be able to help!
Image Analyst
Image Analyst 2014 年 3 月 14 日
Sorry, I don't have the stats toolbox, which is what has kmeans.
Tomas
Tomas 2014 年 3 月 14 日
I just want to know, how to convert ouput save in cell, back to image. Thanks.

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

 採用された回答

Dishant Arora
Dishant Arora 2014 年 3 月 13 日
編集済み: Dishant Arora 2014 年 3 月 13 日
[ I map] = imread('test3.bmp');
I = ~I;
imshow(I,map);
[m n]=size(I)
P = [];
for i=1:m
for j=1:n
if I(i,j)==1
P = [P ; i j];
end
end
end
size(P)
MON=P;
[IDX,ctrs] = kmeans(MON,3);
clusterImage = zeros(size(I));
clusteredImage(sub2ind(size(I) , P(:,1) , P(:,2)))=IDX;
imshow(label2rgb(clusteredImage))

9 件のコメント

Tomas
Tomas 2014 年 3 月 13 日
Warning: Image is too big to fit on screen; displaying at 0% > In imuitools\private\initSize at 73 In imshow at 262
does not display the image,
Dishant Arora
Dishant Arora 2014 年 3 月 14 日
Just some typing error, It should be:
clusteredImage = zeros(size(I))
Tomas
Tomas 2014 年 3 月 14 日
Ok, thanks, I would like to ask, when i have coordinates the image save in cell, how i display cluster in the image.
I enclose my matlab code.
MAIN FEATURES name metodakkk.
Thank you very much
Dishant Arora
Dishant Arora 2014 年 3 月 14 日
You want to show individual clusters in separate images?? if yes:
Z = cellfun(@(Z) Z', Z,'Un',0);
Z = cellfun(@(Z) cell2mat(Z), Z,'Un',0);
Z = cellfun(@(Z) sub2ind(size(I) , Z(:,1) , Z(:,2)), Z,'Un',0);
for ii = 1:length(Z)
figure
cluster{ii} = zeros(size(I));
cluster{ii}(Z{ii}) = 1;
imshow(cluster{ii})
end
Tomas
Tomas 2014 年 3 月 14 日
I want show all cluster in one image.
Thanks.
Dishant Arora
Dishant Arora 2014 年 3 月 14 日
Z = cellfun(@(Z) Z', Z,'Un',0);
Z = cellfun(@(Z) cell2mat(Z), Z,'Un',0);
Z = cellfun(@(Z) sub2ind(size(I) , Z(:,1) , Z(:,2)), Z,'Un',0);
figure
cluster = zeros(size(I));
for ii = 1:length(Z)
cluster(Z{ii}) = ii;
end
imshow((cluster))
Tomas
Tomas 2014 年 3 月 14 日
one more question as a color different clusters ?
Thank you very much for your help
Dishant Arora
Dishant Arora 2014 年 3 月 15 日
編集済み: Dishant Arora 2014 年 3 月 15 日
imshow(label2rgb(cluster))
Tomas
Tomas 2014 年 3 月 15 日
Thank you very much for your help

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

その他の回答 (2 件)

rizwan
rizwan 2015 年 3 月 16 日
Hi Experts, I am using the following code to find clusters in my image using K - Mean [ I map] = imread('D:\MS\Research\Classification Model\Research Implementation\EnhancedImage\ROIImage.jpeg'); I = ~I; imshow(I,map); [m n]=size(I) P = []; for i=1:m for j=1:n if I(i,j)==1 P = [P ; i j]; end end end size(P) MON=P; [IDX,ctrs] = kmeans(MON,3,'display', 'iter','MaxIter',500); clusterImage = zeros(size(I)); clusteredImage(sub2ind(size(I) , P(:,1) , P(:,2)))=IDX; imshow(label2rgb(clusteredImage))
The out put of the above code is
>> ImageEnhancement
m =
180
n =
317
ans =
20306 2
iter phase num sum
1 1 20306 9.40619e+07
2 1 2727 7.34318e+07
3 1 876 7.1216e+07
4 1 574 7.03212e+07
5 1 410 6.98473e+07
6 1 298 6.96024e+07
7 1 173 6.95038e+07
8 1 122 6.94633e+07
9 1 65 6.945e+07
10 1 45 6.9445e+07
11 1 30 6.9443e+07
12 1 15 6.94424e+07
13 1 8 6.94422e+07
14 1 3 6.94422e+07
15 1 1 6.94422e+07
16 2 0 6.94422e+07
Best total sum of distances = 6.94422e+07
Warning: Image is too big to fit on screen; displaying at 2%
Can any one explain this out put and how can i see proper out put of K- Mean???
I shall remain thank full You To
Regards
Yanyu Liang
Yanyu Liang 2016 年 11 月 30 日

0 投票

It shows how the kmeans is going at each iteration. "kmeans" implementation in matlab has two phases (you can think of it as two different approach to update assignment), so "phase" just tells if it is using first phase or second. "num" tells the number of points that change their assignment at that iteration (as you can see when it hits zero, the algorithm stops). "sum" is the objective value "kmeans" is trying to minimize.

カテゴリ

ヘルプ センター および File ExchangeImage Processing Toolbox についてさらに検索

質問済み:

2014 年 3 月 13 日

回答済み:

2016 年 11 月 30 日

Community Treasure Hunt

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

Start Hunting!

Translated by