Pairwise Distances Between Two "islands"/"connected components" in Numpy Array
This is what you would need:
from scipy.spatial.distance import cdist
def Distance(a, m, n):
return cdist(np.argwhere(a==m),np.argwhere(a==n),'minkowski',p=1.).min()
or similarly per @MaxPowers comment (claim: cityblock
is faster):
return cdist(np.argwhere(a==m),np.argwhere(a==n),'cityblock').min()
Find the locations of islands and calculate pairwise distance of locations and get the minimum. I am not 100% sure of your desired distance, but I think you are looking for l1
norm. If not, you can change the cdist
measure to your desired metric.
output:
Distance(a,2,3)
1.0
Distance(a,2,1)
2.0
Distance(a,3,1)
5.0
Distance(a,4,3)
5.0
For many blobs or bigger blobs or if performance/memory efficiency is a criteria, you might want to work with contours of those islands. With that in mind, we will use OpenCV's findContours
to get the contours, then perform pairwise distance computation and get the min
one as the final output. The implementation would look something like this that gets all possible pairiwise distances -
from scipy.spatial.distance import cdist
import cv2
ids = np.arange(1, a.max()+1) #np.unique(a)[1:] if not in ranged sequence
idxs = []
for id_ in ids:
im = (a == id_).astype(np.uint8)
contours,_ = cv2.findContours(im, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
idx = contours[0][:, 0]
idxs.append(idx)
# Get pairwise indices and then distances
r,c = np.triu_indices(len(ids), 1)
pdists = {(ids[i],ids[j]):cdist(idxs[i], idxs[j]).min() for (i, j) in zip(r, c)}
Output dict for given sample -
In [225]: pdists
Out[225]:
{(1, 2): 2.0,
(1, 3): 5.0,
(1, 4): 7.810249675906654,
(2, 3): 1.0,
(2, 4): 5.0,
(3, 4): 3.605551275463989}
By default,cdist
uses euclidean distance as the metric
. Depending on your definition of straight line between islands, you might want to try out other metrics, namely 'minkowski'
and 'cityblock'
for Minkowski
and Manhattan
distances respectively.
So, cdist(idxs[i], idxs[j])
would change to cdist(idxs[i], idxs[j], metric=...)
.