OpenCV 向量<point> - 没有用于初始化'cv::Point_<int>'的匹配承包商

OpenCV vector<point> - No matching contractor for initialization of 'cv::Point_<int>'

本文关键字:gt lt Point 承包商 int 用于 point 向量 初始化 OpenCV cv      更新时间:2023-10-16

我试图将本地构造的(内部方法)cv::Point3i放置到对象变量(声明为std::vector<cv::Point>)上。这样做,我得到编译错误(不是运行时):

memory - No matching constructor for initialization of 'cv::Point_<int>'

用Point2i做同样的事情(省略我需要的一个值),编译器不会抛出错误。

下面是.cpp文件中的代码片段:

void ObjectDetector::centroids2Dto3D() {
    const int* map_ptr = (int*)mapHeight.data;
    unsigned long steps[2];
    steps[0] = mapHeight.step1(0);
    steps[1] = mapHeight.step1(1);
    for (std::vector<cv::Point>::iterator it = centroidsXZ.begin(); it != centroidsXZ.end(); it++) {
        const int x = (*it).x;
        const int z = (*it).y;
        int y = map_ptr[steps[0] * x + steps[1] * z];
        // MARK: The following line causes the error. Without it, the program compiles fine
        centroids.emplace_back(cv::Point3i(x,y,z));
    }
}

由于我不擅长调试c++,我倾向于把错误归咎于我的代码,但我在这里找不到问题。

有人能给我指出一个解决方案或一条通往它的道路吗?

谢谢!

由于您插入到类型为cv::Point3i的矢量对象,那么centroids的类型应该是:std::vector<cv::Point3i>

还有,你把emplace_back叫错了。它的参数应该是转发给Point3i的构造函数的参数,即:centroids.emplace_back(x,y,z);

使用emplace_back将避免使用push_back时所需的额外复制或移动操作。