import cv2
import numpy as np
# 定义输入的点集合
points = [(10, 5), (30, 40), (60, 70), (80, 90)]
# 将点转换为numpy数组格式
pts_np = np.array(points)
# 创建空白图像并在上面画出点集合
image = np.zeros((300, 300, 3), dtype=np.uint8)
for point in points:
x, y = point[0], point[1]
image[(y - 1):(y + 1), (x - 1):(x + 1)] = [255, 255, 255] # 设置颜色为白色
# 计算最小外接正矩形(bounding rectangle)
x, y, w, h = cv2.boundingRect(pts_np)
cv2.rectangle(image, (x, y), (x + w, y + h), (255, 0, 0), 2)
# 显示原始图像及其边界框
cv2.imshow('Original Image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
[python]利用opencv求点集包围的最小外接正矩形
最新推荐文章于 2024-07-04 06:00:00 发布