Matlab:沿阵列边界计算非零数的有效方法

时间:2013-11-06 23:59:58

标签: arrays image matlab count particles

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

  

1)沿周边/边界的非零粒子数,和

     

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

任何快速/有效的方法吗?

enter image description here

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

3 个答案:

答案 0 :(得分:2)

从粒子计数的矩阵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 :(得分:1)

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

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

然后,外围的粒子计数nn = M(peri)。 (1)沿边界的粒子总数为sum(n)。 (2)它们驻留的像素数为sum(n > 0)

答案 2 :(得分:1)

- 我为你的问题提出了这个算法。

- 您想要的细节不是100%清晰,因此可能无法精确计算您想要的内容。

- 解释在评论中

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