# 拍摄移动物体
#
# 注意:您将需要SD卡来运行此示例。
#
# 此示例演示如何使用OpenMV的帧差异来进行运动检测。运动检测后,您的
# OpenMV将拍摄照片。
import sensor, image, pyb, os
RED_LED_PIN = 1
BLUE_LED_PIN = 3
sensor.reset() # Initialize the camera sensor.
sensor.set_pixformat(sensor.RGB565) # or sensor.GRAYSCALE
sensor.set_framesize(sensor.QVGA) # or sensor.QQVGA (or others)
sensor.skip_frames(time = 2000) # Let new settings take affect.
sensor.set_auto_whitebal(False) # Turn off white balance.
if not "temp" in os.listdir(): os.mkdir("temp") # Make a temp directory
while(True):
pyb.LED(RED_LED_PIN).on()
print("About to save background image...")
sensor.skip_frames(time = 2000) # Give the user time to get ready.
pyb.LED(RED_LED_PIN).off()
sensor.snapshot().save("temp/bg.bmp")
print("Saved background image - Now detecting motion!")
pyb.LED(BLUE_LED_PIN).on()
diff = 10 # We'll say we detected motion after 10 frames of motion.
while(diff):
img = sensor.snapshot()
img.difference("temp/bg.bmp")
stats = img.statistics()
# state[5]是照明颜色通道的最大值。当整个图像的最大光照高于20时
# 触发下面的代码。
# 照明差异最大值应该为零。
if (stats[5] > 20):
diff -= 1
pyb.LED(BLUE_LED_PIN).off()
print("Movement detected! Saving image...")
sensor.snapshot().save("temp/snapshot-%d.jpg" % pyb.rng()) # Save Pic.
你的程序的背景是不更新的,也就是一直对比的是最开始的图片。
看一下这个高级帧差分,对比的图片是会更新的。
https://book.openmv.cc/example/04-Image-Filters/advanced-frame-differencing.html