C++ 在 OpenCV 中绘制多边形?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8281239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:18:53  来源:igfitidea点击:

Drawing Polygons in OpenCV?

c++arraysopencvimage-processingvector

提问by fdh

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 an error on the const Point part. The compiler says

我在 const Point 部分不断收到错误消息。编译器说

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 this being only part of my code)

我究竟做错了什么?(PS:显然忽略for循环末尾缺少的括号,因为这只是我代码的一部分)

回答by Oliver Zendel

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

只是为了记录(并且因为这里的 opencv 文档非常稀疏)使用 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));

回答by karlphillip

Let's analyse the offending line:

让我们分析违规行:

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

You declared contourElementas 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.

您声明contourElementvector <vector<Point> >,这意味着contourElement.at(0)返回 avector<Point>而不是 a const cv::Point*。所以这是第一个错误。

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

回答by nikola-miljkovic

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

contourElement 是向量vector<Point>而不是 Point :) 所以而不是:

const Point *elementPoints

put

const vector<Point> *elementPoints