Page 1 of 1

Detect moving object

Posted: Sat Jun 08, 2019 2:22 pm
by JeanJean
HI,

I want to detect all moving objects on my image.
For example counting the cars passing by. Also I do want to get the biggest moving object.
In which direction should I look, should I go for the blob detection, but I don't want to use colors for thresholds ?

Regards,

Jan

Re: Detect moving object

Posted: Sat Jun 08, 2019 2:27 pm
by kwagyeman
Frame differencing followed by find_blobs() will work for what you want to do. However, shadows and other things will trip you up. Try frame differencing out followed by blob detection for quick results however.

Re: Detect moving object

Posted: Tue Jun 11, 2019 4:09 pm
by kwiley
I like optical flow for applications like this. Unlike frame differencing, OF will actually give you the direction and speed of the moving objects in addition to their mere detection. OF is built into the OpenMV APIs I believe. Just dig through the examples some.

Re: Detect moving object

Posted: Fri Jul 05, 2019 5:30 pm
by tarzanboy13
Hello, I am trying In Memory Basic Frame Differencing with find blobs.
Can you help me ? differencing works, but blobs not.

Code: Select all

# In Memory Basic Frame Differencing Example

import sensor, image, pyb, os, time, math

TRIGGER_THRESHOLD = 5
thresholds = [(11, 100, -128, 127, -128, 127)]

sensor.reset() # Initialize the camera sensor.
sensor.set_pixformat(sensor.GRAYSCALE) # or sensor.GRAYSCALE
sensor.set_framesize(sensor.QVGA) # or sensor.QQVGA (or others)
sensor.skip_frames(time = 5000) # Let new settings take affect.
sensor.set_auto_whitebal(False) # Turn off white balance.
clock = time.clock() # Tracks FPS.
sensor.set_auto_exposure(True, exposure_us=10000) # shutter module

extra_fb = sensor.alloc_extra_fb(sensor.width(), sensor.height(), sensor.GRAYSCALE)

print("About to save background image...")
sensor.skip_frames(time = 2000) # Give the user time to get ready.
extra_fb.replace(sensor.snapshot())
print("Saved background image - Now frame differencing!")

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

    # Replace the image with the "abs(NEW-OLD)" frame difference.
    hah = img.difference(extra_fb)

for blob in hah.find_blobs(thresholds, pixels_threshold=1, area_threshold=1):
    # These values depend on the blob not being circular - otherwise they will be shaky.
    if blob.elongation() > 0.5:
        hah.draw_edges(blob.min_corners(), color=(255,0,0))
        hah.draw_line(blob.major_axis_line(), color=(0,255,0))
        hah.draw_line(blob.minor_axis_line(), color=(0,0,255))
    # These values are stable all the time.
    hah.draw_rectangle(blob.rect())
    hah.draw_cross(blob.cx(), blob.cy())
    # Note - the blob rotation is unique to 0-180 only.
    hah.draw_keypoints([(blob.cx(), blob.cy(), int(math.degrees(blob.rotation())))], size=20)

    print(clock.fps())

Re: Detect moving object

Posted: Sat Jul 06, 2019 8:28 pm
by kwagyeman
Hi, are the color tracking thresholds set right? Please look at the difference image and use the Tools-> Machine Vision-> Threshold Editor to get the right color thresholds for the difference image. You have those apply them to the script and things should start to work.

Re: Detect moving object

Posted: Sat Jul 06, 2019 8:30 pm
by kwagyeman
Um, from your code the indentation is wrong. The code for the find blobs never gets executed. Please check your python indentation.

Re: Detect moving object

Posted: Sun Jul 07, 2019 3:05 am
by tarzanboy13
Ok, problem solved, very easy to detect all objects
Global Shutter 37 fps - 73 with USB

Code: Select all

import sensor, image, pyb, os, time, math

thresholds = [(11, 100, -128, 127, -128, 127)]

sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False) # must be turned off for color tracking
sensor.set_auto_whitebal(False) # must be turned off for color tracking
clock = time.clock()
#sensor.set_auto_exposure(True, exposure_us=10000) # shutter module

extra_fb = sensor.alloc_extra_fb(sensor.width(), sensor.height(), sensor.GRAYSCALE)

print("About to save background image...")
sensor.skip_frames(time = 2000) # Give the user time to get ready.
extra_fb.replace(sensor.snapshot())
print("Saved background image - Now frame differencing!")


while(True):
    clock.tick()
    img = sensor.snapshot()
    img.difference(extra_fb)

    for blob in img.find_blobs(thresholds, pixels_threshold=200, area_threshold=200):
        # These values depend on the blob not being circular - otherwise they will be shaky.
        if blob.elongation() > 0.5:
            img.draw_edges(blob.min_corners(), color=(255,0,0))
            img.draw_line(blob.major_axis_line(), color=(0,255,0))
            img.draw_line(blob.minor_axis_line(), color=(0,0,255))
        # These values are stable all the time.
        img.draw_rectangle(blob.rect())
        img.draw_cross(blob.cx(), blob.cy())
        # Note - the blob rotation is unique to 0-180 only.
        img.draw_keypoints([(blob.cx(), blob.cy(), int(math.degrees(blob.rotation())))], size=20)
    print(clock.fps())

Re: Detect moving object

Posted: Sun Jul 07, 2019 3:06 am
by tarzanboy13
So 37 fps with USB ON
and without USB it will be 2*37fps = 74fps ?

Re: Detect moving object

Posted: Tue Jul 09, 2019 1:28 am
by kwagyeman
Um, just click the disable button the on top right of the IDE to see the frame rate without the burden of streaming JPG images to the PC.

Re: Detect moving object

Posted: Tue Jul 09, 2019 2:44 am
by tarzanboy13
hmm, its almost same. average 50fps in QVGA (with and without USB) and 100fps+ in QQVGA