import numpy as np
import cv2

# 创建一个240x100的黑色背景图像
image = np.zeros((240, 100, 3), dtype=np.uint8)

# 定义圆角矩形的左上角和右下角坐标
x1, y1 = 20, 20
x2, y2 = 220, 100

# 定义圆角的半径
radius = 20

# 绘制圆角矩形
cv2.rectangle(image, (x1, y1), (x2, y2), (255, 0, 0), -1)  # 使用红色

# 绘制四个角的圆弧
cv2.ellipse(image, (x1 + radius, y1 + radius), (radius, radius), 180, 0, 90, (0, 255, 0), -1)
cv2.ellipse(image, (x2 - radius, y1 + radius), (radius, radius), 270, 0, 90, (0, 255, 0), -1)
cv2.ellipse(image, (x1 + radius, y2 - radius), (radius, radius), 90, 0, 90, (0, 255, 0), -1)
cv2.ellipse(image, (x2 - radius, y2 - radius), (radius, radius), 0, 0, 90, (0, 255, 0), -1)

# 显示图像
cv2.imshow('Rounded Rectangle', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
 

更多推荐