Ask Your Question
1

How to address a specific centroid obtained from the function connectedComponentsWithStats?

asked 2016-08-05 01:54:08 -0600

ElectronicEng2015 gravatar image

updated 2016-08-05 02:05:49 -0600

Hello everyone, I hope you can help me with a specific issue. I have been using the function connectedComponentsWithStats to extract the centroid of an object in an image. Here is part of my code,

    int num_objects = connectedComponentsWithStats(img, labels, stats, centroids);
    cout << "Object " << i << "with position: " << centroids.at<Point2d>(i) << endl;

when I try to debug this instruction I get the following error:

OpenCV Error: Assertion failed (elemSize() == (((((DataType<_Tp>::type) & ((512 - 1) << 3)) >> 3) + 1) << ((((sizeof(size_t)/4+1)16384|0x3a50) >> ((DataType<_Tp>::type) & ((1 << 3) - 1))2) & 3))) in cv::Mat::at, file c:\opencv310\build\include\opencv2\core\mat.inl.hpp, line 962

I am trying to obtain a point (x, y) which the definition type of each element is a double or CV_64F. When I run the following instruction, I can see all the values of all the found centroids. Nevertheless when I try to retrieve just one specific centroid, the centroid reference "centroids.at<point2d>(i)" just lanches the failure message

           cout <<"positions: " << centroids << endl;

I really appreciate any suggestion that can help me retrieve a particular centroid point information in the correct format (double)

Thank you in advance

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
0

answered 2016-08-05 02:23:43 -0600

berak gravatar image

please take another look at the docs

Centroids are accessed via centroids(label, 0) for x and centroids(label, 1) for y. The data type CV_64F.

so, it's not a Mat of Point2d, but a 2xN Mat, and you'll have to access it like:

double x = centroids.at<double>(i, 0);
double y = centroids.at<double>(i, 1);
edit flag offensive delete link more

Comments

1

Thanks for your help, I also realized that it is possible to address each centroid point by row, being like:

centroids.row(i) as a point

ElectronicEng2015 gravatar imageElectronicEng2015 ( 2016-08-05 03:47:20 -0600 )edit

While it is true that you can access it the way you do, OP's solution is perfectly valid as well, i.e. using Point2d to access the elements. I suspect OP is running out of bounds, that is i > num_objects

Allan Nørgaard gravatar imageAllan Nørgaard ( 2016-10-03 10:03:18 -0600 )edit

Using Point2d doesn't work. I am using Open CV 4 and following along in an open CV book which uses the Point2d method. I have exact same code the OP has and it doesn't work. Returns same error as OP. Not running off the end of array, it happens at first element and there are elements (in my case 259). If I apply answer above the code works.

Scott R gravatar imageScott R ( 2019-10-26 19:51:32 -0600 )edit

The code will run without error if Point2i is used. However, the centroid values are not correct, the values are much to large.

cout << "Object " << i << "with position: " << centroids.at<Point2i>(i) << endl;
Scott R gravatar imageScott R ( 2019-11-17 12:47:27 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-08-05 01:54:08 -0600

Seen: 5,056 times

Last updated: Aug 05 '16