关键函数:cv2.line(),cv2.circle(),cv2.rectangle(),cv2.ellipse(),cv2.putText() 等。
绘制几何形状
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 的伪图标
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() # 按退出不起作用'''
效果:
绘制篮球场
先展示效果
文章来源:https://www.toymoban.com/news/detail-705077.html
代码文章来源地址https://www.toymoban.com/news/detail-705077.html
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)
到了这里,关于OpenCV的绘图函数,实力绘画篮球场的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!