【问题标题】:Displaying kmean result with specific colors to specific clusters以特定颜色向特定集群显示 kmean 结果
【发布时间】:2015-07-05 13:11:54
【问题描述】:

我使用以下 matlab 代码对预处理图像应用了 k-mean 聚类

%B - input image
C=rgb2gray(B);
[idx centroids]=kmeans(double(C(:)),4);
imseg = zeros(size(C,1),size(C,2));
for i=1:max(idx)
     imseg(idx==i)=i;
end
i=mat2gray(imseg);
% i - output image

每次显示输出时,分配给输出图像的颜色都会发生变化。 如何为 cluster1、cluster2、cluster3 和 cluster4 赋予特定颜色。

【问题讨论】:

  • 您可以在显示输出的行中执行此操作,遗憾的是您尚未将其添加到代码中,因此我们无能为力...

标签: matlab k-means


【解决方案1】:

您可以使用颜色图。让R1B1G1 成为您要在其中显示第一个集群的 RGB 值(范围 [0..1] 中的值),R2 是第二个集群的红色通道值,并且依此类推……那么你的颜色图是:

cmp = [R1 G1 B1;
       ...
       R4 G4 B4];

现在,

[idx centroids] = kmeans(double(C(:)),4);
imseg = reshape( idx, size(C) ); %// reshape
figure; imagesc( imseg );colormap( cmp ); %// that's it!

PS,
最好是not to use i as a variable name in Matlab

【讨论】:

    猜你喜欢
    • 2017-10-07
    • 1970-01-01
    • 1970-01-01
    • 2020-02-03
    • 2018-02-07
    • 1970-01-01
    • 2021-04-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多