你可以:
- 从
vector<Point> 创建一个Mat。这将是一个 2 通道矩阵,Nx1
- 重塑,使其成为 1 通道矩阵,Nx2
- 获取包含 x 和 y 坐标的列
- 使用
MatIterator 在该列上使用旋转
注意Mat没有反向迭代器(实现右旋转),所以当shift为负数时,你应该将vector<Point>的大小加到shift中并使用左旋转。
您主要是在使用矩阵标题,因此不会复制数据。
代码如下:
#include <opencv2\opencv.hpp>
#include <vector>
#include <iostream>
using namespace std;
using namespace cv;
int main()
{
vector<Point> pts = { Point(1, 0), Point(0, 1), Point(1, 2), Point(2, 3) };
Point shift(0,2);
for (const Point& p : pts) { cout << "[" << p.x << ", " << p.y << "] "; } cout << endl;
// [1, 0] [0, 1] [1, 2] [2, 3]
// ----------------------------
Mat mpts(pts);
Mat xy = mpts.reshape(1);
Mat x = xy.col(0);
Mat y = xy.col(1);
if (shift.x < 0)
shift.x += pts.size();
std::rotate(x.begin<Point::value_type>(), x.begin<Point::value_type>() + shift.x, x.end<Point::value_type>());
if (shift.y < 0)
shift.y += pts.size();
std::rotate(y.begin<Point::value_type>(), y.begin<Point::value_type>() + shift.y, y.end<Point::value_type>());
// ----------------------------
for (const Point& p : pts) { cout << "[" << p.x << ", " << p.y << "] "; } cout << endl;
// [1, 2] [0, 3] [1, 0] [2, 1]
return 0;
}