A real-time movement detection application built with C# and OpenCvSharp that provides comprehensive motion monitoring and automatic video recording capabilities.
- Real-time Motion Detection: Advanced computer vision algorithms using background subtraction
- Dual Video Display: Live feed and motion detection overlay side by side
- Auto-Recording: Automatically records video when motion is detected
- Manual Recording: Start/stop recording on demand
- Configurable Sensitivity: Real-time adjustment from 1-100
- Background Learning: Automatically adapts to lighting and scene changes
- Professional GUI: Clean Windows Forms interface
- Real-time Controls: Start/Stop detection, sensitivity adjustment, background reset
- Status Monitoring: Live motion detection status and event logging
- Recording Controls: Manual recording toggle and auto-record checkbox
- Event Logging: Comprehensive logging with timestamps
- Configuration System: JSON-based settings management
- Error Handling: Robust camera and processing error management
- Extensible Design: Modular architecture for easy feature additions
- OS: Windows 10/11
- Framework: .NET 8.0 or higher
- Memory: 2GB RAM minimum
- Camera: DirectShow compatible webcam
- Storage: 100MB for application + space for recordings
-
Clone the repository:
git clone <repository-url> cd movement-detection
-
Run the application:
# Option 1: Use the demo script .\demo.ps1 # Option 2: Use the batch file .\run.bat # Option 3: Direct command dotnet run --project MovementDetection.csproj
# Restore dependencies
dotnet restore
# Build the project
dotnet build
# Run the application
dotnet run- Start the Application - Camera initializes automatically
- Click "Start Detection" - Begin motion monitoring
- Adjust Sensitivity - Use the slider (recommended: 25-50)
- Enable Auto-Record - Check the "Auto Record" box for automatic recording
- Monitor Status - Watch real-time updates in the status panel
- Red rectangles highlight detected motion areas
- Green contours show exact movement boundaries
- Status updates appear in real-time
- Event logging tracks all motion events with timestamps
- Auto-Recording: Starts when motion detected, stops 5 seconds after motion ends
- Manual Recording: Click "Start Recording" for continuous recording
- File Management: Videos saved to
Recordings/with timestamp filenames - Event Logs: All activities logged to
Logs/directory
Edit config.json to customize behavior:
{
"Motion": {
"Threshold": 25.0, // Detection sensitivity
"MinContourArea": 500.0, // Minimum motion area
"LearningRate": 0.01, // Background learning rate
"LearningFrames": 30 // Frames to learn background
},
"Camera": {
"DeviceIndex": 0, // Camera device number
"FrameWidth": 640, // Video resolution width
"FrameHeight": 480, // Video resolution height
"Fps": 30.0 // Frame rate
},
"Recording": {
"AutoRecord": true, // Enable auto-recording
"OutputDirectory": "Recordings",
"VideoFormat": "avi",
"VideoCodec": "MJPG"
}
}โโโ Source Files
โ โโโ MainForm.cs # Main application window and UI logic
โ โโโ MotionDetector.cs # Core motion detection algorithms
โ โโโ VideoRecorder.cs # Video recording functionality
โ โโโ MotionEventLogger.cs # Event logging system
โ โโโ AppConfig.cs # Configuration management
โ โโโ Program.cs # Application entry point
โโโ Configuration
โ โโโ MovementDetection.csproj # Project file with dependencies
โ โโโ config.json # Runtime configuration
โ โโโ .gitignore # Git ignore rules
โโโ Documentation
โ โโโ README.md # This file
โ โโโ USER_GUIDE.md # Detailed user manual
โ โโโ PROJECT_SUMMARY.md # Development summary
โโโ Scripts
โ โโโ demo.ps1 # PowerShell demo script
โ โโโ run.bat # Simple startup script
โโโ Output Directories
โโโ Recordings/ # Recorded video files
โโโ Logs/ # Event and system logs
- Frame Capture: 30 FPS video processing
- Preprocessing: Grayscale conversion and Gaussian blur
- Background Modeling: Adaptive background subtraction
- Motion Detection: Frame differencing with binary thresholding
- Noise Reduction: Morphological operations (opening/closing)
- Contour Analysis: Area-based motion filtering
- Visual Feedback: Bounding boxes and contour overlay
- OpenCvSharp4: Computer vision library for .NET
- OpenCvSharp4.runtime.win: Windows runtime for OpenCV
- OpenCvSharp4.Extensions: Additional OpenCV extensions
- System.Text.Json: JSON configuration management
- Home security monitoring
- Office/warehouse surveillance
- Perimeter intrusion detection
- Automated security recording
- Motion-activated lighting systems
- Automated recording for events
- Pet/wildlife monitoring
- Visitor detection and logging
- Computer vision algorithm testing
- Motion analysis research
- Educational computer vision demonstrations
- Prototype surveillance systems
"Could not open camera!" error
- Ensure webcam is connected and recognized by Windows
- Close other applications using the camera (Teams, Skype, etc.)
- Try different camera device index in config.json
Poor motion detection accuracy
- Adjust sensitivity slider (try values 20-50)
- Reset background when lighting changes
- Ensure stable camera mounting (reduce vibrations)
- Check for adequate lighting conditions
Performance issues
- Close unnecessary applications
- Reduce video resolution in config.json
- Check available system memory
- Multiple camera support
- Email/SMS notifications
- Web-based remote monitoring
- Mobile app companion
- Object tracking and classification
- Face recognition integration
- Machine learning-based detection
- Cloud storage integration
- Motion pattern analysis
- Automated alert systems
- Integration with home automation platforms
Contributions are welcome! Please feel free to submit issues, feature requests, or pull requests.
- Fork the repository
- Create a feature branch:
git checkout -b feature-name - Make your changes and test thoroughly
- Submit a pull request with detailed description
This project is open source and available under the MIT License.
- OpenCvSharp: Excellent .NET wrapper for OpenCV
- OpenCV Community: Amazing computer vision library
- Microsoft: .NET framework and development tools
Built with โค๏ธ using C# and OpenCV