매트릭스로 시작합니다 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))
참고 :이 솔루션은이를 보장합니다 경계선의 각 지점에는 0이 아닌 입자 수가 있습니다..