Example: 01-Camera/02-Optical-Flow/absolute-translation.py
# 本作品采用MIT许可证授权。
# 版权所有 (c) 2013-2023 OpenMV LLC。保留所有权利。
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# Absolute Optical Flow Translation
#
# 此示例展示了如何使用您的OpenMV摄像头测量 translation
# in the X and Y direction by comparing the current and a previous
# image against each other. Note that only X and Y translation is
# 已处理 - 此模式下不支持旋转/缩放。
#
# To run this demo effectively please mount your OpenMV Cam on a steady
# 基准点,并缓慢向左、右、上、下移动,
# 观察数值变化。注意,你可以看到位移数值
# 在水平和垂直分辨率的一半范围内波动。
#
# 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
# use a resolution like B64X64 or B64X32 (2x faster).
#
# 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
sensor.reset() # 重置并初始化传感器。
sensor.set_pixformat(sensor.RGB565) # 将像素格式设置为RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.B64X64) # Set frame size to 64x64... (or 64x32)...
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.RGB565)
extra_fb.replace(sensor.snapshot())
while True:
clock.tick() # Track elapsed milliseconds between snapshots().
img = sensor.snapshot() # 拍照并返回图像。
# For this example we never update the old image to measure absolute change.
displacement = extra_fb.find_displacement(img)
# Offset results are noisy without filtering so we drop some accuracy.
sub_pixel_x = int(displacement.x_translation() * 5) / 5.0
sub_pixel_y = int(displacement.y_translation() * 5) / 5.0
if (
displacement.response() > 0.1
): # Below 0.1 or so (YMMV) and the results are just noise.
print(
"{0:+f}x {1:+f}y {2} {3} FPS".format(
sub_pixel_x, sub_pixel_y, displacement.response(), clock.fps()
)
)
else:
print(clock.fps())