• OpenCV4(C++)—— 几何图形的绘制



    一、基本图形

    1、线

    绘制线,要给出两个点坐标

    void cv::line(InputOutputArray img, Point pt1, Point pt2, const Scalar& color, 
    int thickness = 1, int lineType = LINE_8, int shift = 0);
                    
    img:输入/输出图像,即要在其上绘制直线的图像。
    pt1:直线的起始点坐标。
    pt2:直线的结束点坐标。
    color:直线的颜色,可以是 Scalar 类型表示的颜色值。
    thickness:直线的厚度。默认值为1,表示单像素宽度。
    lineType:线条类型,定义了边框的连接方式。默认值为LINE_8。
    shift:坐标点的小数位数。默认值为0
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    2、线圆

    绘制圆,要给出圆点和半径:

    void cv::circle(InputOutputArray img, Point center, int radius, const Scalar& color,
                    int thickness = 1, int lineType = LINE_8, int shift = 0);
                    
    img: 在该图像上进行绘制操作。可以是单通道或多通道图像。
    center: 圆心坐标,指定圆的中心点位置,类型为 cv::Point 或 cv::Point2f。
    radius: 圆的半径,指定圆的大小。
    color: 圆的颜色,类型为 cv::Scalar,表示 BGR 颜色值。例如,红色可表示为 (0, 0, 255)thickness (可选): 表示绘制圆的线条粗细。默认值为 1。如果设为-1,则绘制一个实心圆
    lineType (可选): 指定绘制线条的样式。默认值为 LINE_8,表示8连通线条。
    shift (可选): 像素坐标的小数位数。默认值为 0
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    3、线椭圆

    椭圆相比于圆,半径分成了半长轴和半短轴,并且有角度

    void cv::ellipse(InputOutputArray img, Point center, Size axes, double angle, double startAngle,
     double endAngle, const Scalar& color, int thickness = 1, int lineType = LINE_8, int shift = 0);
                    
    axes:椭圆的主轴尺寸,以半长轴和半短轴的大小表示。
    angle:椭圆旋转角度(逆时针方向)。
    startAngle:椭圆起始角度(以逆时针方向测量)。
    endAngle:椭圆结束角度(以逆时针方向测量)。
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    4、矩形

    绘制矩形,要给出左上角坐标和右下角坐标或者是左上角坐标和宽、高

    第一种
    void cv::rectangle(InputOutputArray img, Point pt1, Point pt2, const Scalar& color, 
    int thickness = 1, int lineType = LINE_8, int shift = 0);
    
    第二种
    void cv::rectangle(InputOutputArray img, Rect rect, const Scalar& color, 
    int thickness = 1, int lineType = LINE_8, int shift = 0);
                    
    pt1:矩形的左上角点坐标。
    pt2:矩形的右下角点坐标
    
    或者:cv::Rect(左上角点坐标,宽,高)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    代码如下(示例):

    #include 
    #include  
    
    using namespace std;
    
    int main()
    {
        cv::Mat mask = cv::Mat::zeros(cv::Size(640, 400), CV_8UC3);
    
        cv::line(mask, cv::Point2f(300, 300), cv::Point2f(400, 400), cv::Scalar(255, 255, 255), 3);  // 宽度为3的直线
    
        cv::circle(mask, cv::Point(30, 30), 10, cv::Scalar(255, 255, 255), 1);  // 空心圆
        cv::circle(mask, cv::Point(100, 30), 15, cv::Scalar(0, 0, 255), -1);   // 实心圆
    
        cv::ellipse(mask, cv::Point(150, 30), cv::Size(30, 15), 30, 0, 360, cv::Scalar(255, 0, 0), -1);  // 实心椭圆
      
        cv::rectangle(mask, cv::Point(200, 200), cv::Point(300, 300), cv::Scalar(0, 255, 0), 2);  // 矩形
        // cv::rectangle(mask, cv::Rect(200,200,100,100), cv::Scalar(0, 255, 0), 2);  // 矩形
    
        cv::imshow("原图", mask);
        cv::waitKey(0);
        cv::destroyAllWindows();
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    在这里插入图片描述

    二、多边形

    图像分割中,目标对象往往是不规则的形状。根据目标对象的多个顶点坐标来绘制(进行分割标签标注的时候,不就是在目标周围点很多个坐标吗)。

    目前OpenCV4提供的绘制多边形的fillPoly有两种构造方式:

    第一种:
    void cv::fillPoly(InputOutputArray img, const Point** pts, const int* npts, int ncontours,
     const Scalar& color, int lineType = LINE_8, int shift = 0, Point offset = Point());
     
    第二种:
    void fillPoly(InputOutputArray img, InputArrayOfArrays pts,
                               const Scalar& color, int lineType = LINE_8, int shift = 0,
                               Point offset = Point() );
                               
    第一种:
    pts:多边形顶点的数组指针,可以使用二维数组或vector来表示。每个多边形都由一组点组成。
    npts:多边形顶点数目的整型数组指针,指定每个多边形的顶点数。
    ncontours:多边形数量,即pts和npts数组中多边形的数量。
    
    第二种:
    上面三个参数统一为一个数组,存放所有多边形的坐标
    
    color:填充的颜色,可以是 Scalar 类型表示的颜色值。
    lineType:线条类型,定义了多边形轮廓的连接方式。默认值为LINE_8。
    shift:坐标点的小数位数。默认值为0。
    offset:偏移量,添加到所有顶点的坐标中。默认情况下为Point(),表示没有偏移。
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    比较简单和常用的是第二种构造方式,只需要给出所有要绘制的坐标就行。在实际应用中,目标对象的坐标是通过一些方法来捕获的,如findContours函数,与之对应的绘制函数还有一个drawContours(),后面用实例再一起说明。

    代码如下(示例):

    #include 
    #include 
    #include  
    
    using namespace std;
    
    int main()
    {
        cv::Mat image(400, 400, CV_8UC3, cv::Scalar(0, 0, 0));
        
        std::vector<cv::Point> points1 = { cv::Point(50, 50), cv::Point(200, 100), cv::Point(150, 200) };
        std::vector<cv::Point> points2 = { cv::Point(250, 250), cv::Point(350, 300), cv::Point(300, 150) };
        std::vector<cv::Point> points3 = { cv::Point(100, 200), cv::Point(200, 300), cv::Point(150, 350) };
    
        std::vector<std::vector<cv::Point>> polygons;
        polygons.push_back(points1);
        polygons.push_back(points2);
        polygons.push_back(points3);
    
        cv::fillPoly(image, polygons, cv::Scalar(255, 0, 0));
    
        cv::imshow("Image", image);
        cv::waitKey(0);
        cv::destroyAllWindows();
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    在这里插入图片描述

  • 相关阅读:
    主成分分析(Principal Component Analysis, PCA)
    MySQL | SELECT
    OSCS 安全周报第 58 期:VMware Aria Operations SSH 身份验证绕过漏洞 (CVE-2023-34039)
    基于Python实现的WoW游戏软件
    【Azure Developer】如何通过Azure Portal快速获取到对应操作的API并转换为Python代码
    java遍历Map的方式
    C语言中的数据表现形式:常量
    [Java] Lock(锁)的tryLock失败是否需要unlock?
    全球隐私计算技术发展概览
    python 拆分pdf(有可执行文件exe)
  • 原文地址:https://blog.csdn.net/qq_43199575/article/details/133738863