Ask Your Question

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

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

masterenol gravatar image

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

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

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

sjhalayka gravatar image

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

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

edit flag offensive delete link more


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 -0500 )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

import numpy as np
import cv2

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

ret, last_frame =

if last_frame is None:

    ret, frame =

    if frame is None:

    a = cv2.absdiff(last_frame, frame)

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

    if cv2.waitKey(33) >= 0:

    last_frame = frame

sjhalayka gravatar imagesjhalayka ( 2018-01-13 14:04:30 -0500 )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 -0500 )edit

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

sjhalayka gravatar imagesjhalayka ( 2018-01-13 14:22:00 -0500 )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 -0500 )edit

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

sjhalayka gravatar imagesjhalayka ( 2018-01-13 14:36:14 -0500 )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 -0500 )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 -0500 )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 -0500 )edit

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 -0500 )edit

Question Tools

1 follower


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

Seen: 4,870 times

Last updated: Jan 12 '18