Mike Cole
Mike Cole

Reputation: 43

OpenCV3 - Accessing label centroids

OpenCV3 has a lot of cool stuff. I did some filtering with connectedComponentsWithStats accessing the array of statistics but can anyone just show me how to access centroids of each label?

Documentation says that's possible as well but i'm not getting it.

Mat stats, centroids, labelImage;
int nLabels = connectedComponentsWithStats(input, labelImage, stats, centroids, connectivity);

Upvotes: 2

Views: 1367

Answers (1)

Miki
Miki

Reputation: 41765

centroids is a matrix of double with two columns (x, y) and rows equals the number of labels.

You can access it like:

Mat1i labels;
Mat1i stats;
Mat1d centroids;
int n_labels = connectedComponentsWithStats(img, labels, stats, centroids);

for (int i = 0; i < centroids.rows; ++i)
{
    cout << "x: " << centroids(i, 0) << " y: " << centroids(i, 1) << endl;
    circle(outputImage, Point(centroids(i, 0), centroids(i, 1)), 3, Scalar(0,255,0));
}

If you declared centroids as Mat, access it like: centroids.at<double>(i,0)

Upvotes: 3

Related Questions