从矩阵开始 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))
注意:此解决方案将确保 边界线上的每个点都有一个非零粒子计数.