Example: 02-Image-Processing/00-Drawing/image_drawing_advanced.py

# 本作品采用MIT许可证授权。
# 版权所有 (c) 2013-2023 OpenMV LLC。保留所有权利。
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# 绘制图像测试脚本,带反弹效果
#
# 练习绘制图像,使用许多不同的值进行测试

import sensor
import image
import time

sensor.reset()
sensor.set_pixformat(sensor.RGB565)  # or GRAYSCALE...
sensor.set_framesize(sensor.QVGA)  # or QQVGA...
sensor.skip_frames(time=2000)
clock = time.clock()

BOUNCE = True
RESCALE = True

SMALL_IMAGE_SCALE = 3

CYCLE_FORMATS = True
CYCLE_MASK = True

# 当 CYCLE_FORMATS 或 CYCLE_MASK 为 true 时使用
value_mixer = 0

# 小图像的位置
x = 100
y = 50

# 反弹方向
xd = 1
yd = 1

# 小图像缩放
rescale = 1.0
rd = 0.1
max_rescale = 5
min_rescale = rd * 2

# 反弹的边界
xmin = -sensor.width() / SMALL_IMAGE_SCALE - 8
ymin = -sensor.height() / SMALL_IMAGE_SCALE - 8
xmax = sensor.width() + 8
ymax = sensor.height() + 8

while True:
    clock.tick()

    status = ""
    value_mixer = value_mixer + 1

    img = sensor.snapshot()
    # 制作传感器的缩放副本
    small_img = img.scale(x_scale=1.0 / SMALL_IMAGE_SCALE,
                          y_scale=1.0 / SMALL_IMAGE_SCALE,
                          hint=image.AREA, copy=True)

    status = "rgb565 "
    if CYCLE_FORMATS:
        image_format = (value_mixer >> 8) & 3
        # 用于测试不同格式的组合
        if image_format == 1:
            small_img = small_img.to_bitmap(copy=True)
            status = "bitmap "
        if image_format == 2:
            small_img = small_img.to_grayscale(copy=True)
            status = "grayscale "
        if image_format == 3:
            small_img = small_img.to_rgb565(copy=True)
            status = "rgb565 "

    # 更新小图像的位置
    if BOUNCE:
        x = x + xd
        if x < xmin or x > xmax:
            xd = -xd

        y = y + yd
        if y < ymin or y > ymax:
            yd = -yd

    # 更新小图像的缩放比例
    if RESCALE:
        rescale = rescale + rd
        if rescale < min_rescale or rescale > max_rescale:
            rd = -rd

    # 找到图像的中心
    scaled_width = int(small_img.width() * abs(rescale))
    scaled_height = int(small_img.height() * abs(rescale))

    apply_mask = CYCLE_MASK and ((value_mixer >> 9) & 1)
    if apply_mask:
        img.draw_image(
            small_img,
            int(x),
            int(y),
            mask=small_img.to_bitmap(copy=True),
            x_scale=rescale,
            y_scale=rescale,
            alpha=240,
            hint=image.BILINEAR
        )
        status += "alpha:240 "
        status += "+mask "
    else:
        img.draw_image(
            small_img,
            int(x),
            int(y),
            x_scale=rescale,
            y_scale=rescale,
            alpha=128,
            hint=image.BILINEAR
        )
        status += "alpha:128 "

    img.draw_string(8, 0, status, mono_space=False)

    print(clock.fps())

results matching ""

    No results matching ""