Opencv - 用多边形内的那些创建二进制掩码(给定它的顶点)

Opencv - create binary mask with ones inside a polygon(given its vertices)

要求:创建一个 640x360 大小的二进制掩码。位于多边形内的点标记为 1。否则为 0。

输入:多边形的顶点。当用户在 html 页面上标记多边形时获得顶点。

是否可以在 opencv 中以简单的方式这样做?

我知道一种可能的解决方案。 http://alienryderflex.com/polygon/。这允许找到一个点是否位于给定顶点的多边形内。但这需要我 运行 遍历每个像素以查找它们是否位于多边形内,我觉得这是一个漫长的过程。

Is it possible to do so in opencv in an easy way?

是的,您想使用 OpenCV fillPoly 函数:

void fillPoly(Mat& img, const Point** pts, const int* npts, int ncontours, const Scalar& color, int lineType=8, int shift=0, Point offset=Point() )

您可以找到有关如何使用此 here 的示例,但实际上 fillPoly() 所做的是 "fills an area bounded by several polygonal contours. The function can fill complex areas, for example, areas with holes, contours with self-intersections (some of their parts), and so forth."