0
0
Computer Visionml~5 mins

Motion detection basics in Computer Vision

Choose your learning style9 modes available
Introduction

Motion detection helps computers see when something moves in a video. It is useful for security, games, and smart cameras.

To alert when someone enters a room using a security camera
To count people walking in a store
To start recording only when there is movement
To control a game character by moving in front of a camera
To detect traffic flow on roads
Syntax
Computer Vision
frame_diff = cv2.absdiff(frame1, frame2)
gray = cv2.cvtColor(frame_diff, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5,5), 0)
_, thresh = cv2.threshold(blur, 20, 255, cv2.THRESH_BINARY)
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

This code compares two video frames to find differences.

Contours show where movement happens.

Examples
Convert the difference between frames to grayscale to simplify processing.
Computer Vision
frame_diff = cv2.absdiff(frame1, frame2)
gray = cv2.cvtColor(frame_diff, cv2.COLOR_BGR2GRAY)
Blur the image to reduce noise, then create a binary image to highlight moving areas.
Computer Vision
blur = cv2.GaussianBlur(gray, (5,5), 0)
_, thresh = cv2.threshold(blur, 20, 255, cv2.THRESH_BINARY)
Find shapes of moving parts and ignore small changes like light flickers.
Computer Vision
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for contour in contours:
    if cv2.contourArea(contour) > 500:
        print('Motion detected!')
Sample Model

This program uses your webcam to detect motion. It shows a green box around moving parts and prints 'Motion detected!' when movement is found.

Computer Vision
import cv2

cap = cv2.VideoCapture(0)
ret, frame1 = cap.read()
ret, frame2 = cap.read()

while cap.isOpened():
    frame_diff = cv2.absdiff(frame1, frame2)
    gray = cv2.cvtColor(frame_diff, cv2.COLOR_BGR2GRAY)
    blur = cv2.GaussianBlur(gray, (5,5), 0)
    _, thresh = cv2.threshold(blur, 20, 255, cv2.THRESH_BINARY)
    contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    motion_detected = False
    for contour in contours:
        if cv2.contourArea(contour) > 1000:
            motion_detected = True
            (x, y, w, h) = cv2.boundingRect(contour)
            cv2.rectangle(frame1, (x, y), (x+w, y+h), (0, 255, 0), 2)

    if motion_detected:
        print('Motion detected!')

    cv2.imshow('Motion Detection', frame1)
    frame1 = frame2
    ret, frame2 = cap.read()

    if cv2.waitKey(30) & 0xFF == 27:  # Press ESC to exit
        break

cap.release()
cv2.destroyAllWindows()
OutputSuccess
Important Notes

Lighting changes can cause false motion detection.

Adjust the contour area threshold to ignore small movements like shadows.

Use a stable camera to get better results.

Summary

Motion detection compares video frames to find changes.

It uses image difference, grayscale, blur, threshold, and contours.

It helps computers notice when something moves in a scene.