Merging labels according to centroid-distances
In this notebook we will merge labels in a label image according to their centroid-distances to each other. Labels close-by will be merged.
See also
For demonstration purposes, we use a modified version of the labels derived from the blobs example-image. We artificially introduce gaps between them.
|
cle._ image
shape | (254, 256) |
dtype | uint32 |
size | 254.0 kB |
min | 0.0 | max | 47.0 |
|
From this image, we extract the coordinates of centroids. From these centroids, we can build a distance matrix. In this matrix, the distance from all centroids to all other centroids is computed. The diagonale is zero as it corresponds to the distance of one centroid to itself. Furthermore, the distance to background (first row and first colum) is also zero, as background is not considered for distance computation.
|
cle._ image
shape | (48, 48) |
dtype | float32 |
size | 9.0 kB |
min | 0.0 | max | 324.1848 |
|
We can threshold this distance matrix with a given maximum distance. The result is a binary matrix.
|
cle._ image
shape | (48, 48) |
dtype | uint8 |
size | 2.2 kB |
min | 0.0 | max | 1.0 |
|
If we werged labels with the background, all labels would be merged because all touch the background. In order to prevent this, we set the first row and column to zero.
|
cle._ image
shape | (48, 48) |
dtype | uint8 |
size | 2.2 kB |
min | 0.0 | max | 1.0 |
|
Using the binary matrix above we can now merge the labels accordingly.
|
cle._ image
shape | (254, 256) |
dtype | uint32 |
size | 254.0 kB |
min | 0.0 | max | 4.0 |
|