user5045533
user5045533

Reputation:

Distance between connected components

I wanted to know if there is some inbuilt function to get distance between different connected components in MATLAB. I am using bwlabel to get the various connected components.Is there some way to get the distance between these connected components?

Upvotes: 1

Views: 1114

Answers (1)

Benoit_11
Benoit_11

Reputation: 13945

I guess you could use regionprops to locate the centroid of each connected component and then apply pdist to find the pairwise distance between each of them.

Simple example:

clear
clc
close all

%// Create logical array
BW = logical ([1     1     1     0     0     0     0     0
               1     1     1     0     1     1     0     0
               1     1     1     0     1     1     0     0
               1     1     1     0     0     0     1     0
               1     1     1     0     0     0     1     0
               1     1     1     0     0     0     1     0
               1     1     1     0     0     1     1     0
               1     1     1     0     0     0     0     0])

%/ Call regionprops and concatenate centroid coordinates
S = regionprops(bwlabel(BW,4),'Centroid')

Centroids = vertcat(S.Centroid)

%// Measure pairwise distance
D = pdist(Centroids,'euclidean')

Outputs in the Command Window:

BW =

     1     1     1     0     0     0     0     0
     1     1     1     0     1     1     0     0
     1     1     1     0     1     1     0     0
     1     1     1     0     0     0     1     0
     1     1     1     0     0     0     1     0
     1     1     1     0     0     0     1     0
     1     1     1     0     0     1     1     0
     1     1     1     0     0     0     0     0


S = 

3x1 struct array with fields:

    Centroid


Centroids =

    2.0000    4.5000
    5.5000    2.5000
    6.8000    5.8000


D =

    4.0311    4.9729    3.5468

Upvotes: 2

Related Questions