Example: 01-Camera/02-Optical-Flow/image-patches-absolute-rotation-scale.py

# 本作品采用MIT许可证授权。
# 版权所有 (c) 2013-2023 OpenMV LLC。保留所有权利。
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# 图像区块绝对光流旋转/缩放
#
# 此示例展示了如何使用您的OpenMV摄像头测量
# rotation/scale by comparing the current and a previous
# image against each other. Note that only rotation/scale is
# handled - not X and Y translation in this mode.
#
# 然而,此示例不仅限于对整个图像
# 一次性进行光流处理。而是通过分组处理图像中的像素
# 来分解流程。这会生成一个“新”的结果图像。
#
# 注意:表面需要具备某种“边缘”特征,
# 算法才能正常工作。无特征的表面会产生疯狂的结果。
#
# To run this demo effectively please mount your OpenMV Cam on a steady
# base and SLOWLY rotate the camera around the lens and move the camera
# forward/backwards to see the numbers change.
# I.e. Z direction changes only.
#
# NOTE You have to use a small power of 2 resolution when using
# find_displacement(). This is because the algorithm is powered by
# something called phase correlation which does the image comparison
# using FFTs. A non-power of 2 resolution requires padding to a power
# of 2 which reduces the usefulness of the algorithm results. Please
# 使用像B128X128或B128X64(快2倍)这样的分辨率。
#
# Your OpenMV Cam supports power of 2 resolutions of 64x32, 64x64,
# 128x64, and 128x128. If you want a resolution of 32x32 you can create
# it by doing "img.scale(x_scale=0.5, y_scale=0.5, hint=image.AREA)" on a 64x64 image.

import sensor
import time
import math

BLOCK_W = 16  # pow2
BLOCK_H = 16  # pow2

sensor.reset()  # 重置并初始化传感器。
sensor.set_pixformat(sensor.GRAYSCALE)  # 将像素格式设置为灰度(或RGB565)
sensor.set_framesize(sensor.B128X128)  # 将帧大小设置为128x128...(或128x64)...
sensor.skip_frames(time=2000)  # 等待设置生效。
clock = time.clock()  # 创建一个时钟对象来跟踪FPS。

# Take from the main frame buffer's RAM to allocate a second frame buffer.
# There's a lot more RAM in the frame buffer than in the MicroPython heap.
# However, after doing this you have a lot less RAM for some algorithms...
# So, be aware that it's a lot easier to get out of RAM issues now.
extra_fb = sensor.alloc_extra_fb(sensor.width(), sensor.height(), sensor.GRAYSCALE)
extra_fb.replace(sensor.snapshot())

while True:
    clock.tick()  # Track elapsed milliseconds between snapshots().
    img = sensor.snapshot()  # 拍照并返回图像。

    for y in range(0, sensor.height(), BLOCK_H):
        for x in range(0, sensor.width(), BLOCK_W):
            # For this example we never update the old image to measure absolute change.
            displacement = extra_fb.find_displacement(
                img,
                logpolar=True,
                roi=(x, y, BLOCK_W, BLOCK_H),
                template_roi=(x, y, BLOCK_W, BLOCK_H),
            )

            # Below 0.1 or so (YMMV) and the results are just noise.
            if displacement.response() > 0.1:
                rotation_change = displacement.rotation()
                zoom_amount = displacement.scale()
                pixel_x = (
                    x
                    + (BLOCK_W // 2)
                    + int(math.sin(rotation_change) * zoom_amount * (BLOCK_W // 4))
                )
                pixel_y = (
                    y
                    + (BLOCK_H // 2)
                    + int(math.cos(rotation_change) * zoom_amount * (BLOCK_H // 4))
                )
                img.draw_line(
                    (x + BLOCK_W // 2, y + BLOCK_H // 2, pixel_x, pixel_y), color=255
                )
            else:
                img.draw_line(
                    (
                        x + BLOCK_W // 2,
                        y + BLOCK_H // 2,
                        x + BLOCK_W // 2,
                        y + BLOCK_H // 2,
                    ),
                    color=0,
                )

    print(clock.fps())

results matching ""

    No results matching ""