简体   繁体   中英

Extract data from Point2f

I'm developing a code that needs to find a red piece. I'm using the function minEnclosingCircle in order to get the center of the image.

The way that the function gives me the center is in a vector format vector<Point2f>center( contours.size() ); . However I need this data to determine a region of interest (ROI). Is there any way I can extract the data from the point, so that I can have two coordinates in X and Y in an integer?

Thanks!

--update--

I'll post some code to try to explain better what i want to do:

vector<vector<Point> > contours_poly( contours.size() );
vector<Point2f>center( contours.size() );
vector<float>radius( contours.size() );


for( int i = 0; i < contours.size(); i++ )
{   
    approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true );
    minEnclosingCircle( (Mat)contours_poly[i], center[i], radius[i] );
 }

So once i get contours from my image I apply the function minEnclosingCicrle to determinate the center. I'm very new in OpenCV. I suppose that the center of the circles that it finds is saved in this vector of Point2f. The thing I want to do is to access to this points in order to get the position of my object to determinate a ROI around the object.

From your comments

Suppose I get 20 circles and I know that the circle 15 is the biggest. I want to know de X and Y position of center[15], which is a point2f but inside a vector.

without bounds checking:

float x = center[15].x;
float y = center[15].y;

with bounds checking:

float x = center.at(15).x;
float y = center.at(15).y;

Concerning the bounds of the vector, if the index is >= center.size() , you're out of bounds. So you can do the checking yourself:

 int idx = 15;
 if (idx < center.size())
 {
   // OK to access with index 15
 }

Note that the indexing starts at 0 , so here you are accessing the 16th point.

If you use IDE then you can just type something like

Point2f p;
p.

and IDE will help you to complete the proper field of the class.

If you write you program without smart auto completion that you can just look in the source code of opencv to see (this parts of code are from opencv source and distributed under the license you can find in opencv website and source codes):

typedef Point_<float> Point2f;
....
template<typename _Tp> class CV_EXPORTS Point_{
....
    _Tp x, y; //< the point coordinates

minEnclosingCircle returns a centre as Point2f and a radius, not sure how you're getting a vector..?

In any case, you can access the coordinates of a Point2f directly as they are public members.

Point2f a;
int x = (int)a.x;
int y = (int)a.y;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM