【CanMV K230】画图,画它个多啦A梦


请添加图片描述

B站视频链接:已做成合集
抖音链接:已做成合集


通过摄像头采集到照片后,我们会进行一些处理,而这时候往往需要一些图形来指示,比如在图片某个位置标记箭头、人脸识别后用矩形框提示等。

本篇重点了解image对象

image对象

构造函数

img=sensor.snapshot()

通过摄像头拍摄方式返回image对象。图像对象是机器视觉操作的基本对象

img=image.Image(path[, copy_to_fb=False])

从 path 中的文件中创建一个新的图像对象。
通过读取图片方式创建image对象。

参数说明
path支持bmp/pgm/ppm/jpg/jpeg格式的图像文件
copy_to_fbcopy_to_fb: 加载大图片。True : 可以加载大图片。False : 不可以加载大图片。

例:img = image.Image(“01Studio.bmp”, copy_to_fb=True)
#表示加载根目录下的01Studio.bmp图片。

img=image.Image(w, h, format)

主动创建一个图像。

参数说明
w图像宽度
h图像高度
format图像格式。部分格式如下:GRAYSCALE : 图像格式;RGB565 : 16bit彩色;(大部分image图像处理函数使用这个格式。)RGB888 : 24bit真彩色

例:img = image.Image(640, 480, image.RGB565) #表示创建一张640x480,
格式为RGB565图像。

使用方法

画线段 image.draw_line

image.draw_line(x0, y0, x1, y1[, color[, thickness=1]])

画线段。

参数说明
x0, y0起始坐标;
x1,y1: 终点坐标;
color颜色;
thickness线条粗细;

例:

#画线段:从 x0, y0 到 x1, y1 坐标的线段,颜色红色,线宽度 2。
img.draw_line(20, 20, 100, 20, color = (255, 0, 0), thickness = 2)

画矩形 image.draw_rectangle

image.draw_rectangle(x, y, w, h[, color[, thickness=1[, fill=False]]])

画矩形。

参数说明
x, y起始坐标;
w宽度;
h高度;
color颜色;
thickness边框粗细;
fill是否填充;、True : 填充。False : 不填充。

例:

#画矩形:绿色不填充。
img.draw_rectangle(150, 20, 100, 30, color = (0, 255, 0), thickness = 2, fill = False)

画圆 image.draw_circle

image.draw_circle(x, y, radius[, color[, thickness=1[, fill=False]]])

画圆。

参数说明
x, y圆心;
radius宽度;
h高度;
color颜色;
thickness:线条粗细;
fill是否填充;
True填充。
False不填充。

例:

#画圆:蓝色不填充。
img.draw_circle(60, 120, 30, color = (0, 0, 255), thickness = 2, fill = False)

绘制椭圆 image.draw_ellipse

image.draw_ellipse(cx, cy, rx, ry, rotation[, color[, thickness=1[, fill=False]]])
参数说明
cx椭圆中心点的x坐标。
cy椭圆中心点的y坐标。
rx椭圆的半径在x轴方向上的长度。
ry椭圆的半径在y轴方向上的长度。
rotation椭圆的旋转角度,以度为单位。0度表示椭圆的主轴与x轴平行。
color椭圆的边框颜色。这通常是一个表示颜色的元组,如 (R, G, B) 或 (R, G, B, A)。
thickness=1椭圆边框的宽度。如果设置为1,将绘制一个线框椭圆;如果设置为-1,则表示填充椭圆。
fill=False一个布尔值,表示椭圆是否应该被填充。如果为True,椭圆将被填充为指定的颜色。

绘制椭圆

参数说明

画箭头 image.draw_arrow

image.draw_arrow(x0, y0, x1, y1[, color[, size,[thickness=1]]])

画箭头。
例:

参数说明
x0, y0起始坐标;
x1, y1终点坐标;
color颜色;
size箭头位置大小;
thickness线条粗细;
#画箭头:白色。
 img.draw_arrow(150, 120, 250, 120, color = (255, 255, 255), size = 20, thickness = 2)

画十字交叉 image.draw_cross

image.draw_cross(x, y[, color[, size=5[, thickness=1]]])

画十字交叉。

参数说明
x, y交叉中点坐标;
color颜色;
size大小;
thickness线条粗细;
  #画十字交叉。
    img.draw_cross(60, 200, color = (255, 255, 255), size = 20, thickness = 2)

写字符 image.draw_string

image.draw_string(x, y, text[, color[, scale=1[,mono_space=True]]]])

写字符。

参数说明
x, y起始坐标;
text字符内容;
color颜色;
scale字体大小;
mono_space强制间隔;True : 有间隔。False : 无间隔。
#写字符。
img.draw_string(150, 200, "Hello 01Studio!", color = (255, 255, 255), scale = 4, mono_space = False)

写字符,支持中文 image.draw_string_advanced

image.draw_string_advanced(x, y, char_size,str,[color, font])

写字符,支持中文。

参数说明
x, y起始坐标;
char_size字体大小;
text字符内容;
color颜色;
font字体类型。
#写字符,支持中文。
img.draw_string_advanced(150, 180, 30, "Hello 01Studio", color = (255, 255, 255))
img.draw_string_advanced(40, 300, 30, "人生苦短, 我用Python", color = (255, 255, 255))

熟悉了image对象的画图功能后,我们尝试在摄像头采集到的画面依次画出线段、矩形、圆形、箭头、十字交叉和字符。具体编程思路如下:

绘制哆啦A梦

'''
实验名称:绘制多啦A梦
实验平台:01Studio CanMV K230
说明:使用给的image对象绘制
测试人:咸鱼浆 2024年8月28日21:28:54
'''


import time, os, sys

from media.sensor import * #导入sensor模块,使用摄像头相关接口
from media.display import * #导入display模块,使用display相关接口
from media.media import * #导入media模块,使用meida相关接口

try:

    sensor = Sensor() #构建摄像头对象
    sensor.reset() #复位和初始化摄像头
    sensor.set_framesize(Sensor.FHD) #设置帧大小FHD(1920x1080),默认通道0
    sensor.set_pixformat(Sensor.RGB565) #设置输出图像格式,默认通道0

    #使用IDE缓冲区输出图像,显示尺寸和sensor配置一致。
    Display.init(Display.LT9611, to_ide=True)

    MediaManager.init() #初始化media资源管理器

    sensor.run() #启动sensor


    while True:
        os.exitpoint() #检测IDE中断

        img = sensor.snapshot() #拍摄一张图
        # 咸鱼菌绘制多啦A梦,看着玩
        #img.draw_string(40, 8, "xianyujiang", scale = 2.0, color = (0,0,0), thickness = 2)
        # 大圆脸
        img.draw_string_advanced(65, 8, 30, "咸鱼浆", color = (255, 255, 255))
        img.draw_ellipse(120, 120, 80, 74,0, 0, 360, color=(0,0,250),
                                thickness = 2,fill = True)
        img.draw_ellipse(120, 138, 60, 55,0, 0, 360, color=(255,255,255),
                                thickness = 2,fill = True)
        # 眼睛外框
        img.draw_ellipse(100, 90,18, 25,0, 0, 360, color=(255,255,255),
                                thickness = 2)
        img.draw_ellipse(100, 90,17, 24,0, 0, 360, color=(255,255,255),
                                thickness = 2)
        img.draw_ellipse(140, 90,18, 25,0, 0, 360, color=(255,255,250),
                                thickness = 2)
        img.draw_ellipse(140, 90,17, 24,0, 0, 360, color=(255,255,255),
                                thickness = 2)
        # 眼珠
        img.draw_ellipse(110, 92,8, 8,0, 0, 360, color=(0,0,0),
                                thickness = 2)
        img.draw_circle(115, 92, 3, color = (255, 250, 255), thickness = 2)

        img.draw_ellipse(128, 92,8, 8,0, 0, 360, color=(0,0,0),
                                thickness = 2)
        img.draw_circle(125, 92, 3, color = (255, 250, 255), thickness = 2)

        # 鼻子
        img.draw_ellipse(120, 118,8, 9,0, 0, 360, color=(255,0,0),
                                thickness = 2)
        img.draw_circle(125, 118, 3, color = (255, 250, 255), thickness = 2)
        img.draw_line(120, 128, 120, 160, color = (0, 0, 0),
                                thickness = 1)
        # 嘴
        img.draw_line(90, 170, 150, 170, color = (0, 0, 0),
                                thickness = 1)

        # 左胡子
        img.draw_line(50, 110, 100, 130, color = (0, 0, 0),
                                thickness = 1)
        img.draw_line(46, 111, 50,110, color = (0, 0, 0),
                                thickness = 1)
        img.draw_line(50, 120, 100, 140, color = (0, 0, 0),
                                thickness = 1)
        img.draw_line(46, 121, 50,120, color = (0, 0, 0),
                                thickness = 1)
        img.draw_line(50, 130, 100, 150, color = (0, 0, 0),
                                thickness = 1)
        img.draw_line(46, 131, 50,130, color = (0, 0, 0),
                                thickness = 1)
        # 右胡子
        img.draw_line(140, 130, 190, 110, color = (0, 0, 0),
                                thickness = 1)
        img.draw_line(190, 110, 194,111, color = (0, 0, 0),
                                thickness = 1)
        img.draw_line(140, 140, 190, 120, color = (0, 0, 0),
                                thickness = 1)
        img.draw_line(190, 120, 194,121, color = (0, 0, 0),
                                thickness = 1)
        img.draw_line(140, 150, 190, 130, color = (0, 0, 0),
                                thickness = 1)
        img.draw_line(190, 130, 194,131, color = (0, 0, 0),
                                thickness = 1)



        Display.show_image(img) #显示图片




###################
# IDE中断释放资源代码
###################
except KeyboardInterrupt as e:
    print("user stop: ", e)
except BaseException as e:
    print(f"Exception {e}")
finally:
    # sensor stop run
    if isinstance(sensor, Sensor):
        sensor.stop()
    # deinit display
    Display.deinit()
    os.exitpoint(os.EXITPOINT_ENABLE_SLEEP)
    time.sleep_ms(100)
    # release media buffer
    MediaManager.deinit()


在这里插入图片描述

官方例程

'''
实验名称:画图
实验平台:01Studio CanMV K230
说明:画各种图形和写字符, 通过IDE和LCD显示。
'''

import time, os, sys

from media.sensor import * #导入sensor模块,使用摄像头相关接口
from media.display import * #导入display模块,使用display相关接口
from media.media import * #导入media模块,使用meida相关接口

try:

    sensor = Sensor() #构建摄像头对象
    sensor.reset() #复位和初始化摄像头
    sensor.set_framesize(width=800, height=480) #设置帧大小VGA,默认通道0
    sensor.set_pixformat(Sensor.RGB565) #设置输出图像格式,默认通道0

    #Display.init(Display.VIRT, sensor.width(), sensor.height()) #使用IDE缓冲区输出图像
    Display.init(Display.ST7701,to_ide=True) #通过01Studio 3.5寸mipi显示屏显示图像
    MediaManager.init() #初始化media资源管理器

    sensor.run() #启动sensor

    clock = time.clock()

    while True:


        os.exitpoint() #检测IDE中断

        ################
        ## 这里编写代码 ##
        ################
        clock.tick()

        img = sensor.snapshot()
        # 画线段:从 x0, y0 到 x1, y1 坐标的线段,颜色红色,线宽度 2。
        img.draw_line(20, 20, 100, 20, color = (255, 0, 0), thickness = 2)

        #画矩形:绿色不填充。
        img.draw_rectangle(150, 20, 100, 30, color = (0, 255, 0), thickness = 2, fill = False)

        #画圆:蓝色不填充。
        img.draw_circle(60, 120, 30, color = (0, 0, 255), thickness = 2, fill = False)

        #画箭头:白色。
        img.draw_arrow(150, 120, 250, 120, color = (255, 255, 255), size = 20, thickness = 2)

        #画十字交叉。
        img.draw_cross(60, 200, color = (255, 255, 255), size = 20, thickness = 2)

        #写字符。
        #img.draw_string(150, 200, "Hello 01Studio!", color = (255, 255, 255), scale = 4, mono_space = False)

        #写字符,支持中文。
        img.draw_string_advanced(150, 180, 30, "Hello 01Studio", color = (255, 255, 255))
        img.draw_string_advanced(40, 300, 30, "人生苦短, 我用Python", color = (255, 255, 255))

        Display.show_image(img)

        print(clock.fps()) #打印FPS


###################
# IDE中断释放资源代码
###################
except KeyboardInterrupt as e:
    print("user stop: ", e)
except BaseException as e:
    print(f"Exception {e}")
finally:
    # sensor stop run
    if isinstance(sensor, Sensor):
        sensor.stop()
    # deinit display
    Display.deinit()
    os.exitpoint(os.EXITPOINT_ENABLE_SLEEP)
    time.sleep_ms(100)
    # release media buffer
    MediaManager.deinit()

在这里插入图片描述

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

咸鱼桨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值