Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 

Repository files navigation

🎥 Motion Detection using OpenCV

A simple real-time motion detection system built using Python, OpenCV, and Imutils. The application uses a webcam to monitor movement and identifies moving objects by comparing the current video frame with an initial reference frame.

When significant movement is detected, the system displays "Moving Object Detected" and highlights the moving region with a bounding box.

🚀 Features

  • 📷 Real-time webcam monitoring
  • 🔍 Detects moving objects using frame differencing
  • 🖼️ Converts frames to grayscale for processing
  • 🌫️ Applies Gaussian Blur to reduce noise
  • 📊 Uses thresholding to identify significant changes
  • 📦 Draws bounding boxes around detected movement
  • ⚡ Lightweight and easy to run
  • ⌨️ Press q to exit the application

🛠️ Technologies Used

  • Python
  • OpenCV
  • Imutils

📂 Project Structure

Motion-Detection/
│
├── motion_detection.py
└── README.md

⚙️ How It Works

The project follows a simple computer vision pipeline:

Webcam
   ↓
Capture Frame
   ↓
Resize Frame
   ↓
Convert to Grayscale
   ↓
Gaussian Blur
   ↓
Capture Initial Frame
   ↓
Compare Current Frame with Initial Frame
   ↓
Calculate Absolute Difference
   ↓
Threshold the Difference
   ↓
Dilate Image
   ↓
Find Contours
   ↓
Filter Small Movements
   ↓
Draw Bounding Box
   ↓
Display Motion Status

1. Capture Webcam Feed

The application accesses the system's default webcam using OpenCV.

cam = cv2.VideoCapture(0)

2. Preprocess the Frame

Each frame is resized, converted to grayscale, and blurred to make motion detection more stable.

img = imutils.resize(img, width=1000)

grayImg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

gaussianImg = cv2.GaussianBlur(grayImg, (21, 21), 0)

3. Create a Reference Frame

The first processed frame is stored as the reference frame.

if firstFrame is None:
    firstFrame = gaussianImg
    continue

4. Detect Differences

The current frame is compared with the reference frame using absolute difference.

imgDiff = cv2.absdiff(firstFrame, gaussianImg)

5. Apply Thresholding

Pixels with significant differences are converted into a binary image.

threshImg = cv2.threshold(
    imgDiff, 25, 255, cv2.THRESH_BINARY
)[1]

6. Find Moving Objects

Contours are detected from the thresholded image. Very small contours are ignored to prevent minor noise from being treated as movement.

if cv2.contourArea(c) < area:
    continue

7. Draw Bounding Boxes

When movement is detected, a rectangle is drawn around the moving object.

cv2.rectangle(
    img,
    (x, y),
    (x + w, y + h),
    (0, 255, 0),
    2
)

The application then displays:

Moving Object Detected

The detection and bounding-box logic are implemented directly in the uploaded Python script.

📦 Installation

Make sure Python is installed on your system.

Install the required libraries:

pip install opencv-python imutils

Or:

pip install -r requirements.txt

If you want to use requirements.txt, create the file with:

opencv-python
imutils

▶️ How to Run

Clone the repository:

git clone https://github.com/your-username/Motion-Detection.git

Navigate into the project:

cd Motion-Detection

Run the Python program:

python motion_detection.py

Your webcam should open automatically.

Move an object in front of the camera and the application will detect the movement.

⌨️ Controls

Key Action
q Exit the application

The program listens for the q key and then releases the camera and closes the OpenCV windows.

🎯 Detection Logic

The main idea behind this project is background/reference-frame subtraction.

Let:

Reference Frame = F₁
Current Frame   = F₂

The difference is calculated as:

Difference = |F₁ - F₂|

If the difference is large enough, the system considers that region to contain movement.

A threshold of 25 is used to identify meaningful pixel differences, while contours smaller than an area of 500 are ignored.

💡 Applications

This basic motion detection technique can be extended for:

  • 🏠 Home security systems
  • 📹 CCTV monitoring
  • 🚨 Intrusion detection
  • 🏢 Office monitoring
  • 🚪 Entry/exit monitoring
  • 🤖 Computer vision projects
  • 📷 Smart surveillance systems

🔮 Future Improvements

Possible improvements include:

  • Add motion detection alerts
  • Capture images when movement is detected
  • Record video automatically
  • Send notifications through email or SMS
  • Detect and track specific objects
  • Use background subtraction algorithms
  • Add a graphical user interface
  • Store motion events with timestamps
  • Integrate with an IoT security system

⚠️ Limitations

This is a basic motion detection implementation and has some limitations:

  • It relies on an initial reference frame.
  • Changes in lighting can trigger false detections.
  • Camera movement can cause incorrect detection.
  • Small movements may be ignored because of the contour-area threshold.
  • It does not identify what the moving object is; it only detects movement.

📜 License

This project is open-source and available for learning and educational purposes.


👩‍💻 Author

Maria Blessy R J

If you found this project useful, consider giving the repository a ⭐ on GitHub!

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages