Azzera filtri
Azzera filtri

Counting the number of adjacent binary points in a matrx

2 visualizzazioni (ultimi 30 giorni)
I am writing a function to count the number of adjacent binary true values in a matrix, and the number of these clusters in a matrix. Adjacent points are connected at either at least one of the top, bottom or sides.
For example, the function run on this matrix would return:
cluster = [10]
as there are 10 points in a single cluster, and the function run on this matrix:
would return
cluster = [1 1 1 1]
as there are 4 separate clusters, each with one point in. length(cluster) should equal the number of clusters in the image, and sum(cluster) == sum(A==1). Could I have some help in writing this function? I have thought to find the indices where the array = 1, then look to see if they are adjacent. It works in counting the single points, but it breaks down when counting the points in a larger cluster.
Minimal working example.
B = rand(10);
A = zeros(size(B));
A(B>0.9) = 1;
[row,col] = find(A(:,:,1)==1);
cluster = 1;
for i = 1:length(row)-1
count = abs(row(i+1)-row(i))<2 && abs(col(i+1)-col(i))<2;
if count == 1
cluster = cluster+count;
elseif count == 0
cluster = [cluster 1];
end
end

Risposte (0)

Prodotti

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by