Polygon drawing in OpenCV?

What am I doing wrong here?

vector <vector<Point> > contourElement; for (int counter = 0; counter < contours -> size (); counter ++) { contourElement.push_back (contours -> at (counter)); const Point *elementPoints [1] = {contourElement.at (0)}; int numberOfPoints [] = {contourElement.at (0).size ()}; fillPoly (contourMask, elementPoints, numberOfPoints, 1, Scalar (0, 0, 0), 8); 

I keep getting the error regarding the const point. Compiler says

 error: cannot convert 'std::vector<cv::Point_<int>, std::allocator<cv::Point_<int> > >' to 'const cv::Point*' in initialization 

What am I doing wrong? (PS: Obviously, ignore the missing bracket at the end of the for loop due to the fact that this is only part of my code)

+4
source share
3 answers

Let's analyze the line of violation:

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

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

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, name it as:

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

For the record only (and because the opencv document is very sparse here) a more compressed 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)); 
+10
source

contourElement is a vector vector<Point> , not a dot :) therefore instead of:

 const Point *elementPoints 

put

 const vector<Point> *elementPoints 
+2
source

Source: https://habr.com/ru/post/1383227/


All Articles