1

I have a vector<vector<Point> > X but I need to pass it to the function cvConvexityDefects that takes in input a CvArr*.

I have already read the topic Convexity defects C++ OpenCv. It takes in input these variables:

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

I cannot get the solution to work because I have a hull parameter that is a vector<Point> and I don't know how to transform it in a vector<int>.

So there are now two questions! :)

How can I convert a vector<vector<Point> > into a vector<int>?

Thanks in advance, have a good day!:)

Community
  • 1
  • 1
Marco Maisto
  • 91
  • 2
  • 11

1 Answers1

0

Use std::for_each and an accumulating object:

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

Used like this:

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

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

    return 0;
}
Peter Wood
  • 23,859
  • 5
  • 60
  • 99
  • thank oyu so much :) that's perfect! just a thing: i comment the line m_accumulated.push_back(point.z) because there are no point.z variable in the point object (just point.x and point.y). thanks again – Marco Maisto Apr 05 '12 at 12:58
  • @MarcoMaisto I don't know OpenCV so it was just a guess. Have fixed. – Peter Wood Apr 05 '12 at 13:01