Search code examples
c++opencvc++11point

How to access each numerical element in cv::Point_<int> type?


I have a cv::Rect object. From it, I am getting the bottom right point of the rectangle. I want to separate the point object into two separate int variables. How do I do this?

This is what I have so far:

cv::Rect rectangle;
bottomRight = rectangle.br() // this gives me a Point <int>, such as [545, 364]

I want to separate bottomRight into its two coordinate points as different int variables, such as:

// bottomRight is [545, 364]
bottomRight_x = bottomRight[0] // should be 545
bottomRight_y = bottomRight[1] // should be 364

When I try to subscript, I get this error:

type 'Point_' does not provide a subscript operator

In Python, I would just subscript as above. How do I do this in C++?


Solution

  • The x and y ordinates of the cv::Point_<T> structure are stored as public member variables (of type T) called x and y (rather than as a 2-element array).

    So, your code should be:

    // bottomRight is [545, 364]
    bottomRight_x = bottomRight.x;
    bottomRight_y = bottomRight.y;
    

    (That is, if you really need to isolate them from the structure itself.)