MATLAB:配列の境界に沿ってゼロ以外の数値をカウントする効率的な方法

StackOverflow https://stackoverflow.com/questions/19825429

質問

以下に示すように画像が見える配列があります。配列内の値は、各ピクセル/グリッドの粒子の数を表します。周辺/境界に沿って非ゼロ粒子の分布を計算したい周辺/境界とは、中心から最も遠いポイントの分布を指します) ゼロ以外の粒子が存在する場所. 。これからの出力として、私は取得したいと思います:

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