绘制十字准心的draw_cross()方法
- image.draw_cross(x, y[, color[, size=5[, thickness=1]]])
复制代码
在图像上绘制一个十字标记。参数可以分别传入 x, y,也可以作为元组 (x, y) 一起传递。
color: 表示颜色的 RGB888 元组,适用于灰度或 RGB565 图像,默认为白色。对于灰度图像,还可以传递像素值(范围 0-255);
对于 RGB565 图像,可以传递字节翻转的 RGB565 值。
size: 控制十字标记的大小,默认为 5。
thickness: 控制十字线条的像素宽度,默认为 1。
该方法返回图像对象,允许通过链式调用其他方法。
不支持压缩图像和 Bayer 格式图像。
测试实验代码
- #【花雕动手做】CanMV K230 AI视觉识别模块之使用draw_cross()方法绘制十字准心
-
- # Import required modules
- # 导入所需的模块
- import time, os, urandom, sys, math
-
- # Import display and media related modules
- # 导入显示和媒体相关模块
- from media.display import *
- from media.media import *
-
- # Define display resolution constants
- # 定义显示分辨率常量
- DISPLAY_WIDTH = 640 # 显示宽度:640像素
- DISPLAY_HEIGHT = 480 # 显示高度:480像素
-
- def display_test():
- """
- Function to test display functionality
- 测试显示功能的函数
- 主要功能:在屏幕上绘制一个精美的十字准心图案,包含中心大十字和多个环绕小十字
- """
-
- # Create main background image with white color
- # 创建白色背景的主图像
- # ARGB8888格式:每个像素32位(Alpha透明通道+RGB各8位)
- img = image.Image(DISPLAY_WIDTH, DISPLAY_HEIGHT, image.ARGB8888)
- img.clear() # 清空图像缓冲区
- # 绘制白色填充矩形作为背景,fill=True表示填充
- img.draw_rectangle(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT, color=(255,255,255), fill=True)
-
- # Initialize display with ST7701 driver
- # 使用ST7701驱动初始化显示器
- # ST7701是常见的LCD屏幕驱动芯片
- # to_ide=True表示将显示输出同时发送到IDE和硬件屏幕
- Display.init(Display.ST7701, width=DISPLAY_WIDTH, height=DISPLAY_HEIGHT, to_ide=True)
-
- # Initialize media manager
- # 初始化媒体管理器 - 负责管理摄像头、显示等媒体资源
- MediaManager.init()
-
- try:
- # ========== 第一层:中心大十字 ==========
- # 在屏幕中心(320,240)位置绘制一个大十字准心
- # 参数说明:
- # 320, 240: 十字中心坐标(屏幕中心)
- # color=(0, 191, 255): 天蓝色 (RGB值)
- # size=40: 十字大小,指单臂长度(像素)
- # thickness=3: 线宽3像素
- img.draw_cross(320, 240, color=(0, 191, 255), size=40, thickness=3)
-
- # ========== 第二层:内圈小十字环绕 ==========
- # 在半径为50像素的圆周上均匀分布8个小十字
- for i in range(8):
- # 计算当前小十字的角度位置(0-360度均匀分布)
- angle = i * (360 / 8) # 每个十字间隔45度
-
- # 使用三角函数计算小十字的坐标位置
- # math.radians()将角度转换为弧度
- # math.cos()和math.sin()计算余弦和正弦值
- x = int(320 + 50 * math.cos(math.radians(angle))) # x坐标 = 中心x + 半径*cos(角度)
- y = int(240 + 50 * math.sin(math.radians(angle))) # y坐标 = 中心y + 半径*sin(角度)
-
- # 绘制内圈小十字
- # color=(135, 206, 235): 浅天蓝色,比中心十字稍浅
- # size=15: 大小15像素
- # thickness=2: 线宽2像素
- img.draw_cross(x, y, color=(135, 206, 235), size=15, thickness=2)
-
- # ========== 第三层:外圈更小的十字 ==========
- # 在半径为80像素的圆周上均匀分布12个更小的十字
- for i in range(12):
- angle = i * (360 / 12) # 每个十字间隔30度
-
- # 计算外圈十字坐标(半径80像素)
- x = int(320 + 80 * math.cos(math.radians(angle)))
- y = int(240 + 80 * math.sin(math.radians(angle)))
-
- # 绘制外圈小十字
- # color=(173, 216, 230): 更浅的天蓝色
- # size=10: 大小10像素
- # thickness=1: 线宽1像素(最细)
- img.draw_cross(x, y, color=(173, 216, 230), size=10, thickness=1)
-
- # ========== 第四层:四个角的装饰性十字 ==========
- # 在屏幕四个角落附近绘制中等大小的装饰十字
- # 左上角十字 (240, 140)
- img.draw_cross(240, 140, color=(0, 191, 255), size=25, thickness=2)
- # 右上角十字 (400, 140)
- img.draw_cross(400, 140, color=(0, 191, 255), size=25, thickness=2)
- # 左下角十字 (240, 340)
- img.draw_cross(240, 340, color=(0, 191, 255), size=25, thickness=2)
- # 右下角十字 (400, 340)
- img.draw_cross(400, 340, color=(0, 191, 255), size=25, thickness=2)
-
- # ========== 第五层:中心点缀小十字 ==========
- # 在中心大十字上面再绘制一个更小的十字,增加层次感
- img.draw_cross(320, 240, color=(173, 216, 230), size=8, thickness=1)
-
- # Update display with background image
- # 更新显示背景图像 - 将绘制好的十字准心图案显示在屏幕上
- Display.show_image(img)
-
- # 主循环保持显示
- while True:
- time.sleep(2) # 每2秒循环一次,保持程序运行
-
- except KeyboardInterrupt as e:
- # 捕获键盘中断(如Ctrl+C),优雅退出
- print("user stop: ", e)
- except BaseException as e:
- # 捕获其他所有异常,防止程序崩溃
- print(f"Exception {e}")
-
- # Cleanup and deinitialize display
- # 清理并反初始化显示器
- Display.deinit()
- # 启用睡眠退出点,允许系统进入低功耗模式
- os.exitpoint(os.EXITPOINT_ENABLE_SLEEP)
- time.sleep_ms(100) # 短暂延时确保资源释放完成
-
- # Release media resources
- # 释放媒体资源
- MediaManager.deinit()
-
- if __name__ == "__main__":
- # Enable exit points and run display test
- # 启用退出点并运行显示测试
- # EXITPOINT_ENABLE允许通过IDE停止程序执行
- os.exitpoint(os.EXITPOINT_ENABLE)
- display_test() # 调用显示测试函数
复制代码
|