OpenCV的绘图函数,实力绘画篮球场

关键函数:cv2.line(),cv2.circle(),cv2.rectangle(),cv2.ellipse(),cv2.putText() 等。

绘制几何形状

c 复制代码
import cv2 as cv
import numpy as np
'''
cv.rectangle(),cv.circle(),cv.line(),cv.putText()   分别是绘制矩形、圆、直线和文字。
cv.rectangle(img, (50, 50), (400, 400), (255, 255, 0), 4)
参数:图像对象,起始点,结束点,颜色,线粗细
cv.circle(img, (200, 200), 100, (255, 0, 0), -1, 8, 0)
参数:图像,圆心,半径,颜色,线粗细
cv.line(img, (100, 100), (400, 400), (0, 255, 0), 8)
参数:图像,起始点,结束点,颜色,线粗细
cv.putText(img, 'OpenCV', org, font, fontScale, color, thickness, cv.LINE_AA)
参数:图像,字符串,位置点,字体,字的大小,颜色,线粗细,cv.LINE_AA

'''
img = np.zeros((800, 800, 3))

temp = np.copy(img)

# 绘制矩形
cv.rectangle(img, (100, 100), (400, 400), (255, 0, 0), 10)
# 绘制圆形
cv.circle(img, (250, 250), 100, (0, 0, 255), -1, 8, 0)
# 绘制直线
cv.line(img, (100, 100), (400, 400), (0, 255, 0), 8)
# 写文字
font = cv.FONT_HERSHEY_SIMPLEX
org = (0, 500)
fontScale = 4
color = (255, 255, 255) #白色  #color = (0, 0, 0)
thickness = 2
img = cv.putText(img, 'OpenCV', org, font, fontScale, color, thickness, cv.LINE_AA)


cv.imshow('123', img)
cv.waitKey(0)

绘制OpenCV 的伪图标

c 复制代码
import cv2 as cv
import numpy as np
img = np.ones((840, 840, 3), np.uint8) * 0    # img = np.ones((740, 600, 3), np.unit8)*255    unit8 --> uint8

# 第一步 扇形 - 圆:
cv.ellipse(img, (420, 200), (140, 140), 120, 0, 300, (0, 0, 255), -1)
cv.circle(img, (420, 200), 55, (0, 0, 0), -1)  # cv.circle(img, (300, 140), 55, (25, 255, 255), -1)

cv.ellipse(img, (260, 480), (140, 140), 0, 0, 300, (0, 255, 0), -1)
cv.circle(img, (260, 480), 55, (0, 0, 0), -1)

cv.ellipse(img, (580, 480), (140, 140), 240, 0, 300, (255, 0, 0), -1)
cv.circle(img, (580, 480), 55, (0, 0, 0), -1)

# 向图像添加文本
font = cv.FONT_HERSHEY_COMPLEX
# 图像,文字,位置,字体  字体大小 ,颜色  线条 ()
cv.putText(img, 'OpenCV', (100, 780), font, 5, (255, 255, 255), 10, cv.LINE_AA)


cv.namedWindow('image', 0)
cv.imshow('image', img)
cv.waitKey(0)
cv.destroyAllWindows()

'''plt.imshow(img)
plt.xticks([]), plt.yticks([])  # 隐藏 x 轴和 y 轴上的刻度值
plt.show()      # 按退出不起作用'''

效果:

绘制篮球场

先展示效果

代码

c 复制代码
import cv2
import numpy as np


def DrawBorder(img, length, width, border, color, line_wide=2, line_type=cv2.LINE_AA):
    cv2.rectangle(img, (border, border), (border + length, border + width), color, line_wide, line_type)


def DrawMidline(img, length, width, border, radius, color, line_wide=2, line_type=cv2.LINE_AA):
    cv2.line(img, (border + length // 2, border), (border + length // 2, border + width), color, line_wide, line_type)
    cv2.circle(img, (border + length // 2, border + width // 2), radius, color, line_wide, line_type)


def DrawThreepointline(img, length, width, border, border_3pl, radius, color, line_wide=2, line_type=cv2.LINE_AA):
    '''
    border_3pl: 三分线半圆圆心与边线距离
    radius: 三分线半圆半径
    '''
    cv2.ellipse(img, (border + border_3pl, border + width // 2), (radius, radius), 0, -90, 90, color, line_wide,
                line_type)
    cv2.line(img, (border, border + width // 2 - radius), (border + border_3pl, border + width // 2 - radius), color,
             line_wide, line_type)
    cv2.line(img, (border, border + width // 2 + radius), (border + border_3pl, border + width // 2 + radius), color,
             line_wide, line_type)

    cv2.ellipse(img, (border + length - border_3pl, border + width // 2), (radius, radius), 180, -90, 90, color,
                line_wide, line_type)
    cv2.line(img, (border + length - border_3pl, border + width // 2 - radius),
             (border + length, border + width // 2 - radius), color, line_wide, line_type)
    cv2.line(img, (border + length - border_3pl, border + width // 2 + radius),
             (border + length, border + width // 2 + radius), color, line_wide, line_type)


def Draw3szone(img, length, width, border, z_length, z_width, radius, color, zone_type=1, line_wide=2,
               line_type=cv2.LINE_AA):
    '''
    zone_type: 1矩形、2梯形
    '''
    if zone_type == 1:
        cv2.rectangle(img, (border, border + width // 2 - z_width // 2),
                      (border + z_length, border + width // 2 + z_width // 2), color, line_wide, line_type)
        cv2.ellipse(img, (border + z_length, border + width // 2), (radius, radius), 0, -90, 90, color, line_wide,
                    line_type)

        cv2.rectangle(img, (border + length - z_length, border + width // 2 - z_width // 2),
                      (border + length, border + width // 2 + z_width // 2), color, line_wide, line_type)
        cv2.ellipse(img, (border + length - z_length, border + width // 2), (radius, radius), 180, -90, 90, color,
                    line_wide, line_type)
    elif zone_type == 2:
        rect = np.array([[[border, border + width // 2 - z_width // 2],
                          [border + z_length, border + width // 2 - radius],
                          [border + z_length, border + width // 2 + radius],
                          [border, border + width // 2 + z_width // 2]]], np.int32)
        cv2.polylines(img, rect, False, color, line_wide, line_type)
        cv2.circle(img, (border + z_length, border + width // 2), radius, color, line_wide, line_type)

        rect = np.array([[[border + length, border + width // 2 - z_width // 2],
                          [border + length - z_length, border + width // 2 - radius],
                          [border + length - z_length, border + width // 2 + radius],
                          [border + length, border + width // 2 + z_width // 2]]], np.int32)
        cv2.polylines(img, rect, False, color, line_wide, line_type)
        cv2.circle(img, (border + length - z_length, border + width // 2), radius, color, line_wide, line_type)


white = (255, 255, 255)
black = (0, 0, 0)
blue = (255, 0, 0)
green = (0, 255, 0)
red = (0, 0, 255)

scale = 100
line_color = white
background_color = black

# 球场各参数
border = int(2 * scale)  # 边界
length = int(28 * scale)  # 场地长
width = int(15 * scale)  # 场地宽
radius = int(1.8 * scale)  # 圆圈半径
# 三分线
radius_3pl = int(6.75 * scale)  # 三分线圆圈半径
border_3pl = int(1.57 * scale)  # 三分线圆心与边界距离
# 三秒区
type_3szone = 1  # 三秒区类型, 1为矩形, 2为梯形
length_3szone = int(5.8 * scale)  # 矩形/梯形长
width_3szone = int(4.9 * scale)  # 矩形/梯形宽

img = (background_color * np.ones((width + border * 2, length + border * 2, 3))).astype(np.uint8)

DrawBorder(img, length, width, border, line_color)
DrawMidline(img, length, width, border, radius, line_color)
DrawThreepointline(img, length, width, border, border_3pl, radius_3pl, line_color)
Draw3szone(img, length, width, border, length_3szone, width_3szone, radius, line_color, type_3szone)

# 显示
cv2.namedWindow('test', 0)
cv2.resizeWindow('test', int(28 * 40 + 2 * 40), int(15 * 40 + 2 * 40))
cv2.imshow('test', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

# 存储
# cv2.imwrite("basketball_court_01.jpg", img)
相关推荐
狂炫冰美式17 小时前
3天,1人,从0到付费产品:AI时代个人开发者的生存指南
前端·人工智能·后端
LCG元17 小时前
垂直Agent才是未来:详解让大模型"专业对口"的三大核心技术
人工智能
我不是QI18 小时前
周志华《机器学习—西瓜书》二
人工智能·安全·机器学习
操练起来18 小时前
【昇腾CANN训练营·第八期】Ascend C生态兼容:基于PyTorch Adapter的自定义算子注册与自动微分实现
人工智能·pytorch·acl·昇腾·cann
KG_LLM图谱增强大模型18 小时前
[500页电子书]构建自主AI Agent系统的蓝图:谷歌重磅发布智能体设计模式指南
人工智能·大模型·知识图谱·智能体·知识图谱增强大模型·agenticai
声网18 小时前
活动推荐丨「实时互动 × 对话式 AI」主题有奖征文
大数据·人工智能·实时互动
caiyueloveclamp18 小时前
【功能介绍03】ChatPPT好不好用?如何用?用户操作手册来啦!——【AI溯源篇】
人工智能·信息可视化·powerpoint·ai生成ppt·aippt
q***484118 小时前
Vanna AI:告别代码,用自然语言轻松查询数据库,领先的RAG2SQL技术让结果更智能、更精准!
人工智能·microsoft
LCG元18 小时前
告别空谈!手把手教你用LangChain构建"能干活"的垂直领域AI Agent
人工智能
想你依然心痛19 小时前
视界无界:基于Rokid眼镜的AI商务同传系统开发与实践
人工智能·智能硬件·rokid·ai眼镜·ar技术