how to merge centroids?

how can i merge centroid on top right corner?
if the centroids of any pair are closer than a certain amount

3 Comments

Posting a raw image and your code for getting centroids out of that image can help the Community provide a good answer more quickly.
In general, you can make use of a property of subtraction in MATLAB to quickly calculate the differences between all combinations of numbers in a set:
» X = rand(1,5)
X =
0.655740699156587 0.035711678574190 0.849129305868777 0.933993247757551 0.678735154857773
» Y = rand(1,5)
Y =
0.757740130578333 0.743132468124916 0.392227019534168 0.655477890177557 0.171186687811562
» X - X'
ans =
0 -0.620029020582397 0.193388606712190 0.278252548600964 0.022994455701187
0.620029020582397 0 0.813417627294588 0.898281569183361 0.643023476283584
-0.193388606712190 -0.813417627294588 0 0.084863941888773 -0.170394151011004
-0.278252548600964 -0.898281569183361 -0.084863941888773 0 -0.255258092899777
-0.022994455701187 -0.643023476283584 0.170394151011004 0.255258092899777 0
» dX_values = X - X'
dX_values =
0 -0.620029020582397 0.193388606712190 0.278252548600964 0.022994455701187
0.620029020582397 0 0.813417627294588 0.898281569183361 0.643023476283584
-0.193388606712190 -0.813417627294588 0 0.084863941888773 -0.170394151011004
-0.278252548600964 -0.898281569183361 -0.084863941888773 0 -0.255258092899777
-0.022994455701187 -0.643023476283584 0.170394151011004 0.255258092899777 0
» dY_values = Y - Y'
dY_values =
0 -0.014607662453417 -0.365513111044165 -0.102262240400777 -0.586553442766772
0.014607662453417 0 -0.350905448590748 -0.087654577947360 -0.571945780313354
0.365513111044165 0.350905448590748 0 0.263250870643388 -0.221040331722606
0.102262240400777 0.087654577947360 -0.263250870643388 0 -0.484291202365995
0.586553442766772 0.571945780313354 0.221040331722606 0.484291202365995 0
» distances = sqrt(dX_values.^2 + dY_values.^2)
distances =
0 0.620201072368244 0.413520238381710 0.296449062428468 0.587003991651289
0.620201072368244 0 0.885879715449129 0.902548116484463 0.860581876796204
0.413520238381710 0.885879715449129 0 0.276591593378040 0.279093165353078
0.296449062428468 0.902548116484463 0.276591593378040 0 0.547443752982836
0.587003991651289 0.860581876796204 0.279093165353078 0.547443752982836 0
stats = regionprops(bw2);
s = regionprops(bw2, 'centroid');
C = cat(1, s.Centroid);
hold on
plot(C(:,1), C(:,2), 'b*')
hold off
D = pdist2(C(:,1),C(:,2),'euclidean','Smallest',2)
figure; imshow(P); hold on;
for i = 1 : length(stats)
hold on; rectangle('position', stats(i).BoundingBox, 'EdgeColor', 'g', 'LineWidth', 2)
end
title(sprintf('Jumlah Tumbuhan %d', length(stats)));
that is my code for finding the centroid, i dont know how to combine the 2 centroid if they were close enough.

Sign in to comment.

Answers (1)

KSSV
KSSV on 26 May 2022

0 votes

You have the coordinates of centroid, you can use knnsearch to get the nearest neighbors.

Asked:

on 25 May 2022

Commented:

on 26 May 2022

Community Treasure Hunt

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

Start Hunting!