【问题标题】:Matlab: Efficient way of counting non-zero numbers along the boundary of an arrayMatlab:沿数组边界计算非零数的有效方法
【发布时间】:2013-11-06 23:59:57
【问题描述】:

我有一个数组,其图像如下所示。数组中的值表示每个像素/网格处的粒子数。我想计算非零粒子沿外围/边界的分布(外围/边界是指距中心最远点的分布存在非零粒子的位置 em>。作为输出,我想获得:

1) 沿外围/边界的非零粒子数,以及

2) 这些粒子所在的像素/网格数

有什么快速/有效的方法吗?

编辑 1:描述边界示例的快照 边界线跟踪非零粒子。

【问题讨论】:

  • 我无法理解这里的术语。您能否展示一个简单的 5x5 示例矩阵以及所需的输出是什么样的?

标签: arrays image matlab count particles


【解决方案1】:

从粒子计数矩阵M 开始,这将为您提供问题定义的边界Mb 中的掩码,

% define particle count matrix and find non-zero locations
M = randi(5,10,10)-1
[nr,nc] = size(M);
[pRows,pCols] = find(M);

% identify locations that compose the "boundary" line
boundCoords = [accumarray(pCols,pRows',[nc 1],@min)', ...
               accumarray(pCols,pRows',[nc 1],@max)', ...
               1:nr 1:nr; ...
               1:nc 1:nc, ...
               accumarray(pRows,pCols',[nr 1],@min)', ...
               accumarray(pRows,pCols',[nr 1],@max)'];
boundCoords = unique(boundCoords','rows');
boundCoords(any(boundCoords==0,2),:)=[]; %' remove possible (unlikely) zeros

% create a mask representation of the boundary line
Mb = false(size(M));
Mb(sub2ind(size(Mb),boundCoords(:,1),boundCoords(:,2))) = true

这就是我理解您希望您的边界掩码看起来的样子。构成边界的像素数为

numBorderPix = sum(Mb(:))

那么这些边界点上的粒子数为

numBorderParticles = sum(M(Mb))

注意:此解决方案将确保边界线上的每个点都具有非零粒子数

【讨论】:

  • 我有一个相关的问题。如果我想找到三个不同的边界,它们是 - 1) 一个具有最大粒子数的边界,2) 一个具有最小粒子数的边界,以及 3) 一个具有“x%”最大粒子数的边界(#1)?让我知道您是否理解,否则我应该为此提出不同的问题。谢谢!
  • @Pupil 最好发布一个新问题。
  • 这是new question
  • @chappjc 我不知道您是否检查了新问题。但我认为修改新方面的解决方案(定义边界的两种方法)并获得两个掩码之间的最小二乘距离会很容易。
【解决方案2】:

用于矩阵逻辑索引的外围peri M

peri = true(25);
peri(2:end-1, 2:end-1) = false;

那么,外围的粒子数nn = M(peri)。 (1) 沿边界的粒子总数为sum(n)。 (2) 它们所在的像素数为sum(n > 0)

【讨论】:

  • 请看上面正在编辑的草图,它描述了我正在谈论的边界。
【解决方案3】:

-我想出了这个算法来解决你的问题。

-您想要的细节不是 100% 清楚,因此可能无法准确计算出您想要的。

-解释在cmets中

A=full(sprand(10,10,0.9));

crossKernel=[0 1 0; 1 1 1; 0 1 0]; %% neighbor kernel
isBorder = (conv2(ones(size(A)),crossKernel,'same')~=5); %% find pixels on border
isZeroOnBorder = isBorder & (A==0); %% find zeros on border
%%% the pixels on the new border are...
isNewBorder = (conv2(double(isZeroOnBorder),crossKernel,'same')... %% next to a zero on border
              | isBorder )... %% or on the border of the matrix
              & (~isZeroOnBorder); %% and are not zeros on border
newBorderLength=nnz(isNewBorder) %% counting and obtaining result

【讨论】:

    猜你喜欢
    • 2011-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多