我有一个数组,其图像看起来如下所示。阵列中的值表示每个像素/网格处的粒子数。我想计算沿外围/边界的非零粒子的分布(外围/边界是指从中心的最远点的分布) 存在非零颗粒的地方. 。作为此的输出,我想获得:

1)沿周围/边界的非零粒子的#

2)这些颗粒驻留的像素/网格

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

enter image description here

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

有帮助吗?

解决方案

从矩阵开始 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))

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

其他提示

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

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

然后,粒子计数 n 在外围是 n = M(peri). 。 (1)沿边界的粒子总数为 sum(n). 。 (2)它们居住的像素的数量 sum(n > 0).

- 我为您的问题提出了这种算法。

- 您想要的内容并非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
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top