Example: 02-Image-Processing/03-Frame-Differencing/in_memory_advanced_frame_differencing.py

# 本作品采用MIT许可证授权。
# 版权所有 (c) 2013-2023 OpenMV LLC。保留所有权利。
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# 高级帧差分示例
#
# 此示例演示了如何使用OpenMV Cam进行帧差分。此
# 示例是高级的,因为它执行背景更新以处理
# 调整场景背景图像。

import sensor
import time

TRIGGER_THRESHOLD = 5

BG_UPDATE_FRAMES = 50  # 混合前的帧数。
BG_UPDATE_BLEND = 128  # 混合程度...([0-255]对应[0.0-1.0]范围)。

sensor.reset()  # 初始化相机传感器。
sensor.set_pixformat(sensor.RGB565)  # 或 sensor.RGB565
sensor.set_framesize(sensor.QVGA)  # 或传感器.QQVGA(或其他规格)
sensor.skip_frames(time=2000)  # 让新设置生效。
sensor.set_auto_whitebal(False)  # 关闭白平衡。
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. However,
# frame differencing doesn't use a lot of the extra space in the frame buffer.
# But, things like AprilTags do and won't work if you do this...
extra_fb = sensor.alloc_extra_fb(sensor.width(), sensor.height(), sensor.RGB565)

print("About to save background image...")
sensor.skip_frames(time=2000)  # 给用户时间准备。
extra_fb.replace(sensor.snapshot())
print("Saved background image - Now frame differencing!")

triggered = False

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

    frame_count += 1
    if frame_count > BG_UPDATE_FRAMES:
        frame_count = 0
        # Blend in new frame. We're doing 255-alpha here because we want to
        # blend the new frame into the background. Not the background into the
        # 新帧 which would be just alpha. Blend replaces each pixel by
        # ((NEW*(alpha))+(OLD*(255-alpha)))/255. So, a low alpha results in
        # low blending of the new image while a high alpha results in high
        # blending of the new image. We need to reverse that for this update.
        img.blend(extra_fb, alpha=(255 - BG_UPDATE_BLEND))
        extra_fb.replace(img)

    # Replace the image with the "abs(NEW-OLD)" frame difference.
    img.difference(extra_fb)

    hist = img.get_histogram()
    # This code below works by comparing the 99th percentile value (e.g. the
    # non-outlier max value against the 90th percentile value (e.g. a non-max
    # value. The difference between the two values will grow as the difference
    # image seems more pixels change.
    diff = hist.get_percentile(0.99).l_value() - hist.get_percentile(0.90).l_value()
    triggered = diff > TRIGGER_THRESHOLD

    print(clock.fps(), triggered)  # 注意:您的 OpenMV Cam 在连接到
    # 计算机时运行速度会减半。断开连接后,FPS 应该会增加。

results matching ""

    No results matching ""