OpenCvSharp从入门到实践-(07)绘制图形

目录

1、线段的绘制

1.1实例1-绘制线段拼成一个"王"字

2、矩形的绘制

2.1实例2-绘制一个矩形边框

2.2实例3-绘制一个实心矩形

3、圆的绘制

3.1实例4-绘制"交通灯"

4、多边形绘制

4.1实例5-绘制等腰梯形

5、文字的绘制

5.1实例6-绘制文字OpenCvSharp


1、线段的绘制

OpenCvSharp提供Cv2.Line方法,使用该方法可以绘制各种线段,Cv2.Line方法如下:

public static void Line(InputOutputArray img, Point pt1, Point pt2, Scalar color, int thickness = 1, LineTypes lineType = LineTypes.Link8, int shift = 0)

说明:

摘要:
    Draws a line segment connecting two points

参数:
  img:
    The image.

  pt1:
    First point of the line segment.

  pt2:
    Second point of the line segment.

  color:
    Line color.

  thickness:
    Line thickness. [By default this is 1]

  lineType:
    Type of the line. [By default this is LineType.Link8]

  shift:
     Number of fractional bits in the point coordinates. [By default this is 0]

1.1实例1-绘制线段拼成一个"王"字

代码如下:

Mat canvas = Mat.Zeros(300, 300, MatType.CV_8UC3);

Cv2.Line(canvas, new Point(50, 50), new Point(250, 50), new Scalar(255, 0, 0), 5);
Cv2.Line(canvas, new Point(50, 150), new Point(250, 150), new Scalar(0, 255, 0), 10);
Cv2.Line(canvas, new Point(50, 250), new Point(250, 250), new Scalar(0, 0, 255), 15);
Cv2.Line(canvas, new Point(150, 50), new Point(150, 250), new Scalar(0, 255, 255), 20);

Cv2.ImShow("lines", canvas);
Cv2.WaitKey();
Cv2.DestroyAllWindows();

效果

每条线段的起点坐标和终点坐标

2、矩形的绘制

OpenCvSharp提供Cv2.Rectangle方法,该方法既可以绘制矩形边框,也可以绘制实心矩形,其函数如下:

public static void Rectangle(InputOutputArray img, Point pt1, Point pt2, Scalar color, int thickness = 1, LineTypes lineType = LineTypes.Link8, int shift = 0)

说明:
摘要:
    Draws simple, thick or filled rectangle

参数:
  img:
    Image.

  pt1:
    One of the rectangle vertices.

  pt2:
    Opposite rectangle vertex.

  color:
    Line color (RGB) or brightness (grayscale image).

  thickness:
    Thickness of lines that make up the rectangle. Negative values make the function
    to draw a filled rectangle. [By default this is 1]

  lineType:
    Type of the line, see cvLine description. [By default this is LineType.Link8]

  shift:
    Number of fractional bits in the point coordinates. [By default this is 0]

2.1实例2-绘制一个矩形边框

代码如下:

Mat canvas = Mat.Zeros(300, 300, MatType.CV_8UC3);

Cv2.Rectangle(canvas, new Point(50, 50), new Point(200, 150), new Scalar(255, 0, 0), 20);

Cv2.ImShow("Rectangle", canvas);
Cv2.WaitKey();
Cv2.DestroyAllWindows();

效果

2.2实例3-绘制一个实心矩形

代码如下:

Mat canvas = Mat.Zeros(300, 300, MatType.CV_8UC3);

Cv2.Rectangle(canvas, new Point(50, 50), new Point(200, 150), new Scalar(255, 0, 0), -1);

Cv2.ImShow("Rectangle", canvas);
Cv2.WaitKey();
Cv2.DestroyAllWindows();

效果

3、圆的绘制

 OpenCvSharp提供Cv2.Circle方法,该方法既可以绘制圆形边框,也可以绘制实心圆形,Cv2.Circle函数如下:

public static void Circle(InputOutputArray img, int centerX, int centerY, int radius, Scalar color, int thickness = 1, LineTypes lineType = LineTypes.Link8, int shift = 0)

说明:

摘要:
     Draws a circle

 参数:
   img:
     Image where the circle is drawn.

   centerX:
     X-coordinate of the center of the circle.

   centerY:
     Y-coordinate of the center of the circle.

   radius:
     Radius of the circle.

   color:
     Circle color.

   thickness:
     Thickness of the circle outline if positive, otherwise indicates that a filled
     circle has to be drawn. [By default this is 1]

   lineType:
     Type of the circle boundary. [By default this is LineType.Link8]

   shift:
     Number of fractional bits in the center coordinates and radius value. [By default
     this is 0]

3.1实例4-绘制"交通灯"

代码如下:

Mat canvas = Mat.Zeros(300, 300, MatType.CV_8UC3);

Cv2.Circle(canvas, new Point(50, 150), 40, new Scalar(0, 0, 255), -1);

Cv2.Circle(canvas, new Point(150, 150), 40, new Scalar(0, 255, 255), -1);

Cv2.Circle(canvas, new Point(250, 150), 40, new Scalar(0, 255, 0), -1);

Cv2.ImShow("TrafficLights", canvas);
Cv2.WaitKey();
Cv2.DestroyAllWindows();

效果

4、多边形绘制

OpenCvSharp提供Polylines方法绘制多边形,其函数如下:

public static void Polylines(Mat img, IEnumerable<IEnumerable<Point>> pts, bool isClosed, Scalar color, int thickness = 1, LineTypes lineType = LineTypes.Link8, int shift = 0)

说明:

摘要:
    draws one or more polygonal curves

参数:
  img:
    画布
  pts:
    多边形各顶点组成的列表
  isClosed:
    是否闭合
  color:
    颜色
  thickness:
    线条宽度
  lineType:

  shift:

4.1实例5-绘制等腰梯形

代码如下:

Mat canvas = Mat.Zeros(300, 300, MatType.CV_8UC3);

List<OpenCvSharp.Point> pts1 = new List<OpenCvSharp.Point>
{
    new OpenCvSharp.Point(100,50),
    new OpenCvSharp.Point(200,50),
    new OpenCvSharp.Point(50,250),
    new OpenCvSharp.Point(250,250)
};

List<List<OpenCvSharp.Point>> pts = new List<List<Point>>();
pts.Add(pts1);

Cv2.Polylines(canvas, pts, true, new Scalar(0, 0, 255), 1);

Cv2.ImShow("Polylines", canvas);
Cv2.WaitKey();
Cv2.DestroyAllWindows();

效果

5、文字的绘制

OpenCvSharp提供Cv2.PutText方法进行文字绘制,其函数如下:

public static void PutText(InputOutputArray img, string text, Point org, HersheyFonts fontFace, double fontScale, Scalar color, int thickness = 1, LineTypes lineType = LineTypes.Link8, bool bottomLeftOrigin = false)

说明:

摘要:
    renders text string in the image

参数:
  img:
    Image.

  text:
    Text string to be drawn.

  org:
    Bottom-left corner of the text string in the image.

  fontFace:
    Font type, see #HersheyFonts.

  fontScale:
    Font scale factor that is multiplied by the font-specific base size.

  color:
    Text color.

  thickness:
    Thickness of the lines used to draw a text.

  lineType:
    Line type. See #LineTypes

  bottomLeftOrigin:
    When true, the image data origin is at the bottom-left corner. Otherwise, it is at the top-left corner.

5.1实例6-绘制文字OpenCvSharp

代码如下:

Mat canvas = Mat.Zeros(300, 300, MatType.CV_8UC3);

Cv2.PutText(canvas, "OpenCvSharp", new Point(0, 50), HersheyFonts.Italic, 1, new Scalar(0,255,0), 1, LineTypes.AntiAlias, false);

Cv2.ImShow("Text", canvas);
Cv2.WaitKey();
Cv2.DestroyAllWindows();

效果

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2023-12-09 09:12:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-09 09:12:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-09 09:12:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-09 09:12:02       20 阅读

热门阅读

  1. forceUpdate

    2023-12-09 09:12:02       32 阅读
  2. HASH以及leetcode刷题

    2023-12-09 09:12:02       33 阅读
  3. 区分工业设计软件中CAD、CAE、CAM、PDM等概念

    2023-12-09 09:12:02       40 阅读
  4. Elasticsearch的Snapshot and Restore(快照备份与恢复)

    2023-12-09 09:12:02       36 阅读
  5. 2021 OWASP Top 10 Web Application Security Risks (2)

    2023-12-09 09:12:02       29 阅读
  6. MySQL六 | 索引

    2023-12-09 09:12:02       40 阅读
  7. mysql存json数据时的查询办法

    2023-12-09 09:12:02       41 阅读
  8. 2312d,d语言来绑定C++和rust

    2023-12-09 09:12:02       40 阅读
  9. 微服务和无服务器架构时代的持续测试

    2023-12-09 09:12:02       46 阅读
  10. centos7.9 安装sersync+rsync 服务器数据实时同步

    2023-12-09 09:12:02       44 阅读
  11. vue 批量下载文件,不走后端接口的方法

    2023-12-09 09:12:02       41 阅读
  12. nginx 的概念、高并发处理及详细参数配置

    2023-12-09 09:12:02       39 阅读
  13. Axios

    Axios

    2023-12-09 09:12:02      29 阅读