【问题标题】:Transform a vector<vector<Point> > X into a IplImage* or cv::Mat*将 vector<vector<Point> > X 转换为 IplImage* 或 cv::Mat*
【发布时间】:2012-04-19 02:23:40
【问题描述】:

我有一个vector&lt;vector&lt;Point&gt; &gt; X,但我需要将它传递给函数cvConvexityDefects,该函数接受CvArr* 的输入。

我已经阅读了主题Convexity defects C++ OpenCv。它接受这些变量的输入:

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

我无法让解决方案正常工作,因为我有一个 vector&lt;Point&gt; 的船体参数,但我不知道如何将其转换为 vector&lt;int&gt;

所以现在有两个问题! :)

如何将vector&lt;vector&lt;Point&gt; &gt; 转换为vector&lt;int&gt;

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

【问题讨论】:

标签: c++ opencv vector


【解决方案1】:

使用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;
}

【讨论】:

  • 非常感谢 oyu :) 太完美了!只是一件事:我评论了 m_accumulated.push_back(point.z) 行,因为点对象中没有 point.z 变量(只有 point.x 和 point.y)。再次感谢
  • @MarcoMaisto 我不知道 OpenCV,所以这只是一个猜测。已修复。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-07
  • 2017-10-26
  • 1970-01-01
  • 2017-09-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多