A cross-platform computer vision system for real-time color object detection using HSV-based filtering. Supports high-resolution industrial cameras (IMX415 4K) and standard consumer webcams (Logitech), with platform-specific implementations for macOS and Windows.
The system works in two stages:
- Calibration — interactively tune HSV color thresholds for your target object under your lighting conditions
- Detection — run real-time detection with bounding boxes, centroid markers, area measurements, and a live FPS counter
ComputerVisionProject/
├── MacOS/
│ └── IMX415/
│ ├── calibration.py # HSV calibration tool for macOS + IMX415
│ └── colorodDetection.py # Real-time detection for macOS + IMX415
└── Windows/
├── IMX415/
│ ├── calibrationWindows.py # HSV calibration tool for Windows + IMX415
│ └── colorDetectionWindows.py# Real-time detection for Windows + IMX415
└── Logitech/
├── calibrationWindows.py # HSV calibration tool for Windows + Logitech
└── colorodDetectionWindows.py # Real-time detection for Windows + Logitech
- Python 3.7+
- OpenCV
- NumPy
Install dependencies:
pip install opencv-python numpyRun the calibration script for your platform and camera. Interactive sliders let you adjust the HSV range until only your target object is visible in the mask.
macOS (IMX415):
python MacOS/IMX415/calibration.pyWindows (IMX415):
python Windows/IMX415/calibrationWindows.pyWindows (Logitech):
python Windows/Logitech/calibrationWindows.pyThe calibration window shows three panels side by side:
| Panel | Description |
|---|---|
| Original | Raw camera feed |
| Mask | Binary mask from HSV threshold |
| Result | Masked output (target object isolated) |
Press S to save the calibrated HSV values. Press Q to quit.
Run the detection script. It uses the calibrated HSV values to find and track the object in real time.
macOS (IMX415):
python MacOS/IMX415/colorodDetection.pyWindows (IMX415):
python Windows/IMX415/colorDetectionWindows.pyWindows (Logitech):
python Windows/Logitech/colorodDetectionWindows.pyEach detected object is annotated with:
- Green bounding box
- Red dot at the centroid
- Label showing pixel area and center coordinates
(x, y)
A color-coded FPS counter is displayed in the top-left corner:
| Color | FPS |
|---|---|
| Green | ≥ 20 fps |
| Orange | ≥ 10 fps |
| Red | < 10 fps |
Press Q to quit.
- Resolution: 3840×2160 (4K) for calibration, 1920×1080 for detection
- Codec: MJPEG
- Target FPS: 60
- Backend:
CAP_AVFOUNDATION(macOS) /CAP_MSMF(Windows)
- Standard webcam resolution
- Backend:
CAP_MSMF(Windows)
- Each frame is captured from the camera and converted from BGR to HSV color space.
- A binary mask is generated by thresholding the HSV frame within the calibrated color range.
- Morphological open and close operations remove noise and fill gaps in the mask.
- Contours are extracted from the cleaned mask. Contours below a minimum area threshold are ignored.
- For each valid contour, the bounding box and centroid are calculated and drawn on the frame.
HSV color space is used instead of RGB because it separates color information (hue) from lighting intensity (value), making detection more robust under varying lighting conditions.
- Run calibration under the same lighting conditions you plan to use for detection.
- Start by isolating the Hue channel to match your object's color, then refine Saturation and Value to exclude background noise.
- The minimum area threshold in the detection script can be adjusted to filter out small false positives.