十字交叉
图像绘制,就是通过在图片上绘制图形,如矩形、圆形、线条、文字等。图像绘制的作用在于,通过这些图形我们可以直观的看到AI视觉算法的结果和问题,把算法结果通过绘制图像叠加在显示设备上,提高阅读性,方便理解。也可以在图片上进行特殊点标记,有助于数据整理与分析,通过绘图和文字功能,可以直观向用户展现系统状态、统计信息或者反馈其他内容,类似触摸屏的坐标或者对话框。
本节我们介绍图像绘制中的线条。
代码展示
#画十字
import time, os, gc, sys, urandom
from media.display import *
from media.media import *
DISPLAY_MODE = "VIRT"
def main():
# 设置尺寸
width = 640
height = 480
# 根据模式设置显示宽高
if DISPLAY_MODE == "VIRT":
# 使用IDE作为输出
Display.init(Display.VIRT, width = 800, height = 480, fps = 100)
width = 800
height = 480
elif DISPLAY_MODE == "LCD":
# 使用LCD作为显示输出
Display.init(Display.ST7701, width = 800, height = 480, to_ide = True)
width = 800
height = 480
elif DISPLAY_MODE == "HDMI":
# 使用hdmi作为显示输出,设置为1080P
Display.init(Display.LT9611, width = 1920, height = 1080, to_ide = True)
width = 1920
height = 1080
else:
raise ValueError("请选择 'VIRT', 'LCD' 或 'HDMI'")
# 媒体初始化
MediaManager.init()
fps = time.clock()
# 创建绘制图像
img = image.Image(width, height, image.ARGB8888)
try:
while True:
fps.tick()
# 检查是否退出。
os.exitpoint()
img.clear()
for i in range(10):
x = (urandom.getrandbits(30) % (2*img.width())) - (img.width()//2)
y = (urandom.getrandbits(30) % (2*img.height())) - (img.height()//2)
r = (urandom.getrandbits(30) % 127) + 128
g = (urandom.getrandbits(30) % 127) + 128
b = (urandom.getrandbits(30) % 127) + 128
# 箭头 draw_cross(X,Y,颜色,大小,线条宽度)
img.draw_cross(x, y, color = (r, g, b), size = 10, thickness = 2)
# 将结果绘制到屏幕
Display.show_image(img)
print(fps.fps())
time.sleep_ms(100)
except KeyboardInterrupt as e:
print("用户停止: ", e)
except BaseException as e:
print(f"异常: {e}")
# 销毁显示
Display.deinit()
os.exitpoint(os.EXITPOINT_ENABLE_SLEEP)
time.sleep_ms(100)
# 释放媒体缓冲区
MediaManager.deinit()
if __name__ == "__main__":
os.exitpoint(os.EXITPOINT_ENABLE)
main()
例程讲解
draw_cross
draw_cross函数可实现在图像上绘制一个十字交叉。
语法
image.draw_cross(x, y, color, size=5, thickness=1)
参数解释
x, y:交叉点坐标。
color:交叉的颜色。
size:交叉的大小(默认为5)。
thickness:交叉线条的厚度(默认为1)。
效果展示
我们本次展示的十字是利用随机值函数生成的数值,在显示器上生成十字,效果如下图。
【学习k230 - 例程25】图像绘制 - 十字交叉