Is it possible to get the distances between each centroid? For example I have 5 centroids (centroid 1 - 5) in an image and I would like to get the distance between centroid 1 and centroid 2, then centroid 1 and centroid 3, then centroid 1 and centroid 4, then centroid 1 and centroid 5, then for centroid 2 and centroid 3, and so on. Then I will store these distances in an adjacency list or an adjacency matrix so that I could create a graph network.

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 9 Apr. 2022

0 Stimmen

use pdist()

5 Kommentare

I have this chunk of code already that prints a line between connected coins, but this only takes into account adjacent centroids not all. Can you please edit it such that it can also give me the distances between all the coins and store it in an adjacency matrix/list?
%% Blob Analysis
hBlobAnalysis = vision.BlobAnalysis('MinimumBlobArea', 10000,'MaximumBlobArea',150000);
[objArea,objCentroid,bboxOut] = step(hBlobAnalysis, IBWopen);
%% Print Centroid
CC = bwconncomp(IBWopen);
S = regionprops(CC, 'centroid');
centroids = cat(1, S.Centroid);
NodeCount = length(objCentroid)
figure
subplot(1,2,2)
imshow(pic)
hold on
plot(centroids(:, 1), centroids(:, 2), 'b*','LineWidth', 5)
hold off
%% Print Lines
props = regionprops(IBWopen , 'Centroid', 'EquivDiameter');
meanDiam = mean([props.EquivDiameter]);
xy = vertcat(props.Centroid);
distances = pdist2(xy, xy);
hold on;
for k = 1 : size(distances, 1)
thisRow = distances(k, :);
closeIndexes = find(thisRow > 0 & thisRow < 1.12 * meanDiam);
for k2 = 1 : length(closeIndexes)
x1 = xy(k, 1);
x2 = xy(closeIndexes(k2), 1);
y1 = xy(k, 2);
y2 = xy(closeIndexes(k2), 2);
plot([x1, x2], [y1, y2], 'k-', 'LineWidth', 2)
end
end
hold off;
distances = squareform(pdist2(xy)) ;
is equivalent to what you have; I am not sure which would be faster. Either way, distances would already contain the distance between each centroid and each other centroid.
Since the distance between each point and itself is 0, then the distances array already acts as a weighted adjacency matrix.
Alright, my question now is that would I be able to create a graph network using MATLAB out of this weighted adjacency matrix similar to python's NetworkX?
G = graph(distances)
Is it possible to use graphtraverse or dfsearch to calculate for the effective resistance of the graph?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Get Started with Signal Processing Toolbox finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by