img.difference() question

Hello, I tried to modify code in
by replacing the stored image with previous snapshot. But the
frame buffer window becomes totally black and every pixel in
img becomes (0,0,0).
I’m using cam m4 v2.

import sensor, image, pyb, os, time

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.set_auto_whitebal(False) # Turn off white balance.
clock = time.clock() # Tracks FPS.

background = sensor.snapshot()

    clock.tick() # Track elapsed milliseconds between snapshots().
    img = sensor.snapshot() # Take a picture and return the image.

    img.difference(background)# Replace the image with the "abs(NEW-OLD)" frame difference.
    print(clock.fps()) # Note: Your OpenMV Cam runs about half as fast while
    # connected to your computer. The FPS should increase once disconnected.

BTW, I tried and with a SD card
saving the background image file. But when it runs to sensor.snapshot().save(“temp/bg.bmp”)
the program would crash. Didn’t find out why.

Hi, you need the SD card to work. Um, there’s an issue with v1.6.0 of the IDE where I made the serial timeout too small for the IDE to accept the OpenMV Cam saving images to disk.

Please use this link to download a pre-release version of the 1.7.0 IDE where I’ve fixed this issue: Let me know if you need a Linux or Mac build. I can quickly get that posted.

As for what you are trying to do in your script. There’s only one frame buffer. sensor.shapshot() returns an image that points to that frame buffer. You have to do “background = sensor.snapshot().copy()” to create a secondary image. However, you’ll run out of RAM if you try to do this. This is the reason we use the SD card for frame differencing. Because we don’t have the RAM otherwise.

It works. :slight_smile:
Thank you very much.
Really appreciate it.