Drawing Polygons in OpenCV? Drawing Polygons in OpenCV? arrays arrays

Drawing Polygons in OpenCV?


Just for the record (and because the opencv docu is very sparse here) a more reduced snippet using the c++ API:

  std::vector<cv::Point> fillContSingle;  [...]  //add all points of the contour to the vector  fillContSingle.push_back(cv::Point(x_coord,y_coord));  [...]  std::vector<std::vector<cv::Point> > fillContAll;  //fill the single contour   //(one could add multiple other similar contours to the vector)  fillContAll.push_back(fillContSingle);  cv::fillPoly( image, fillContAll, cv::Scalar(128));


Let's analyse the offending line:

const Point *elementPoints [1] = { contourElement.at(0) };

You declared contourElement as vector <vector<Point> >, which means that contourElement.at(0) returns a vector<Point> and not a const cv::Point*. So that's the first error.

In the end, you need to do something like:

vector<Point> tmp = contourElement.at(0);const Point* elementPoints[1] = { &tmp[0] };int numberOfPoints = (int)tmp.size();

Later, call it as:

fillPoly (contourMask, elementPoints, &numberOfPoints, 1, Scalar (0, 0, 0), 8);


contourElement is vector of vector<Point> and not Point :) so instead of:

const Point *elementPoints

put

const vector<Point> *elementPoints