【问题标题】:MATLAB: draw centroidsMATLAB:绘制质心
【发布时间】:2011-02-08 15:09:00
【问题描述】:

我的主要问题是一个特征质心,我如何在 MATLAB 中绘制它?

更详细地说,我有一个NxNx3 图像(RGB 图像),我使用4x4 块并为每个块计算6 维特征向量。我将这些特征向量存储在一个Mx6 矩阵中,在该矩阵上运行kmeans 函数并在kx6 矩阵中获得质心,其中k 是集群的数量,6 是每个特征的数量块。

如何在我的图像中绘制这些中心簇,以便可视化算法是否按照我希望的方式执行?或者,如果有人对我如何可视化图像上的质心有任何其他方式/建议,我将不胜感激。

【问题讨论】:

    标签: matlab cluster-analysis data-visualization k-means image-segmentation


    【解决方案1】:

    这是可视化集群的一种方法:

    正如您所描述的,首先我提取块,计算每个块的特征向量,然后对这个特征矩阵进行聚类。

    接下来我们可以可视化分配给每个块的集群。请注意,我假设 4x4 块是不同的,这很重要,以便我们可以将块映射到它们在原始图像中的位置。

    最后,为了在图像上显示聚类质心,我只需找到离每个聚类最近的块并将其显示为该聚类的代表。

    这是展示上述想法的完整示例(在您的情况下,您可能希望用您自己的实现替换计算每个块的特征的函数;我只是采用 min/max/mean/median/Q1 /Q3 作为每个 4x4 块的特征向量):

    %# params
    NUM_CLUSTERS = 3;
    BLOCK_SIZE = 4;
    featureFunc = @(X) [min(X); max(X); mean(X); prctile(X, [25 50 75])];
    
    %# read image
    I = imread('peppers.png');
    I = double( rgb2gray(I) );
    
    %# extract blocks as column
    J = im2col(I, [BLOCK_SIZE BLOCK_SIZE], 'distinct');  %# 16-by-NumBlocks
    
    %# compute features for each block
    JJ = featureFunc(J)';                                %'# NumBlocks-by-6
    
    %# cluster blocks according to the features extracted
    [clustIDX, ~, ~, Dist] = kmeans(JJ, NUM_CLUSTERS);
    
    %# display the cluster index assigned for each block as an image
    cc = reshape(clustIDX, ceil(size(I)/BLOCK_SIZE));
    RGB = label2rgb(cc);
    imshow(RGB), hold on
    
    %# find and display the closest block to each cluster
    [~,idx] = min(Dist);
    [r c] = ind2sub(ceil(size(I)/BLOCK_SIZE), idx);
    for i=1:NUM_CLUSTERS
        text(c(i)+2, r(i), num2str(i), 'fontsize',20)
    end
    plot(c, r, 'k.', 'markersize',30)
    legend('Centroids')
    

    【讨论】:

      【解决方案2】:

      质心不对应于图像中的坐标,而是对应于特征空间中的坐标。有两种方法可以测试 kmeans 的执行情况。对于这两种方式,您都希望首先将这些点与其最近的集群相关联。您可以从 kmeans 的第一个输出中获取此信息。

      (1)您可以通过将6维空间缩小为2或3维空间,然后用不同颜色绘制不同分类的坐标来可视化聚类结果。

      假设特征向量收集在一个名为featureArray 的数组中,并且您要求nClusters 集群,您将使用mdscale 将数据转换为3D 空间,如下所示:

      %# kmeans clustering
      [idx,centroids6D] = kmeans(featureArray,nClusters);
      %# find the dissimilarity between features in the array for mdscale.
      %# Add the cluster centroids to the points, so that they get transformed by mdscale as well.
      %# I assume that you use Euclidean distance. 
      dissimilarities = pdist([featureArray;centroids6D]);
      %# transform onto 3D space
      transformedCoords = mdscale(dissimilarities,3);
      %# create colormap with nClusters colors
      cmap = hsv(nClusters);
      %# loop to plot
      figure
      hold on,
      for c = 1:nClusters
          %# plot the coordinates
          currentIdx = find(idx==c);
          plot3(transformedCoords(currentIdx,1),transformedCoords(currentIdx,2),...
              transformedCoords(currentIdx,3),'.','Color',cmap(c,:));
          %# plot the cluster centroid with a black-edged square
          plot3(transformedCoords(1:end-nClusters+c,1),transformedCoords(1:end-nClusters+c,2),...
              transformedCoords(1:end-nClusters+c,3),'s','MarkerFaceColor',cmap(c,:),...
              MarkerEdgeColor','k');
      end
      

      (2) 或者,您可以创建一个伪彩色图像,显示图像的哪一部分属于哪个集群

      假设你有nRows by nCols 块,你写

      %# kmeans clustering
      [idx,centroids6D] = kmeans(featureArray,nClusters);
      %# create image
      img = reshape(idx,nRows,nCols);
      %# create colormap
      cmap = hsv(nClusters);
      
      %# show the image and color according to clusters
      figure
      imshow(img,[])
      colormap(cmap)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-27
        • 1970-01-01
        • 2017-04-13
        • 2018-05-14
        • 2014-10-29
        • 2011-12-16
        相关资源
        最近更新 更多