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)
相关推荐
Ai智享9 分钟前
3d系统误差分析
数码相机·计算机视觉·3d
Ai智享14 分钟前
高反光表面三维视觉测量方法
计算机视觉
Logrus IT26 分钟前
俄语画外音的特点
人工智能·语音识别
浊酒南街44 分钟前
SVM模型(理论知识2)
人工智能·机器学习·支持向量机
Fuweizn1 小时前
案例|富唯智能复合机器人CNC柔性上下料
人工智能·机器人·自动化
KeyPan1 小时前
【机器学习:三十三(二)、支持向量机(SVM)的核函数:概念、类型与应用】
人工智能·神经网络·算法·机器学习·支持向量机·数据挖掘
Robot2511 小时前
协作机器人公司切入人形机器人赛道,大有可为!
人工智能·科技·机器人·自动驾驶
机器学习小小白1 小时前
【机器学习实战】kaggle 欺诈检测---使用生成对抗网络(GAN)解决欺诈数据中正负样本极度不平衡问题
人工智能·python·深度学习·神经网络·机器学习·生成对抗网络·gan
沉睡的小灰1 小时前
What can I say? Mamba 环境配置教程与攻略
人工智能·深度学习·mamba
beyond阿亮1 小时前
llama.cpp编译和运行 API调用
c++·人工智能·ai·llama