Distance between neighbouring objects in an image

Hi
I want to calculate the distance between neighbouring objects in a this image. I first convert the image into a binary image. The I locate all the object centroids using regionprops and store them in a matrix. Then, I calculate and plot all the possible distances from the centroid matrix using the distance formula. But this is not what I want. I want an output where distances between only neighbouring images are calculated and plotted.(See image attached).

5 Kommentare

What defines neighboring? For example, why isn't the orange connection below labeled in your image?
DGM
DGM am 22 Jun. 2023
Verschoben: DGM am 22 Jun. 2023
I don't know if this is appropriate, but this thought crossed my mind.
inpict = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1416954/image.png');
imshow(inpict); hold on
% i'm not going to bother processing a screenshot. these are just approximate
xy = [47 117; 62 281; 154 73; 270 53; 271 220; 333 297; 392 73; 458 208; 572 41; 586 281];
% calculate triangulation of given points
DT = delaunayTriangulation(xy);
triplot(DT,'color','g')
% find all edge lengths
E = edges(DT); % edges as pairs of vertex indices
E = permute(reshape(xy(E(:),:),[],2,2),[1 3 2]); % [x y]
edgelength = sqrt(sum(diff(E,1,3).^2,2)) % euclidean distance
edgelength = 20×1
164.6845 115.6936 227.4379 217.7200 271.4719 117.7115 187.8776 167.0030 123.6285 302.2383
Of course, I'm just blindly finding all the edge lengths without regard for which are which. You can use the edge list or the other properties of the triangulation object as you see fit. The only idea I'm suggesting is that perhaps this is one way to define the connectivity relationship (i.e. which objects are "neighbors").
Impressive idea @DGM!
What's the context? Robot path planning? Are you sure you don't want the shortest distance between the perimeters of two objects to each other to see if the vehicle will fit?
Or maybe you want the Hausdorf distance?
We really need to know the context of your problem to know what distance(s) you really need and what constitutes "a neighbor".
Yash
Yash am 23 Jun. 2023
Thank you so much @DGM. This was exactly what I was looking for.You have been a huge help. @Image Analyst my colleagues think it is better if shortest distance between the perimeters are calculated. The definition of "neighbour" as defined by @DGM's code works for me. I was wondering if there was a way to just calculate distances from the edges instead of the centroids. Once again, thank you so much for your help.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Gefragt:

am 22 Jun. 2023

Kommentiert:

am 23 Jun. 2023

Community Treasure Hunt

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

Start Hunting!

Translated by