Color-based box (and corner/parallelogram) detection utilities with multiple algorithm versions and Raspberry Pi deployment support.
- Multiple detection pipelines (V1.2 HSV + morphology, V3 channel subtraction).
- Configurable HSV color ranges via interactive ROI tools.
- Support for detecting 3 colored reference boxes + a yellow locator box.
- Parallelogram / corner arrangement utilities.
- Single-file deploy script for Raspberry Pi: raspi_functions.py.
- Modular refactored detector: color_detector_refactored.py.
- Core (versioned) scripts:
- cbd_v1.2.py (HSV + distance transform)
- cbd_v2.py
- cbd_v3.py (newer experiments)
- color_detector_refactored.py
- raspi_functions.py (all-in-one)
- Tools (interactive config generation):
- tools/create_config.py (main object colors)
- tools/create_boxes_config.py (4 small boxes set)
- tools/new_obj.py (extend existing config)
- Parallelogram helpers:
JSON configs are ignored by Git (.gitignore contains *.json) so generate them locally.
Main: config.json
Secondary (4-box mode): config_boxes.json
Both contain:
{
"color_ranges": {
"red": [ [ [low_H,low_S,low_V], [high_H,high_S,high_V] ], ... ],
"green": [...],
"blue": [...],
"yellow": [...]
},
"big_box_crop": [x, y, w, h] (optional)
"boxes_crop": [x, y, w, h] (in config_boxes.json)
}
Loaded via:
load_configload_config(two variants: one forconfig.jsonnear line ~227 and one forconfig_boxes.jsonnear line ~377)load_config
If missing, some scripts fall back to hard‑coded defaults.
Interactive (requires GUI display):
python tools/create_config.py # builds config.json
python tools/create_boxes_config.py # builds config_boxes.json
python tools/new_obj.py # append/merge new object-specific ranges
Follow on-screen ROI selections. Color ranges are auto-generated per ROI.
Implemented in:
preprocess_v1_2_method- Supporting utilities:
detect_contours_and_centroids
Flow:
- HSV threshold per color range.
- Morphological cleanup.
- Distance transform to tighten mask.
- Contour filtering (area + aspect ratio).
- Representative centroid per channel.
Implemented in:
preprocess_v3- Inline helper:
isolate_and_subtract_channelinside that function and standalone in raspi_functions.py (top section).
Flow:
- Split BGR.
- For target channel c: subtract other channels → enhances dominance.
- Otsu threshold.
- Morph open + close.
- Contours extracted from cleaned mask.
Corner arrangement utilities (e.g., identifying relative positions) live around mid/lower sections of raspi_functions.py (see functions near detection of yellow + ordering, e.g., identify_corner_arrangement).
Use raspi_functions.py for minimal dependency deployment (aggregation of all logic). Copy only needed image + config assets beside the script.
- Create a configuration file (
config.json) in the project root. Use the interactive tool:
python tools/create_config.py
(Place the generated config.json next to the scripts.)
- HSV-only detection (simple pipeline):
python cbd_v3.py
This uses HSV ranges from config.json.
- Channel isolation + HSV (combined method):
python color_detector_refactored.py
Provide an image path; the script loads config.json automatically.
Notes:
- Regenerate
config.jsonif lighting changes. - Keep
config.jsonin the same directory as the scripts.
- Start with evenly lit images.
- Avoid over-tight ranges; include slight illumination variance.
- Re-generate after lighting/environment changes.
- Yellow often needs separate (smaller) morphology kernel (
MORPH_KERNEL_YELLOWin raspi_functions.py).
- Add new color: update generation tool to prompt for it.
- Add algorithm variant: create
preprocess_vXfunction and mirror usage pattern. - Unify duplicate
load_configfunctions if maintenance becomes difficult.
Install (typical):
pip install opencv-python numpy
(If running on Raspberry Pi, use opencv-contrib-python)
- Reduce resolution before processing for speed.
- Tune morphology kernel sizes (
MORPH_KERNEL_COLOR,MORPH_KERNEL_YELLOW) per camera. - Cache configs; avoid reloading each frame.
- Blank masks: verify HSV range order and lighting.
- Wrong color dominance in V3: ensure channel subtraction not saturating (inspect intermediate single-channel result).
- No contours: relax min area thresholds in functions like
detect_contours_and_centroids.