问题在于
img_now = sensor.snapshot()
img_diff2 = img_now.difference(img_past)
应该换成
img = sensor.snapshot()
img_now.replace(img)
img.difference(img_past)
img_diff2.replace(img)
下面的代码是可以运行的
import sensor, image, time
sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000) # Give the user time to get ready.
i=0
m=320
n=240
#temp = sensor.alloc_extra_fb(m, n, sensor.GRAYSCALE)
img_past = sensor.alloc_extra_fb(m, n, sensor.GRAYSCALE)
img_now = sensor.alloc_extra_fb(m, n, sensor.GRAYSCALE)
kernel_edge = [1, 1, 1, \
1, -8, 1, \
1, 1, 1]
img=sensor.snapshot()
img_past.replace(img)
clock = time.clock()
while i < 1000:
# 获取当前灰度图
img = sensor.snapshot()
#当前灰度图归一化
#temp.normalize(0, 255)
img_now.replace(img)
#差分图的绝对值
# img.save("temp3_withoutdifference/img_now"+str(i)+".bmp")
img.difference(img_past)
img.morph(1, kernel_edge)
# img.save("temp2/diff"+str(i)+".bmp")
i+=1
img_past.replace(img_now)
print(clock.fps())