将 Vector<vector<Point> > X 转换为 IplImage* 或 cv::Mat*

Transform a vector<vector<Point> > X into a IplImage* or cv::Mat*

本文关键字:gt lt IplImage cv Mat vector Vector Point 转换      更新时间:2023-10-16

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

我已经阅读了主题"凸性缺陷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;
}