A computer vision project that counts vehicles crossing a designated line in a video using background subtraction and contour detection.
This project uses OpenCV to detect and count vehicles passing through a virtual counting line in a video. It employs background subtraction techniques to identify moving objects and tracks them as they cross a predefined boundary.
- ✅ Real-time vehicle detection using background subtraction
- ✅ Automatic counting when vehicles cross the counting line
- ✅ Visual feedback with bounding boxes and center points
- ✅ Configurable detection parameters
- ✅ Counter display on video feed
opencv-python
opencv-contrib-python
numpy
git clone https://github.com/yourusername/vehicle-counter.git
cd vehicle-counterpip install opencv-python opencv-contrib-python numpyPlace your video file in the project directory and name it video.mp4 (or update the file path in the code)
Run the script:
python vehicle_counter.py▶️ Opens and processes the video file- 🟢 Displays the video with detected vehicles marked by green rectangles
- 📏 Shows a counting line across the frame
- 🔢 Counts vehicles as they cross the line
- 📊 Displays the total count on screen
Press Enter to stop the program.
| Parameter | Description | Default Value |
|---|---|---|
min_width_react |
Minimum width of detected rectangle | 80 |
min_height_react |
Minimum height of detected rectangle | 80 |
count_line_position |
Y-coordinate of counting line | 550 |
offset |
Allowable pixel error for counting | 6 |
You can adjust these parameters in the code to fine-tune detection for your specific video.
- Background Subtraction: Uses MOG (Mixture of Gaussians) algorithm to separate foreground (vehicles) from background
- Preprocessing: Applies Gaussian blur and morphological operations to reduce noise
- Contour Detection: Identifies vehicle shapes using contour detection
- Center Tracking: Calculates center point of each detected vehicle
- Line Crossing Detection: Counts vehicles when their center crosses the counting line
cap = cv2.VideoCapture('your_video.mp4')cap = cv2.VideoCapture(0) # 0 for default webcamThe program displays:
- 🎥 Original video with annotations
- 🟢 Green rectangles around detected vehicles
- 🔴 Red circles at vehicle centers
- 🟠 Orange/blue counting line
- 🔢 Real-time vehicle count
- 💬 Console output for each counted vehicle
- 📹 Works best with static camera angles
- 💡 Performance depends on video quality and lighting conditions
- 🔧 May require parameter tuning for different scenarios
- 🌦️ Background subtraction can be sensitive to environmental changes
| Issue | Solution |
|---|---|
| No vehicles detected | Adjust min_width_react and min_height_react values |
| False positives | Increase minimum size parameters or adjust morphological operations |
| Missed vehicles | Lower the offset value or adjust counting line position |
| Video not found | Verify video file path and name |
Note: This project requires
opencv-contrib-pythonfor thecv2.bgsegm.createBackgroundSubtractorMOG()function.
Tip: For best results, use videos with:
- Static camera position
- Good lighting conditions
- Clear view of vehicles
- Minimal background movement
- OpenCV community for excellent documentation
- Background subtraction algorithms research
- Computer vision tutorials and resources
⭐ Star this repo if you find it helpful!