c++ - 将 vector<vector<Point>> X 转换为 IplImage* 或 cv::Mat*

标签 c++ opencv vector

我有一个 vector<vector<Point> > X 但我需要将它传递给函数 cvConvexityDefects输入一个 CvArr* .

我已经读过题目Convexity defects C++ OpenCv .它接受输入这些变量:

vector<Point>& contour, vector<int>& hull, vector<Point>& convexDefects

我无法使解决方案起作用,因为我的船体参数是 vector<Point>我不知道如何在 vector<int> 中转换它.

那么现在有两个问题! :)

如何转换 vector<vector<Point> >进入 vector<int>

提前致谢,祝您有美好的一天!:)

最佳答案

使用 std::for_each 和一个累积对象:

class AccumulatePoints
{
public:
    AccumulatePoints(std::vector<int>& accumulated)
    : m_accumulated(accumulated)
    {
    }

    void operator()(const std::vector<Point>& points)
    {
        std::for_each(points.begin(), points.end(), *this);
    }

    void operator()(const Point& point)
    {
        m_accumulated.push_back(point.x);
        m_accumulated.push_back(point.y);
    }
private:
    std::vector<int>& m_accumulated;
};

这样使用:

int main()
{
    std::vector<int> accumulated;
    std::vector<std::vector<Point>> hull;

    std::for_each(hull.begin(), hull.end(), AccumulatePoints(accumulated));

    return 0;
}

关于c++ - 将 vector<vector<Point>> X 转换为 IplImage* 或 cv::Mat*,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10027022/

相关文章:

c++ - OpenGL 闪烁屏幕

c++ - 使用 uftrace 分析 C++ 程序后如何理解 2 个 main() 函数?

c++ - 如何在编译时计算梅森数

C++访问 vector 中的对元素

java - 公开使用的 Vector 中的私有(private)类

c++ - 为什么我们可以push_back仅用range定义的子 vector ?

c++ - 使用指向对象的指针显式调用析构函数

c++ - 在 xcode 中编译和链接 OpenCV 3.0.0 时遇到问题

opencv - 在 ubuntu 18.04 上安装 opencv 2.4.9

java - OpenCV 层次结构始终为空