如何访问 cv::Point_<int> 类型中的每个数字元素?

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

我有一个 cv::Rect 对象。从中,我得到了矩形的右下角。我想将点对象分成两个单独的 int 变量。我该怎么做?

这是我目前拥有的:

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

我想把bottomRight分成它的两个坐标点作为不同的int变量,比如:

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

当我尝试下标时,出现此错误:

type 'Point_' does not provide a subscript operator

在Python中,我会像上面那样下标。我如何在 C++ 中执行此操作?

cv::Point_<T>结构的xy坐标存储为public成员变量(的类型 T) 称为 xy(而不是作为 2 元素数组)。

所以,您的代码应该是:

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

(也就是说,如果您确实需要将它们与结构本身隔离。)