マトリックスから始めます 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))
注:このソリューションはそれを確実にします 境界線上の各ポイントには、ゼロ粒子カウントがありません.