Example: 02-Image-Processing/03-Frame-Differencing/in_memory_structural_similarity.py
# 本作品采用MIT许可证授权。
# 版权所有 (c) 2013-2023 OpenMV LLC。保留所有权利。
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# Structural Similarity (SSIM) Example
#
# This example shows off how to use the SSIM algorithm on your OpenMV Cam
# to detect differences between two images. The SSIM algorithm compares
# 8x8 blocks of pixels between two images to determine a similarity
# score between two images.
import sensor
import time
# The image has likely changed if the sim.min() is lower than this.
MIN_TRIGGER_THRESHOLD = -0.4
sensor.reset() # 初始化相机传感器。
sensor.set_pixformat(sensor.RGB565) # 或 sensor.GRAYSCALE
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!")
while True:
clock.tick() # Track elapsed milliseconds between snapshots().
img = sensor.snapshot() # 拍照并返回图像。
sim = img.get_similarity(extra_fb)
change = "- Change -" if sim.min() < MIN_TRIGGER_THRESHOLD else "- No Change -"
print(clock.fps(), change, sim)