Question

I have an array whose image looks as shown below. The values in the array represent number of particles at each pixel/grid. I want to calculate distribution of non-zero particles along the periphery/boundary (periphery/boundary refers to the distribution of farthest points from center) where non-zero particles exist. As an output from this, I would like to obtain:

1) # of non-zero particles along the periphery/boundary, and

2) # of pixels/grids where those particles reside

Any quick/efficient way of doing this?

enter image description here

EDIT 1: Snapshot Describing the Example of a Boundary The boundary line traces the non-zero particles. enter image description here

Was it helpful?

Solution

Starting with a matrix M of particle counts, this will get you a mask in Mb of the boundary as it has been defined by the question,

% 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

That is what I understand you want your boundary mask to look like. The number of pixels that make up the boundary is

numBorderPix = sum(Mb(:))

The number of particles on those border points is then

numBorderParticles = sum(M(Mb))

NOTE: This solution will ensure that each point on the boundary line has a non-zero particle count.

OTHER TIPS

The periphery peri for logical indexing of your matrix M is

peri = true(25);
peri(2:end-1, 2:end-1) = false;

Then, the particle counts n in the periphery are n = M(peri). (1) The total number of particles along the boundary is sum(n). (2) The number of pixels where they reside is sum(n > 0).

-I came up with this algorithm for your problem.

-The details of what you want are not 100% clear so that may not compute exactly what you want.

-The explanation is in the comments

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top