|
| 1 | +# ColorBoxDetector |
| 2 | + |
| 3 | +Color-based box (and corner/parallelogram) detection utilities with multiple algorithm versions and Raspberry Pi deployment support. |
| 4 | + |
| 5 | +## Features |
| 6 | +- Multiple detection pipelines (V1.2 HSV + morphology, V3 channel subtraction). |
| 7 | +- Configurable HSV color ranges via interactive ROI tools. |
| 8 | +- Support for detecting 3 colored reference boxes + a yellow locator box. |
| 9 | +- Parallelogram / corner arrangement utilities. |
| 10 | +- Single-file deploy script for Raspberry Pi: [raspi_functions.py](raspi_functions.py). |
| 11 | +- Modular refactored detector: [color_detector_refactored.py](color_detector_refactored.py). |
| 12 | + |
| 13 | +## Project Layout (Key Files) |
| 14 | +- Core (versioned) scripts: |
| 15 | + - [cbd_v1.2.py](cbd_v1.2.py) (HSV + distance transform) |
| 16 | + - [cbd_v2.py](cbd_v2.py) |
| 17 | + - [cbd_v3.py](cbd_v3.py) (newer experiments) |
| 18 | + - [color_detector_refactored.py](color_detector_refactored.py) |
| 19 | + - [raspi_functions.py](raspi_functions.py) (all-in-one) |
| 20 | +- Tools (interactive config generation): |
| 21 | + - [tools/create_config.py](tools/create_config.py) (main object colors) |
| 22 | + - [tools/create_boxes_config.py](tools/create_boxes_config.py) (4 small boxes set) |
| 23 | + - [tools/new_obj.py](tools/new_obj.py) (extend existing config) |
| 24 | +- Parallelogram helpers: |
| 25 | + - [parallelogram_detector_dual_method.py](parallelogram_detector_dual_method.py) |
| 26 | + - [parallelogram_detector_v1_2.py](parallelogram_detector_v1_2.py) |
| 27 | + |
| 28 | +## Configuration |
| 29 | +JSON configs are ignored by Git (`.gitignore` contains *.json) so generate them locally. |
| 30 | + |
| 31 | +Main: `config.json` |
| 32 | +Secondary (4-box mode): `config_boxes.json` |
| 33 | + |
| 34 | +Both contain: |
| 35 | +``` |
| 36 | +{ |
| 37 | + "color_ranges": { |
| 38 | + "red": [ [ [low_H,low_S,low_V], [high_H,high_S,high_V] ], ... ], |
| 39 | + "green": [...], |
| 40 | + "blue": [...], |
| 41 | + "yellow": [...] |
| 42 | + }, |
| 43 | + "big_box_crop": [x, y, w, h] (optional) |
| 44 | + "boxes_crop": [x, y, w, h] (in config_boxes.json) |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +Loaded via: |
| 49 | +- [`load_config`](cbd_v1.2.py) |
| 50 | +- [`load_config`](raspi_functions.py) (two variants: one for `config.json` near line ~227 and one for `config_boxes.json` near line ~377) |
| 51 | +- [`load_config`](color_detector_refactored.py) |
| 52 | + |
| 53 | +If missing, some scripts fall back to hard‑coded defaults. |
| 54 | + |
| 55 | +## Generating Configs |
| 56 | +Interactive (requires GUI display): |
| 57 | +``` |
| 58 | +python tools/create_config.py # builds config.json |
| 59 | +python tools/create_boxes_config.py # builds config_boxes.json |
| 60 | +python tools/new_obj.py # append/merge new object-specific ranges |
| 61 | +``` |
| 62 | +Follow on-screen ROI selections. Color ranges are auto-generated per ROI. |
| 63 | + |
| 64 | +## Detection Pipelines |
| 65 | + |
| 66 | +### V1.2 (HSV + Distance Transform) |
| 67 | +Implemented in: |
| 68 | +- [`preprocess_v1_2_method`](raspi_functions.py) |
| 69 | +- Supporting utilities: [`detect_contours_and_centroids`](raspi_functions.py) |
| 70 | + |
| 71 | +Flow: |
| 72 | +1. HSV threshold per color range. |
| 73 | +2. Morphological cleanup. |
| 74 | +3. Distance transform to tighten mask. |
| 75 | +4. Contour filtering (area + aspect ratio). |
| 76 | +5. Representative centroid per channel. |
| 77 | + |
| 78 | +### V3 (Channel Isolation & Subtraction) |
| 79 | +Implemented in: |
| 80 | +- [`preprocess_v3`](color_detector_refactored.py) |
| 81 | +- Inline helper: `isolate_and_subtract_channel` inside that function and standalone in [raspi_functions.py](raspi_functions.py) (top section). |
| 82 | + |
| 83 | +Flow: |
| 84 | +1. Split BGR. |
| 85 | +2. For target channel c: subtract other channels → enhances dominance. |
| 86 | +3. Otsu threshold. |
| 87 | +4. Morph open + close. |
| 88 | +5. Contours extracted from cleaned mask. |
| 89 | + |
| 90 | +### Corner / Arrangement Logic |
| 91 | +Corner arrangement utilities (e.g., identifying relative positions) live around mid/lower sections of [raspi_functions.py](raspi_functions.py) (see functions near detection of yellow + ordering, e.g., `identify_corner_arrangement`). |
| 92 | + |
| 93 | +## Raspberry Pi Deployment |
| 94 | +Use [raspi_functions.py](raspi_functions.py) for minimal dependency deployment (aggregation of all logic). Copy only needed image + config assets beside the script. |
| 95 | +## Usage |
| 96 | +1. Create a configuration file (`config.json`) in the project root. Use the interactive tool: |
| 97 | + ``` |
| 98 | + python tools/create_config.py |
| 99 | + ``` |
| 100 | + (Place the generated `config.json` next to the scripts.) |
| 101 | + |
| 102 | +2. HSV-only detection (simple pipeline): |
| 103 | + ``` |
| 104 | + python cbd_v3.py |
| 105 | + ``` |
| 106 | + This uses HSV ranges from `config.json`. |
| 107 | + |
| 108 | +3. Channel isolation + HSV (combined method): |
| 109 | + ``` |
| 110 | + python color_detector_refactored.py |
| 111 | + ``` |
| 112 | + Provide an image path; the script loads `config.json` automatically. |
| 113 | + |
| 114 | +Notes: |
| 115 | +- Regenerate `config.json` if lighting changes. |
| 116 | +- Keep `config.json` in the same directory as the scripts. |
| 117 | + |
| 118 | +## Color Range Tuning Tips |
| 119 | +- Start with evenly lit images. |
| 120 | +- Avoid over-tight ranges; include slight illumination variance. |
| 121 | +- Re-generate after lighting/environment changes. |
| 122 | +- Yellow often needs separate (smaller) morphology kernel (`MORPH_KERNEL_YELLOW` in [raspi_functions.py](raspi_functions.py)). |
| 123 | + |
| 124 | +## Extending |
| 125 | +1. Add new color: update generation tool to prompt for it. |
| 126 | +2. Add algorithm variant: create `preprocess_vX` function and mirror usage pattern. |
| 127 | +3. Unify duplicate `load_config` functions if maintenance becomes difficult. |
| 128 | + |
| 129 | +## Dependencies |
| 130 | +Install (typical): |
| 131 | +``` |
| 132 | +pip install opencv-python numpy |
| 133 | +``` |
| 134 | +(If running on Raspberry Pi, use `opencv-contrib-python`) |
| 135 | + |
| 136 | +## Performance Notes |
| 137 | +- Reduce resolution before processing for speed. |
| 138 | +- Tune morphology kernel sizes (`MORPH_KERNEL_COLOR`, `MORPH_KERNEL_YELLOW`) per camera. |
| 139 | +- Cache configs; avoid reloading each frame. |
| 140 | + |
| 141 | +## Troubleshooting |
| 142 | +- Blank masks: verify HSV range order and lighting. |
| 143 | +- Wrong color dominance in V3: ensure channel subtraction not saturating (inspect intermediate single-channel result). |
| 144 | +- No contours: relax min area thresholds in functions like [`detect_contours_and_centroids`](raspi_functions.py). |
0 commit comments