OpenCV-Python学习<六> 绘图

本文介绍了如何使用OpenCV库在图像上绘制各种图形,包括直线、矩形、圆以及添加文字。通过实例代码展示了如何设置颜色、线条类型和粗细,以及实心和空心图形的区别。此外,还演示了如何绘制椭圆和多边形,并讲解了在图像上添加文字的方法,包括字体、大小、颜色和方向的设置。

OpenCV提供了方便的绘图功能,可以绘制直线,矩阵,圆,椭圆等多种几何图形,还能再指定位置添加文字说明。

1. 绘制直线:

img = v2.line(img, pt1, pt2, color [, thickness [, lineType]])

返回值, img: 绘制出的图像。

参数:

img: 在其上绘制直线的载体图像(绘图的容器载体,也成为画布,画板)

pt1,pt2: 线段的起点和终点。类型为tuple.

color:绘制直线的颜色。通常使用BGR模型表示。 例如:(0, 0, 255)表示红色。

thickness:线条的粗细。默认为1. 值越大越粗。

lineType: 线条类型。默认为8连接类型。

import cv2
import sys

img_src = cv2.imread("images/sample_1.webp")
if(img_src is None):
    print("Read file error.")
    sys.exit()
print("Image Shape:", img_src.shape)

width = img_src.shape[1]
height = img_src.shape[0]

img_des = cv2.line(img_src, (0, 0), (width, height), (0, 0, 255), 3, cv2.LINE_AA)

cv2.imshow("Orz", img_src)
cv2.imshow("After", img_des)
cv2.waitKey()
cv2.destroyAllWindows()

2. 绘制矩形:

img = cv2.rectangle(img, pt1, pt2, color[

venv) mac@macdeMacBook-Pro project % python -c "import paddle; print(paddle.__version__)" /Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages/paddle/utils/cpp_extension/extension_utils.py:718: UserWarning: No ccache found. Please be aware that recompiling all source files may be required. You can download and install ccache from: https://github.com/ccache/ccache/blob/master/doc/INSTALL.md warnings.warn(warning_message) 3.2.2 (venv) mac@macdeMacBook-Pro project % pip uninstall paddleocr -y WARNING: Skipping paddleocr as it is not installed. (venv) mac@macdeMacBook-Pro project % pip install "paddleocr>=2.7.0.3" --no-cache-dir Collecting paddleocr>=2.7.0.3 Downloading paddleocr-3.3.2-py3-none-any.whl.metadata (55 kB) Requirement already satisfied: paddlex<3.4.0,>=3.3.0 in /Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages (from paddlex[ocr-core]<3.4.0,>=3.3.0->paddleocr>=2.7.0.3) (3.3.12) Requirement already satisfied: PyYAML>=6 in /Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages (from paddleocr>=2.7.0.3) (6.0.2) Requirement already satisfied: requests in /Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages (from paddleocr>=2.7.0.3) (2.32.5) Requirement already satisfied: typing-extensions>=4.12 in /Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages (from paddleocr>=2.7.0.3) (4.15.0) Requirement already satisfied: aistudio-sdk>=0.3.5 in /Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages (from paddlex<3.4.0,>=3.3.0->paddlex[ocr-core]<3.4.0,>=3.3.0->paddleocr>=2.7.0.3) (0.3.8) Requirement already satisfied: chardet in /Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages (from paddlex<3.4.0,>=3.3.0->paddlex[ocr-core]<3.4.0,>=3.3.0->paddleocr>=2.7.0.3) (5.2.0) Requirement already satisfied: colorlog in /Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages (from paddlex<3.4.0,>=3.3.0->paddlex[ocr-core]<3.4.0,>=3.3.0->paddleocr>=2.7.0.3) (6.10.1) Requirement already satisfied: filelock in /Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages (from paddlex<3.4.0,>=3.3.0->paddlex[ocr-core]<3.4.0,>=3.3.0->paddleocr>=2.7.0.3) (3.20.1) Requirement already satisfied: huggingface-hub in /Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages (from paddlex<3.4.0,>=3.3.0->paddlex[ocr-core]<3.4.0,>=3.3.0->paddleocr>=2.7.0.3) (1.2.3) Requirement already satisfied: modelscope>=1.28.0 in /Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages (from paddlex<3.4.0,>=3.3.0->paddlex[ocr-core]<3.4.0,>=3.3.0->paddleocr>=2.7.0.3) (1.33.0) Requirement already satisfied: numpy>=1.24 in /Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages (from paddlex<3.4.0,>=3.3.0->paddlex[ocr-core]<3.4.0,>=3.3.0->paddleocr>=2.7.0.3) (2.2.6) Requirement already satisfied: packaging in /Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages (from paddlex<3.4.0,>=3.3.0->paddlex[ocr-core]<3.4.0,>=3.3.0->paddleocr>=2.7.0.3) (25.0) Requirement already satisfied: pandas>=1.3 in /Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages (from paddlex<3.4.0,>=3.3.0->paddlex[ocr-core]<3.4.0,>=3.3.0->paddleocr>=2.7.0.3) (2.3.3) Requirement already satisfied: pillow in /Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages (from paddlex<3.4.0,>=3.3.0->paddlex[ocr-core]<3.4.0,>=3.3.0->paddleocr>=2.7.0.3) (12.0.0) Requirement already satisfied: prettytable in /Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages (from paddlex<3.4.0,>=3.3.0->paddlex[ocr-core]<3.4.0,>=3.3.0->paddleocr>=2.7.0.3) (3.17.0) Requirement already satisfied: py-cpuinfo in /Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages (from paddlex<3.4.0,>=3.3.0->paddlex[ocr-core]<3.4.0,>=3.3.0->paddleocr>=2.7.0.3) (9.0.0) Requirement already satisfied: pydantic>=2 in /Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages (from paddlex<3.4.0,>=3.3.0->paddlex[ocr-core]<3.4.0,>=3.3.0->paddleocr>=2.7.0.3) (2.12.5) Requirement already satisfied: ruamel.yaml in /Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages (from paddlex<3.4.0,>=3.3.0->paddlex[ocr-core]<3.4.0,>=3.3.0->paddleocr>=2.7.0.3) (0.18.17) Requirement already satisfied: ujson in /Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages (from paddlex<3.4.0,>=3.3.0->paddlex[ocr-core]<3.4.0,>=3.3.0->paddleocr>=2.7.0.3) (5.11.0) Requirement already satisfied: imagesize in /Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages (from paddlex[ocr-core]<3.4.0,>=3.3.0->paddleocr>=2.7.0.3) (1.4.1) Requirement already satisfied: opencv-contrib-python==4.10.0.84 in /Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages (from paddlex[ocr-core]<3.4.0,>=3.3.0->paddleocr>=2.7.0.3) (4.10.0.84) Requirement already satisfied: pyclipper in /Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages (from paddlex[ocr-core]<3.4.0,>=3.3.0->paddleocr>=2.7.0.3) (1.4.0) Requirement already satisfied: pypdfium2>=4 in /Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages (from paddlex[ocr-core]<3.4.0,>=3.3.0->paddleocr>=2.7.0.3) (5.2.0) Requirement already satisfied: python-bidi in /Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages (from paddlex[ocr-core]<3.4.0,>=3.3.0->paddleocr>=2.7.0.3) (0.6.7) Requirement already satisfied: shapely in /Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages (from paddlex[ocr-core]<3.4.0,>=3.3.0->paddleocr>=2.7.0.3) (2.1.2) Requirement already satisfied: psutil in /Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages (from aistudio-sdk>=0.3.5->paddlex<3.4.0,>=3.3.0->paddlex[ocr-core]<3.4.0,>=3.3.0->paddleocr>=2.7.0.3) (7.2.0) Requirement already satisfied: tqdm in /Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages (from aistudio-sdk>=0.3.5->paddlex<3.4.0,>=3.3.0->paddlex[ocr-core]<3.4.0,>=3.3.0->paddleocr>=2.7.0.3) (4.67.1) Requirement already satisfied: bce-python-sdk in /Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages (from aistudio-sdk>=0.3.5->paddlex<3.4.0,>=3.3.0->paddlex[ocr-core]<3.4.0,>=3.3.0->paddleocr>=2.7.0.3) (0.9.56) Requirement already satisfied: click in /Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages (from aistudio-sdk>=0.3.5->paddlex<3.4.0,>=3.3.0->paddlex[ocr-core]<3.4.0,>=3.3.0->paddleocr>=2.7.0.3) (8.3.1) Requirement already satisfied: setuptools in /Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages (from modelscope>=1.28.0->paddlex<3.4.0,>=3.3.0->paddlex[ocr-core]<3.4.0,>=3.3.0->paddleocr>=2.7.0.3) (80.9.0) Requirement already satisfied: urllib3>=1.26 in /Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages (from modelscope>=1.28.0->paddlex<3.4.0,>=3.3.0->paddlex[ocr-core]<3.4.0,>=3.3.0->paddleocr>=2.7.0.3) (2.6.2) Requirement already satisfied: python-dateutil>=2.8.2 in /Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages (from pandas>=1.3->paddlex<3.4.0,>=3.3.0->paddlex[ocr-core]<3.4.0,>=3.3.0->paddleocr>=2.7.0.3) (2.9.0.post0) Requirement already satisfied: pytz>=2020.1 in /Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages (from pandas>=1.3->paddlex<3.4.0,>=3.3.0->paddlex[ocr-core]<3.4.0,>=3.3.0->paddleocr>=2.7.0.3) (2025.2) Requirement already satisfied: tzdata>=2022.7 in /Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages (from pandas>=1.3->paddlex<3.4.0,>=3.3.0->paddlex[ocr-core]<3.4.0,>=3.3.0->paddleocr>=2.7.0.3) (2025.3) Requirement already satisfied: annotated-types>=0.6.0 in /Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages (from pydantic>=2->paddlex<3.4.0,>=3.3.0->paddlex[ocr-core]<3.4.0,>=3.3.0->paddleocr>=2.7.0.3) (0.7.0) Requirement already satisfied: pydantic-core==2.41.5 in /Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages (from pydantic>=2->paddlex<3.4.0,>=3.3.0->paddlex[ocr-core]<3.4.0,>=3.3.0->paddleocr>=2.7.0.3) (2.41.5) Requirement already satisfied: typing-inspection>=0.4.2 in /Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages (from pydantic>=2->paddlex<3.4.0,>=3.3.0->paddlex[ocr-core]<3.4.0,>=3.3.0->paddleocr>=2.7.0.3) (0.4.2) Requirement already satisfied: six>=1.5 in /Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages (from python-dateutil>=2.8.2->pandas>=1.3->paddlex<3.4.0,>=3.3.0->paddlex[ocr-core]<3.4.0,>=3.3.0->paddleocr>=2.7.0.3) (1.17.0) Requirement already satisfied: charset_normalizer<4,>=2 in /Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages (from requests->paddleocr>=2.7.0.3) (3.4.4) Requirement already satisfied: idna<4,>=2.5 in /Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages (from requests->paddleocr>=2.7.0.3) (3.11) Requirement already satisfied: certifi>=2017.4.17 in /Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages (from requests->paddleocr>=2.7.0.3) (2025.11.12) Requirement already satisfied: pycryptodome>=3.8.0 in /Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages (from bce-python-sdk->aistudio-sdk>=0.3.5->paddlex<3.4.0,>=3.3.0->paddlex[ocr-core]<3.4.0,>=3.3.0->paddleocr>=2.7.0.3) (3.23.0) Requirement already satisfied: future>=0.6.0 in /Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages (from bce-python-sdk->aistudio-sdk>=0.3.5->paddlex<3.4.0,>=3.3.0->paddlex[ocr-core]<3.4.0,>=3.3.0->paddleocr>=2.7.0.3) (1.0.0) Requirement already satisfied: fsspec>=2023.5.0 in /Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages (from huggingface-hub->paddlex<3.4.0,>=3.3.0->paddlex[ocr-core]<3.4.0,>=3.3.0->paddleocr>=2.7.0.3) (2025.12.0) Requirement already satisfied: hf-xet<2.0.0,>=1.2.0 in /Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages (from huggingface-hub->paddlex<3.4.0,>=3.3.0->paddlex[ocr-core]<3.4.0,>=3.3.0->paddleocr>=2.7.0.3) (1.2.0) Requirement already satisfied: httpx<1,>=0.23.0 in /Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages (from huggingface-hub->paddlex<3.4.0,>=3.3.0->paddlex[ocr-core]<3.4.0,>=3.3.0->paddleocr>=2.7.0.3) (0.28.1) Requirement already satisfied: shellingham in /Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages (from huggingface-hub->paddlex<3.4.0,>=3.3.0->paddlex[ocr-core]<3.4.0,>=3.3.0->paddleocr>=2.7.0.3) (1.5.4) Requirement already satisfied: typer-slim in /Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages (from huggingface-hub->paddlex<3.4.0,>=3.3.0->paddlex[ocr-core]<3.4.0,>=3.3.0->paddleocr>=2.7.0.3) (0.21.0) Requirement already satisfied: anyio in /Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages (from httpx<1,>=0.23.0->huggingface-hub->paddlex<3.4.0,>=3.3.0->paddlex[ocr-core]<3.4.0,>=3.3.0->paddleocr>=2.7.0.3) (4.12.0) Requirement already satisfied: httpcore==1.* in /Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages (from httpx<1,>=0.23.0->huggingface-hub->paddlex<3.4.0,>=3.3.0->paddlex[ocr-core]<3.4.0,>=3.3.0->paddleocr>=2.7.0.3) (1.0.9) Requirement already satisfied: h11>=0.16 in /Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages (from httpcore==1.*->httpx<1,>=0.23.0->huggingface-hub->paddlex<3.4.0,>=3.3.0->paddlex[ocr-core]<3.4.0,>=3.3.0->paddleocr>=2.7.0.3) (0.16.0) Requirement already satisfied: exceptiongroup>=1.0.2 in /Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages (from anyio->httpx<1,>=0.23.0->huggingface-hub->paddlex<3.4.0,>=3.3.0->paddlex[ocr-core]<3.4.0,>=3.3.0->paddleocr>=2.7.0.3) (1.3.1) Requirement already satisfied: wcwidth in /Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages (from prettytable->paddlex<3.4.0,>=3.3.0->paddlex[ocr-core]<3.4.0,>=3.3.0->paddleocr>=2.7.0.3) (0.2.14) Requirement already satisfied: ruamel.yaml.clib>=0.2.15 in /Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages (from ruamel.yaml->paddlex<3.4.0,>=3.3.0->paddlex[ocr-core]<3.4.0,>=3.3.0->paddleocr>=2.7.0.3) (0.2.15) Downloading paddleocr-3.3.2-py3-none-any.whl (86 kB) Installing collected packages: paddleocr Successfully installed paddleocr-3.3.2 (venv) mac@macdeMacBook-Pro project % python -c "import paddleocr; print(paddleocr.__version__)" Checking connectivity to the model hosters, this may take a while. To bypass this check, set `DISABLE_MODEL_SOURCE_CHECK` to `True`. 3.3.2 (venv) mac@macdeMacBook-Pro project % python ocr_test.py Checking connectivity to the model hosters, this may take a while. To bypass this check, set `DISABLE_MODEL_SOURCE_CHECK` to `True`. Traceback (most recent call last): File "/Users/mac/Projects/GaPinyc/PaddleOCR/Test/project/ocr_test.py", line 12, in <module> ocr = PaddleOCR( File "/Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages/paddleocr/_pipelines/ocr.py", line 163, in __init__ super().__init__(**base_params) File "/Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages/paddleocr/_pipelines/base.py", line 63, in __init__ self._common_args = parse_common_args( File "/Users/mac/Projects/GaPinyc/PaddleOCR/Test/venv/lib/python3.10/site-packages/paddleocr/_common_args.py", line 45, in parse_common_args raise ValueError(f"Unknown argument: {name}") ValueError: Unknown argument: layout_analysis (venv) mac@macdeMacBook-Pro project %
最新发布
12-28
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值