[C++] opencv - fillPoly(填充多边形)函数介绍和使用场景

27 篇文章 1 订阅

fillPoly函数介绍

fillPoly()函数是OpenCV中用于绘制填充多边形的函数。函数原型如下:

/** @brief Fills the area bounded by one or more polygons.

The function cv::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.

@param img Image.
@param pts Array of polygons where each polygon is represented as an array of points.
@param color Polygon color.
@param lineType Type of the polygon boundaries. See #LineTypes
@param shift Number of fractional bits in the vertex coordinates.
@param offset Optional offset of all points of the contours.
 */
CV_EXPORTS_W void fillPoly(InputOutputArray img, InputArrayOfArrays pts,
                           const Scalar& color, int lineType = LINE_8, int shift = 0,
                           Point offset = Point() );

/** @overload */
CV_EXPORTS void fillPoly(InputOutputArray img, const Point** pts,
                         const int* npts, int ncontours,
                         const Scalar& color, int lineType = LINE_8, int shift = 0,
                         Point offset = Point() );

函数参数说明如下:

img:输入图像。
pts:多边形数组,其中每个多边形由点数组表示。
color:多边形的颜色。
lineType:多边形边界的类型。参见#LineTypes。
shift:顶点坐标中的分数位数。
offset:轮廓的所有点的可选偏移量。
此外,还提供了一个重载版本的fillPoly函数,它接受一个const Point**类型的pts参数,以及一个const int*类型的npts参数和一个表示轮廓数量的整数ncontours。

使用场景

fillPoly()函数适用于需要绘制填充多边形的场景,例如在图像上绘制一个封闭的图形、制作一个简单的遮罩等。

使用案例

绘制实心三角形

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main()
{
    // 创建一个黑色背景的图像
    Mat img = Mat::zeros(300, 300, CV_8UC3);

    // 定义三角形三个顶点的坐标
    Point points[1][3];
	points[0][0] = Point(50, 50);
	points[0][1] = Point(150, 200);
	points[0][2] = Point(200, 50);


    // 定义三角形的颜色
    Scalar color(0, 255, 0);

    // 将三角形的顶点坐标存储到数组中
    const Point* ppts[1] = {points[0]};
    const int npts[] = {3};
    // 在图像上绘制实心三角形
    fillPoly(img, ppts, npts, 1, color);

    // 显示结果
    imshow("实心三角形", img);
    waitKey(0);

    return 0;
}

绘制实心矩形

#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;

int main()
{
    // 创建一个空白图像
    Mat image = Mat::zeros(400, 400, CV_8UC3);

    // 定义矩形的顶点
    Point pts[4];
    pts[0] = Point(50, 50);
    pts[1] = Point(200, 50);
    pts[2] = Point(200, 200);
    pts[3] = Point(50, 200);

    // 将顶点放入一个vector中
    vector<Point> poly;
    for (int i = 0; i < 4; i++)
    {
        poly.push_back(pts[i]);
    }

    // 填充矩形
    fillPoly(image, poly, Scalar(255, 0, 0));

    // 显示图像
    imshow("实心矩形", image);
    waitKey(0);

    return 0;
}

 

绘制实心多边形

#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;

int main()
{
    // 创建一个空白图像
    Mat image = Mat::zeros(512, 512, CV_8UC3);

    // 定义矩形的顶点
    Point pts[5];
    pts[0] = Point(100, 100);
    pts[1] = Point(300, 150);
    pts[2] = Point(300, 350);
    pts[3] = Point(250, 450);
    pts[4] = Point(50, 450);

    // 将顶点放入一个vector中
    vector<Point> poly;
    for (int i = 0; i < 5; i++)
    {
        poly.push_back(pts[i]);
    }

    // 填充多边形
    fillPoly(image, poly, Scalar(255, 0, 0));

    // 显示图像
    imshow("实心多边形", image);
    waitKey(0);

    return 0;
}

结论

fillPoly()函数是OpenCV中用于绘制填充多边形的函数。可以用来绘制实心三角形,实心矩形,实心多边形等。

  • 9
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
fillPolyOpenCV库中的一个函数,用于填充多边形。该函数可以接受多个多边形的顶点坐标数组,并将其填充为指定的颜色。 在使用fillPoly函数之前,首先需要创建一个和图像大小相同的空白画布(可以是灰度图或彩色图)。然后,将多个多边形的顶点坐标存储在一个二维数组中,每个多边形的坐标用一个单独的数组表示。 fillPoly函数的基本语法如下: ```cpp void fillPoly(InputOutputArray img, InputArrayOfArrays pts, const Scalar& color, int lineType = LINE_8, int shift = 0, Point offset = Point() ) ``` 其中,参数img是要填充的图像,pts是一个包含多个多边形的数组,color是填充的颜色,lineType是线的类型(默认为8邻域连接线),shift是坐标点除以2的移位数,offset是坐标点的偏移量(默认为0,0)。 调用fillPoly函数后,会将指定的多边形填充为指定的颜色,并在原始图像上进行修改。 下面是一个使用fillPoly函数的例子: ```cpp #include <opencv2/opencv.hpp> using namespace cv; int main() { Mat img = Mat::zeros(400, 400, CV_8UC3); // 创建一个400x400的彩色图像 Point pts[1][4]; // 定义一个四边形的顶点坐标数组 pts[0][0] = Point(50, 50); pts[0][1] = Point(50, 150); pts[0][2] = Point(150, 150); pts[0][3] = Point(150, 50); const Point* ppt[1] = { pts[0] }; int npt[] = { 4 }; fillPoly(img, ppt, npt, 1, Scalar(255, 0, 0), LINE_8); // 填充四边形为蓝色 imshow("image", img); waitKey(0); return 0; } ``` 以上代码创建了一个400x400的彩色图像,然后定义了一个四边形的顶点坐标数组,最后调用fillPoly函数将该四边形填充为蓝色,并显示结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老狼IT工作室

你的鼓励将是我创作的最大动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值