
使用 left、top、right 和 bottom 坐标表示的矩形内绘制圆圈
如何使用 left、top、right 和 bottom 坐标来表示矩形,并使用 opencv 的 cv2.circle() 函数在这个矩形内绘制圆圈呢?
解决方案:
首先,导入必要的库:
import cv2 import numpy as np
接下来,创建一个填充白色(255)的黑色图像:
img = np.ones((1000, 1000), np.uint8) * 255
接着,使用 rectangle() 函数在图像上绘制一个矩形:
left, top, right, bottom = 530, 836, 685, 885 cv2.rectangle(img, (left, top), (right, bottom), 0)
最后,使用两个嵌套的 for 循环遍历矩形内的每个像素点,并使用 circle() 函数在这些像素点上绘制圆圈:
for y in range(top * 2, bottom * 2 + 1, bottom - top):
for x in range(left * 2, right * 2 + 1, right - left):
cv2.circle(img, (x, y), 8, 0, cv2.filled, cv2.line_aa, 1)将结果显示在图像窗口中:
cv2.imshow('img', img)
cv2.waitKey()最终,你将看到一个带有 nine个黑色圆圈的白色矩形。每个圆圈都位于矩形内的九个特定点:左上、上中、右上、左中、中心、右中、左下、下中和右下。
以上就是如何使用 left、top、right 和 bottom 坐标在矩形内绘制圆圈?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号