Classical, dependency-light inspection for defocus / phase-shift microlens imaging. Two pillars:
- Microstructure morphology — five-step phase-shifting recovers wrapped phase, DC, and modulation amplitude from fringe (schlieren-style) images.
- Defect detection — a 28-frame threshold baseline segments the lens and classifies scratches / pits / crashes / anomalies, exporting masks, COCO annotations, and overlays.
Learning-based ("intelligent") detection is developed in a separate project. This package is the classical morphology / defect front end intended to plug into a larger microstructure-lens recognition system.
python -m venv .venv && source .venv/bin/activate # Windows: .venv\Scripts\activate
pip install -e . # add ".[dev]" for tests + lintersCLI entry point: microlens-defects (package microlens_defects, v0.4.0).
Defect detection (single sample):
microlens-defects detect --glasses 2006 --side left --grating cycle \
--db microlens_metadata.db --img-root organized_tiffs \
--save-dir defect_detection_outputsBatch over the database (use --limit to cap; passing only --limit enables
--all automatically; --all cannot be combined with single-sample options):
microlens-defects detect --all --limit 10 \
--db microlens_metadata.db --img-root organized_tiffs \
--save-dir defect_detection_outputs \
--config configs/detect_threshold.yamlMicrostructure morphology (five-step phase-shifting, 5 fringe frames):
microlens-defects phase5 ./five_frames --pattern "*.tif" --output phase_result.npzThreshold baseline (28-frame stack; per-pixel DC mean and temporal std):
- CLAHE on inverted DC → adaptive threshold + morphology → coarse global mask.
- Gaussian density threshold → crash (large damaged regions).
- Geometry / prominence rules → scratch (elongated, merged), pit, anomaly.
- Export full-image mask, COCO annotations, and an overlay.
Per sample, written to save-dir/<glasses>_<side>_<grating>/:
| File | Content |
|---|---|
<tag>_dc_clahe.png |
DC + CLAHE base image (morphology) |
<tag>_global_mask.png |
binary global mask |
<tag>_overlay.png |
classified defect overlay |
<tag>_annotations.json |
COCO annotations |
Plus save-dir/metadata_summary.csv|jsonl (per-sample stats + parameter snapshot).
COCO categories: scratch(1), pit(2), crash(3), anomaly(4).
The five-step phase command saves a compressed NPZ with phase, dc,
amplitude, and mask arrays. See the algorithm in
features/five_step_phase.py.
Tune via configs/detect_threshold.yaml or CLI overrides; the resolved parameters are logged and stored in the metadata snapshot.
- Threshold / morphology:
adaptive_block,adaptive_c,open_kernel,close_kernel - Density (crash):
density_kernel_ratio,density_threshold,dense_min_area - Scratch:
scratch_min_len,scratch_min_aspect, extend / merge parameters - Prominence:
prominence_min_value(applied to pit / anomaly only)
- SQLite table
imageswith at least:glasses_id,lens_side,grating_type,phase_index(0..28),file_path. --img-root(defaultorganized_tiffs): resolved as a relative path first, then tried as an absolute path.
src/microlens_defects/
├── data/db.py # SQLite + image-root stack loader
├── detection/ # threshold defect detector
│ ├── base.py # BaseDetector / DetectionResult interface
│ ├── params.py # parameters + validation
│ ├── features.py # feature extraction
│ ├── masks.py # mask building + component classification
│ ├── rendering.py # overlay / COCO / metadata export
│ └── threshold.py # ThresholdDetector + run_threshold_detection
├── features/five_step_phase.py
├── cli/app.py # Typer CLI (detect, phase5)
├── logging.py · exceptions.py
configs/ docs/ tests/ scripts/make_demo_webp.py
pip install -e ".[dev]"
pytest -q
ruff check src tests && ruff format --check src testsDocs: docs/index.md · architecture, API guide, troubleshooting. License: LICENSE.
