04. 绘图功能

一、绘制直线

  我们可以在终端中使用 pip 安装 OpenCV 模块。默认是从国外的主站上下载,因此,我们可能会遇到网络不好的情况导致下载失败。我们可以在 pip 指令后通过 -i 指定国内镜像源下载

pip install opencv-python -i https://mirrors.aliyun.com/pypi/simple

  国内常用的 pip 下载源列表:

  在 OpenCV 中,我们可以使用 cv2.line() 方法 绘制一条直线,它的定义如下:

cv2.line(img: cv2.typing.MatLike, pt1: cv2.typing.Point, pt2: cv2.typing.Point, color: cv2.typing.Scalar, thickness: int = ..., lineType: int = ..., shift: int = ...) -> cv2.typing.MatLike: ...

  其中,参数 img 表示 绘图对象,也可以认为是 画布。参数 pt1 表示 直线的起点。参数 pt2 表示 直线的终点。参数 color 表示 直线的颜色,在 OperCV 中,默认采用的是 BGR 色彩空间。参数 thickness 表示 线条的宽度,默认是 1。参数 lineType 表示 线条样式。参数 shift 表示 点坐标中的小数位

  参数 lineType 表示 线条样式,主要表现为线条的抗锯齿,即线条边缘的毛刺,可选值如下:

cv2.FILLED
cv2.LINE_4
cv2.LINE_8                                                                      # 默认值
cv2.LINE_AA
import cv2
import numpy as np

if __name__ == '__main__':
    esp_key_value = 27
    
    image = np.zeros((480, 640, 3), dtype=np.uint8)                             # 1.创建一个黑丝图像
    cv2.line(image, (10, 10), (300, 300), (255, 0, 0), 5)                       # 2.在图像上绘制一条线
    cv2.imshow("image", image)                                                  # 3.显示图像
    key = cv2.waitKey(0)                                                        # 4.等待用户按键
    cv2.destroyAllWindows()                                                     # 5.关闭所有窗口

二、绘制矩形

  在 OpenCV 中,我们可以使用 cv2.rectangle() 方法 绘制矩形,它的定义如下:

cv2.rectangle(img: cv2.typing.MatLike, pt1: cv2.typing.Point, pt2: cv2.typing.Point, color: cv2.typing.Scalar, thickness: int = ..., lineType: int = ..., shift: int = ...) -> cv2.typing.MatLike: ..

  其中,参数 img 表示 绘图对象,也可以认为是 画布。参数 pt1 表示 矩形左上角坐标。参数 pt2 表示 矩形右下角坐标。参数 color 表示 矩形的颜色,在 OperCV 中,默认采用的是 BGR 色彩空间。参数 thickness 表示 线条的宽度,默认是 1,如果设置为 -1,则填充矩形。参数 lineType 表示 线条样式。参数 shift 表示 点坐标中的小数位

import cv2
import numpy as np

if __name__ == '__main__':
    esp_key_value = 27
    
    image = np.zeros((480, 640, 3), dtype=np.uint8)                             # 1.创建一个黑丝图像
    cv2.rectangle(image, (50, 100), (250, 300), (255, 0, 0), 10)                # 2.在图像上绘制一个矩形
    cv2.rectangle(image, (400, 100), (600, 300), (255, 0, 0), -1)               
    cv2.imshow("image", image)                                                  # 3.显示图像
    key = cv2.waitKey(0)                                                        # 4.等待用户按键
    cv2.destroyAllWindows()                                                     # 5.关闭所有窗口

三、绘制圆形

  在 OpenCV 中,我们可以使用 cv2.rectangle() 方法 绘制圆形,它的定义如下:

cv2.circle(img: cv2.typing.MatLike, center: cv2.typing.Point, radius: int, color: cv2.typing.Scalar, thickness: int = ..., lineType: int = ..., shift: int = ...) -> cv2.typing.MatLike: ...

  其中,参数 img 表示 绘图对象,也可以认为是 画布。参数 center 表示 圆心坐标。参数 radius 表示 圆的半径。参数 color 表示 圆的颜色,在 OperCV 中,默认采用的是 BGR 色彩空间。参数 thickness 表示 线条的宽度,默认是 1,如果设置为 -1,则填充圆形。参数 lineType 表示 线条样式。参数 shift 表示 点坐标中的小数位

import cv2
import numpy as np

if __name__ == '__main__':
    esp_key_value = 27
    
    image = np.zeros((480, 640, 3), dtype=np.uint8)                             # 1.创建一个黑丝图像
    cv2.circle(image, (150, 150), 100, (255, 0, 0), 10)                         # 2.在图像上绘制一个圆形
    cv2.circle(image, (450, 150), 100, (255, 0, 0), -1)               
    cv2.imshow("image", image)                                                  # 3.显示图像
    key = cv2.waitKey(0)                                                        # 4.等待用户按键
    cv2.destroyAllWindows()                                                     # 5.关闭所有窗口

四、绘制椭圆

  在 OpenCV 中,我们可以使用 cv2.ellipse() 方法 绘制椭圆,它的定义如下:

cv2.ellipse(img: cv2.typing.MatLike, center: cv2.typing.Point, axes: cv2.typing.Size, angle: float, startAngle: float, endAngle: float, color: cv2.typing.Scalar, thickness: int = ..., lineType: int = ..., shift: int = ...) -> cv2.typing.MatLike: ...

  其中,参数 img 表示 绘图对象,也可以认为是 画布。参数 center 表示 圆心坐标。参数 axes 表示 宽和高的一半。参数 angle 表示 椭圆偏移的角度(逆时针旋转)。参数 startAngle 表示 圆弧的起始角度。参数 endAngle 表示 圆弧的终止角度。参数 color 表示 圆的颜色,在 OperCV 中,默认采用的是 BGR 色彩空间。参数 thickness 表示 线条的宽度,默认是 1,如果设置为 -1,则填充圆形。参数 lineType 表示 线条样式。参数 shift 表示 点坐标中的小数位

import cv2
import numpy as np

if __name__ == '__main__':
    esp_key_value = 27
    
    image = np.zeros((480, 640, 3), dtype=np.uint8)                             # 1.创建一个黑丝图像
    cv2.ellipse(image, (150, 150), (100, 50), 45, 0, 360,(255, 0, 0), 10, cv2.LINE_AA)   # 2.在图像上绘制一个椭圆
    cv2.ellipse(image, (150, 300), (100, 50), 45, 0, 180,(255, 0, 0), 10, cv2.LINE_AA)
    cv2.ellipse(image, (400, 150), (100, 50), 45, 0, 360,(255, 0, 0), -1, cv2.LINE_AA)
    cv2.ellipse(image, (400, 300), (100, 50), 45, 0, 180,(255, 0, 0), -1, cv2.LINE_AA)
    cv2.imshow("image", image)                                                  # 3.显示图像
    key = cv2.waitKey(0)                                                        # 4.等待用户按键
    cv2.destroyAllWindows()                                                     # 5.关闭所有窗口

六、绘制多边形

  在 OpenCV 中,我们可以使用 cv2.polylines() 方法 绘制多边形,它的定义如下:

cv2.polylines(img: cv2.typing.MatLike, pts: _typing.Sequence[cv2.typing.MatLike], isClosed: bool, color: cv2.typing.Scalar, thickness: int = ..., lineType: int = ..., shift: int = ...) -> cv2.typing.MatLike: ...

  其中,参数 img 表示 绘图对象,也可以认为是 画布。参数 pts 表示 顶点的坐标序列。参数 isClosed 表示 图形是否封闭。参数 color 表示 直线的颜色,在 OperCV 中,默认采用的是 BGR 色彩空间。参数 thickness 表示 线条的宽度,默认是 1。参数 lineType 表示 线条样式。参数 shift 表示 点坐标中的小数位

import cv2
import numpy as np

if __name__ == '__main__':
    esp_key_value = 27
    
    pts1 = np.array([(100, 100), (200, 200), (300, 100)], np.int32)
    pts2 = np.array([(400, 100), (500, 200), (600, 100)], np.int32)

    image = np.zeros((480, 640, 3), dtype=np.uint8)                             # 1.创建一个黑丝图像
    cv2.polylines(image, [pts1], True, (255, 0, 0), 3)                          # 2.在图像上绘制一个多边形
    cv2.polylines(image, [pts2], False, (255, 0, 0), 3)
    
    cv2.imshow("image", image)                                                  # 3.显示图像
    key = cv2.waitKey(0)                                                        # 4.等待用户按键
    cv2.destroyAllWindows()                                                     # 5.关闭所有窗口

  如果,我们要画填充多边形,可以使用 cv2.fillPoly() 函数。

cv2.fillPoly(img: cv2.typing.MatLike, pts: _typing.Sequence[cv2.typing.MatLike], color: cv2.typing.Scalar, lineType: int = ..., shift: int = ..., offset: cv2.typing.Point = ...) -> cv2.typing.MatLike: ...
import cv2
import numpy as np

if __name__ == '__main__':
    esp_key_value = 27
    
    pts = np.array([(100, 100), (200, 200), (300, 100)], np.int32)

    image = np.zeros((480, 640, 3), dtype=np.uint8)                             # 1.创建一个黑丝图像
    cv2.fillPoly(image, [pts], (255, 0, 0))                                     # 2.在图像上绘制填充一个多边形

    cv2.imshow("image", image)                                                  # 3.显示图像
    key = cv2.waitKey(0)                                                        # 4.等待用户按键
    cv2.destroyAllWindows()                                                     # 5.关闭所有窗口

七、绘制文字

  在 OpenCV 中,我们可以使用 cv2.polylines() 方法 绘制字体,它的定义如下:

cv2.putText(img: cv2.typing.MatLike, text: str, org: cv2.typing.Point, fontFace: int, fontScale: float, color: cv2.typing.Scalar, thickness: int = ..., lineType: int = ..., bottomLeftOrigin: bool = ...) -> cv2.typing.MatLike: ...

  其中,参数 img 表示 绘图对象,也可以认为是 画布。参数 text 表示 要输出的字体。参数 org 表示 第一个文字左下角的坐标。参数 fontFace 表示 字体样式。参数 fontScale 表示 字体大小。参数 color 表示 直线的颜色,在 OperCV 中,默认采用的是 BGR 色彩空间。参数 thickness 表示 线条的宽度,默认是 1。参数 lineType 表示 线条样式。参数 bottomLeftOrigin 表示 是否垂直倒影显示

  参数 fontFace 表示 字体样式,它的可选值如下:

cv2.FONT_HERSHEY_SIMPLEX                                                        # 正常大小无衬线字体
cv2.FONT_HERSHEY_PLAIN                                                          # 小号无衬线字体
cv2.FONT_HERSHEY_DUPLEX                                                         # 正常大小无衬线字体,但比较复杂
cv2.FONT_HERSHEY_COMPLEX                                                        # 正常大小衬线字体
cv2.FONT_HERSHEY_TRIPLEX                                                        # 正常大小衬线字体,但比较复杂
cv2.FONT_HERSHEY_COMPLEX_SMALL                                                  # 小号衬线字体
cv2.FONT_HERSHEY_SCRIPT_SIMPLEX                                                 # 手写样式字体
cv2.FONT_HERSHEY_SCRIPT_COMPLEX                                                 # 手写样式字体,但比较复杂
cv2.FONT_ITALIC                                                                 # 斜体字体
import cv2
import numpy as np

if __name__ == '__main__':
    esp_key_value = 27
    text = "Hello Sakura!"

    image = np.zeros((480, 640, 3), dtype=np.uint8)                             # 1.创建一个黑丝图像
    cv2.putText(image, text, (100, 100), cv2.FONT_HERSHEY_COMPLEX, 2, (255, 0, 0), 3)   # 2.在图像上绘制字体
    cv2.putText(image, text, (100, 300), cv2.FONT_HERSHEY_COMPLEX, 2, (255, 0, 0), 3, bottomLeftOrigin=True)

    cv2.imshow("image", image)                                                  # 3.显示图像
    key = cv2.waitKey(0)                                                        # 4.等待用户按键
    cv2.destroyAllWindows()                                                     # 5.关闭所有窗口

  默认情况下,OpenCV 中无法识别中文,因此,我们需要借助 Pillow 第三方模块显示中文。我们在终端中通过 pip 安装 Pillow。

pip install pillow -i https://mirrors.aliyun.com/pypi/simple
import cv2
import numpy as np

from PIL import ImageFont, ImageDraw, Image

if __name__ == '__main__':
    esp_key_value = 27
    text = "你好,小樱!"

    image = np.zeros((480, 640, 3), dtype=np.uint8)                             # 1.创建一个黑丝图像

    font = ImageFont.truetype("simhei.ttf", 32)                                 # 2.导入字体
    image_pil = Image.fromarray(image)                                          # 3.将OpenCV图像转换为PIL图像
    draw = ImageDraw.Draw(image_pil)                                            # 4.创建一个绘图对象
    draw.text((100, 100), text, font=font, fill=(255, 0, 0))                    # 5.在图像上绘制文本

    image = np.array(image_pil)                                                 # 6.将PIL图像转换为OpenCV图像

    cv2.imshow("image", image)                                                  # 7.显示图像
    key = cv2.waitKey(0)                                                        # 8.等待用户按键
    cv2.destroyAllWindows()                                                     # 9.关闭所有窗口
posted @ 2025-12-25 23:12  星光映梦  阅读(3)  评论(0)    收藏  举报