Example explanation 04-Image-Filters->advanced_frame_differencing advanced frame difference
# 高级帧间差分例子
#
# 注意: 为了运行这个程序,你需要插入SD卡。
#
# 这个例子示范了OpenMV的帧间差分算法。
# 之所以叫做高级的帧间查分,是因为背景图片会实时更新
import sensor, image, os, time
BG_UPDATE_FRAMES = 50 # How many frames before blending. 融合前有多少帧图像
BG_UPDATE_BLEND = 128 # How much to blend by... ([0-256]==[0.0-1.0]).
sensor.reset() # 初始化摄像头
sensor.set_pixformat(sensor.GRAYSCALE) # or sensor.RGB565
#设置图像色彩格式,有RGB565色彩图和GRAYSCALE灰度图两种
sensor.set_framesize(sensor.QVGA) # or sensor.QQVGA (or others)
#设置图像像素大小
sensor.skip_frames(time = 2000) # 让新的设置生效
sensor.set_auto_whitebal(False) # 关闭白平衡
clock = time.clock() # 跟踪FPS帧率
if not "temp" in os.listdir(): os.mkdir("temp") # 新建一个新的文件夹
print("About to save background image...")
sensor.skip_frames(time = 2000) # 给用户一个时间来准备
sensor.snapshot().save("temp/bg.bmp")
print("Saved background image - Now frame differencing!")
frame_count = 0
while(True):
clock.tick() # 追踪两个snapshots()之间经过的毫秒数.
img = sensor.snapshot() # 拍一张照片,返回图像
frame_count += 1
if frame_count > BG_UPDATE_FRAMES:
frame_count = 0
# Blend in new frame. We're doing 256-alpha here because we want to
# blend the new frame into the backgound. Not the background into the
# new frame which would be just alpha. Blend replaces each pixel by
# ((NEW*(alpha))+(OLD*(256-alpha)))/256. 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.
# 融入新的帧。
# 我们在这里做256-alpha因为我们想把新帧混合到背景中而不是背景进入新帧,背景融入新帧只是alpha。
# Blend将每个像素替换为((NEW*(alpha))+(OLD*(256-alpha)) /256.
# 因此,低alpha导致新图像的低混合,而高alpha导致新图像的高混。
# 我们需要在这次更新中逆转这一点
img.blend("temp/bg.bmp", alpha=(256-BG_UPDATE_BLEND))
img.save("temp/bg.bmp")
# Replace the image with the "abs(NEW-OLD)" frame difference.
# 将图像替换为 "abs(NEW-OLD)" 帧差
img.difference("temp/bg.bmp")
print(clock.fps()) # 注意: 当连接电脑后,OpenMV会变成一半的速度。当不连接电脑,帧率会增加。
Singtown Technology OpenMV official Chinese document function explanation: