【问题标题】:Matlab finding the center of cluster of a few pixels and counting the clustersMatlab找到几个像素的簇中心并计算簇
【发布时间】:2025-12-08 20:35:02
【问题描述】:

所以我有这个矩阵 A,它由 1 和零组成,我有大约 10 到 14 个许多像素的白点,但我只想要每个白色簇的 1 个白色像素/中心坐标,我该如何计算有多少个簇及其中心。

尝试将矩阵 A 想象为从黑色天空开始的白色夜空,以及如何计算星星和星星中心,以及星星是由白色像素簇组成的。

集群的大小也不完全相同。

【问题讨论】:

  • 我在想如果我做某种类型的扫描,即每 5 x 5 像素,如果每个 5 x 5 框的总和大于阈值,它将是一个中心点,因此放置那里是 1,其他一切都为零
  • 示例图片会有所帮助。

标签: arrays matlab function image-processing matrix


【解决方案1】:

这是一些使用bwlabel 和/或regioprops 的代码,它们分别用于识别矩阵中的连通分量和许多其他属性。我认为它非常适合您的问题;但是,您可能想稍微修改一下我的代码,作为它的起点。

clear
clc

%// Create dummy matrix.
BW = logical ([ 1     1     1     0     1     1     1     0
                1     1     1     0     1     1     1     0
                1     1     1     0     1     1     1     0
                0     0     0     0     0     0     0     0
                0     0     0     0     0     1     1     0
                1     1     1     1     0     1     1     0
                1     1     1     1     0     1     1     0
                1     1     1     1     0     0     0     0]);
        
%// Identify clusters.
L = bwlabel(BW,4)

矩阵 L 如下所示:

L =

     1     1     1     0     3     3     3     0
     1     1     1     0     3     3     3     0
     1     1     1     0     3     3     3     0
     0     0     0     0     0     0     0     0
     0     0     0     0     0     4     4     0
     2     2     2     2     0     4     4     0
     2     2     2     2     0     4     4     0
     2     2     2     2     0     0     0     0

在这里,您有很多方法可以定位集群的中心。第一个使用 bwlabel 的输出来查找每个集群并循环计算坐标。它有效且具有指导意义,但它有点长而且效率不高。正如@nkjt 所提到的,第二种方法使用 regionprops ,它使用 'Centroid' 属性完全符合您的要求。所以这里有两种方法:

方法一:有点复杂 所以 bwlabel 确定了 4 个集群,这是有道理的。现在我们需要确定每个集群的中心。我的方法可能会简化;但是我有点没时间了,所以可以随意修改它。

%// Get number of clusters
NumClusters = numel(unique(L)) -1;

Centers = zeros(NumClusters,2);
CenterLinIdices = zeros(NumClusters,1);

for k = 1:NumClusters
    
%// Find indices for elements forming each cluster.
    [r, c] = find(L==k);
    
%// Sort the elements to know hot many rows and columns the cluster is spanning.
    [~,y] = sort(r);
    c = c(y);
    r = r(y);
    
    NumRow = numel(unique(r));
    NumCol = numel(unique(c));
    
%// Calculate the approximate center of the cluster.
    CenterCoord = [r(1)+floor(NumRow/2) c(1)+floor(NumCol/2)];
    
%// Actually this array is not used here but you might want to keep it for future reference.
    Centers(k,:) = [CenterCoord(1) CenterCoord(2)];
    
%// Convert the subscripts indices to linear indices for easy reference.
    CenterLinIdices(k) = sub2ind(size(BW),CenterCoord(1),CenterCoord(2));
end

%// Create output matrix full of 0s, except at the center of the clusters.
BW2 = false(size(BW));
BW2(CenterLinIdices) = 1

BW2 =

     0     0     0     0     0     0     0     0
     0     1     0     0     0     1     0     0
     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     1     0
     0     0     1     0     0     0     0     0
     0     0     0     0     0     0     0     0

方法 2 使用 regionprops 和 'Centroid' 属性。

获得矩阵 L 后,应用 regionprops 并连接输出以获取直接包含坐标的数组。简单多了!

%// Create dummy matrix.
BW = logical ([ 1     1     1     0     1     1     1     0
                1     1     1     0     1     1     1     0
                1     1     1     0     1     1     1     0
                0     0     0     0     0     0     0     0
                0     0     0     0     0     1     1     0
                1     1     1     1     0     1     1     0
                1     1     1     1     0     1     1     0
                1     1     1     1     0     0     0     0]);

%// Identify clusters.
L = bwlabel(BW,4)

s = regionprops(L,'Centroid');

CentroidCoord = vertcat(s.Centroid)

这给出了这个:

CentroidCoord =

    2.0000    2.0000
    2.5000    7.0000
    6.0000    2.0000
    6.5000    6.0000

这更简单,并且在使用 floor 后提供相同的输出。

希望有帮助!

【讨论】:

  • +1,但您可以改用regionprops 让您的生活更轻松。
  • 哈哈是的!今天晚些时候我会用 regionprops 提出另一个解决方案,谢谢!
  • 谢谢,这对您很有帮助