Ask Your Question
1

Would Python 3 and OpenCV allow me to create a motion detection grid?

asked 2018-01-12 17:31:42 -0600

masterenol gravatar image

updated 2018-01-12 18:45:38 -0600

I want to be able to monitor only certain blocks for motion detection. For example:

Motion Detection Grid

I'm currently studying OpenCV, and I want to make sure I am on the right path.

Thanks for any support any of you can provide.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2018-01-12 20:53:40 -0600

sjhalayka gravatar image

updated 2018-01-12 20:54:08 -0600

You're on the right path. Just use some simple absdiff magic to conjure up motion detection.

edit flag offensive delete link more

Comments

Hi sjhalayka, thanks for the response! I'm having trouble finding examples on how to use absdiff for motion detection. All the tutorials and scripts I found for Python motion detection don't seem to work for me. The OpenCV documentation does not provide examples on using absdiff for live video. Can you please help me get started with OpenCV motion detection?

masterenol gravatar imagemasterenol ( 2018-01-13 13:22:48 -0600 )edit

@masterenol: Here is some python code to use the absdifffunction, using an input video. Changing it to use a webcam shouldn't be too hard. A sample test.avi file can be downloaded from https://github.com/sjhalayka/obstacle...

import numpy as np
import cv2

cap = cv2.VideoCapture('test.avi')

ret, last_frame = cap.read()

if last_frame is None:
    exit()

while(cap.isOpened()):
    ret, frame = cap.read()

    if frame is None:
        break

    a = cv2.absdiff(last_frame, frame)

    cv2.imshow('frame', frame)
    cv2.imshow('a', a)

    if cv2.waitKey(33) >= 0:
        break

    last_frame = frame

cap.release()
cv2.destroyAllWindows()
sjhalayka gravatar imagesjhalayka ( 2018-01-13 14:04:30 -0600 )edit

Cool thanks! Using cv2.VideoCapture(0), the code gives me two windows, one very similar to using cv2.createBackgroundSubtractorMOG2() but much cleaner; is this the purpose of using absdiff in the code you provided?

masterenol gravatar imagemasterenol ( 2018-01-13 14:16:45 -0600 )edit

If I understand your question, then yes, this is the purpose of absdiff.

sjhalayka gravatar imagesjhalayka ( 2018-01-13 14:22:00 -0600 )edit

When I use print(a), I get several 3x3 matrices; do I need to understand this in order to code for motion detection? Picture link.

masterenol gravatar imagemasterenol ( 2018-01-13 14:33:01 -0600 )edit

I'm not sure what that's all about, sorry.

sjhalayka gravatar imagesjhalayka ( 2018-01-13 14:36:14 -0600 )edit

Ok, no worries. I'm just trying to figure out how to use absdiff to trigger a response for when motion is detected, for example having the window display the text 'Motion Detected' using cv2.putText().

masterenol gravatar imagemasterenol ( 2018-01-13 14:39:00 -0600 )edit

I think one way would be to do a call to threshold, then see if there are any white pixels in the resulting binarized image (in the regions of interest).

sjhalayka gravatar imagesjhalayka ( 2018-01-13 14:55:01 -0600 )edit

Hi sjhalayka, I'm having trouble with the threshold function. All the tutorials I find online for OpenCV threshold are for single images only, and I don't know how to operate the threshold function when using cv2.VideoCapture(0). Can you please help me again?

masterenol gravatar imagemasterenol ( 2018-01-13 20:39:34 -0600 )edit
1

Sorry, I don't have a webcam.

a = cv2.absdiff(last_frame, frame)

a = cv2.cvtColor(a, cv2.COLOR_BGR2GRAY)

ret, a = cv2.threshold(a,127,255,cv2.THRESH_BINARY)

sjhalayka gravatar imagesjhalayka ( 2018-01-14 00:14:33 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-01-12 17:31:42 -0600

Seen: 5,138 times

Last updated: Jan 12 '18