Compute the centroid of each numbered component in a label matrix. More...

Modules

 centroids
 The C++ interface for centroids.
 

Detailed Description

Compute the centroid of each numbered component in a label matrix.

The centroid of each connected component is an (x,y) coordinate that is the average of the (x,y) coordinates of all of the pixels with that label.

The input image should be a 2D, single precision floating point image where the components are numbered sequentially from 1 to N, where N is the overall number of components.

// Get a label matrix for the connected components in an image
const int len = 8;
array r = randn(len, len) * 10.0f;
// Convert to binary
array in = (r > 5.0f);
array labels = regions(in);
print(labels);
// 0 0 0 0 4 0 5 0
// 0 0 0 0 0 0 5 5
// 0 1 0 1 0 0 0 0
// 0 0 1 0 1 1 0 6
// 1 1 0 0 0 1 0 6
// 0 0 0 3 0 0 0 6
// 0 0 0 0 3 0 0 6
// 0 2 0 0 0 3 0 0
array c = centroids(labels);
print(c);
// 1.0000 3.1250 2.6250
// 2.0000 7.0000 1.0000
// 3.0000 6.0000 4.0000
// 4.0000 0.0000 4.0000
// 5.0000 0.6667 6.3333
// 6.0000 4.5000 7.0000