C++ 按点坐标旋转点的矢量
是否有一种简单的方法可以将存储在C++ 按点坐标旋转点的矢量,c++,opencv,vector,C++,Opencv,Vector,是否有一种简单的方法可以将存储在cv::vector中的一组点移动cv::Point定义的量?类似于std::rotate,但仅针对点的一个坐标。它应该考虑向量的大小 比如说, [1,0]、[0,1]、[1,2]、[2,3]被[0,2]移动到[1,2]、[0,3]、[1,0]、[2,1] 我能想到的唯一方法是使用for循环手动执行此操作。您可以: 从向量创建Mat。它将是一个2通道矩阵,Nx1 重塑形状,使其成为1通道矩阵Nx2 获取包含x和y坐标的列 使用MatIterator 请注意,Mat
cv::vector
中的一组点移动cv::Point
定义的量?类似于std::rotate,但仅针对点的一个坐标。它应该考虑向量的大小
比如说,
[1,0]、[0,1]、[1,2]、[2,3]
被[0,2]
移动到[1,2]、[0,3]、[1,0]、[2,1]
我能想到的唯一方法是使用for循环手动执行此操作。您可以:
向量
创建Mat
。它将是一个2通道矩阵,Nx1MatIterator
Mat
没有反向迭代器(用于实现右旋转),因此,当shift为负数时,应将向量的大小添加到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;
}
#包括
#包括
#包括
使用名称空间std;
使用名称空间cv;
int main()
{
向量pts={点(1,0),点(0,1),点(1,2),点(2,3)};
点位移(0,2);
对于(const Point&p:pts){coutstd::rotate
不采用自定义交换程序,因此需要设计迭代器适配器以提供点的y坐标视图。最好编写循环。