문제

아래 그림과 같이 이미지가 보이는 배열이 있습니다. 어레이의 값은 각 픽셀/그리드의 입자 수를 나타냅니다. 주변/경계를 따라 0이 아닌 입자의 분포를 계산하고 싶습니다.주변/경계는 센터에서 가장 먼 지점의 분포를 나타냅니다.) 0이 아닌 입자가 존재하는 경우. 이것으로부터의 결과로서, 나는 다음을 얻고 싶다.

1) 주변/경계를 따라 0이 아닌 입자의 #

2) 입자가 상주하는 픽셀/그리드의 #

이 작업을 수행하는 빠른/효율적인 방법은 무엇입니까?

enter image description here

편집 1 : 경계의 예를 설명하는 스냅 샷경계선은 0이 아닌 입자를 추적합니다.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))

참고 :이 솔루션은이를 보장합니다 경계선의 각 지점에는 0이 아닌 입자 수가 있습니다..

다른 팁

주변 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