All MicroEvals
Construct an end-to-end, production-ready image processing s...
Create MicroEval
Header image for Construct an end-to-end, production-ready image processing s...

Construct an end-to-end, production-ready image processing s...

Prompt

Construct an end-to-end, production-ready image processing system for high-precision, structure-preserving facial and cutaneous hair detection, isolation, and removal using Multi-Angle Directional Morphological Component Analysis (MCA) and Guided Isophote Inpainting. The software must be fully implemented across complete, unabridged Python modules with zero placeholders, mocks, or simulated functions. --- ### System Architecture and Implementation Requirements #### 1. Color-Space Decoupling Module (`color_space.py`) * **Objective:** Decouple luminance (structural/topographical features) from chrominance (perceptual color channels) to isolate hair contrast without altering subsurface skin pigmentation. * **Requirements:** * Implement functions to convert input RGB/BGR arrays to the CIE $L^*a^*b^*$ and YCrCb color spaces. * Extract the $L^*$ (Luminance) channel as a 32-bit floating-point normalized matrix ($[0.0, 1.0]$) and an 8-bit unsigned integer matrix ($[0, 255]$). * Provide bidirectional reconstruction utilities to recombine processed luminance with untouched or edge-smoothed chrominance components without clipping artifacts or gamut distortion. #### 2. Directional Structuring Element and Filter Bank Module (`filter_bank.py`) * **Objective:** Capture anisotropic 1D curvilinear hair geometries across arbitrary angular orientations. * **Requirements:** * Construct a parametric filter bank generator that yields line structuring elements $\mathcal{S}_\theta$ across $N$ equidistant angular orientations spanning $\theta \in [0^\circ, 180^\circ)$ (e.g., $0^\circ, 20^\circ, 40^\circ, 60^\circ, 80^\circ, 100^\circ, 120^\circ, 140^\circ, 160^\circ$). * Define structuring elements with configurable kernel length $L \in [5, 15]$ pixels and width $W = 1$ pixel. * Execute White Top-Hat and Black Top-Hat morphological operations across all angular kernels: $$\mathcal{T}_{\text{white}}(L) = L - (L \circ \mathcal{S}_\theta)$$ $$\mathcal{T}_{\text{black}}(L) = (L \bullet \mathcal{S}_\theta) - L$$ * Perform maximum intensity projection across all orientation responses to yield a unified directional morphological response map: $$\mathcal{R}_{\text{hair}}(x, y) = \max_{\theta} \left( \mathcal{T}_{\theta}(x, y) \right)$$ #### 3. Follicle Segmentation and Anatomical Mask Generation (`segmentation.py`) * **Objective:** Generate a binary follicle mask isolating hair fibers while strictly preserving anatomical landmarks (jawline boundaries, nodules, cartilage contours, and ocular regions). * **Requirements:** * Apply adaptive Otsu thresholding combined with local dynamic contrast normalization to the directional morphological response map $\mathcal{R}_{\text{hair}}$. * Implement region-of-interest (ROI) spatial masking using skin-tone detection in the YCrCb/HSV space to prevent false positives in high-contrast facial regions (eyelashes, eyebrows, pupils, nostrils). * Apply elliptical morphological dilation ($\text{kernel size } 3\times3$) to encompass the optical penumbra and transition zones of each hair shaft. #### 4. Multi-Scale Isophote-Driven Inpainting Engine (`inpainting.py`) * **Objective:** Reconstruct missing skin textures beneath the binary hair mask by propagating boundary information along isophote lines. * **Requirements:** * Implement Fast Marching Method (FMM) biharmonic inpainting (Telea and Navier-Stokes partial differential equation schemes) restricted strictly to masked follicle pixels. * Preserve underlying gradient coherence by calculating local gradient vectors $\nabla I$ and propagating radiance along orthogonal tangents $\nabla I^\perp$. * Provide multi-scale pyramid support to process coarse global shading and fine skin micro-texture independently. #### 5. Edge-Preserving Guided Fusion Filter (`guided_filter.py`) * **Objective:** Eliminate residual high-frequency artifacts while retaining true anatomical geometry and edge sharpness. * **Requirements:** * Implement an explicit guided filter algorithm that models the filtered output $q$ as a local linear transform of a guidance image $I$: $$q_i = a_k I_i + b_k, \quad \forall i \in \omega_k$$ * Minimize the cost function over local windows $\omega_k$ of radius $r$ with regularization parameter $\epsilon$: $$E(a_k, b_k) = \sum_{i \in \omega_k} \left( (a_k I_i + b_k - p_i)^2 + \epsilon a_k^2 \right)$$ * Compute local variance $\sigma_k^2$ and mean $\mu_k$ using box filters for $O(N)$ runtime efficiency. * Blend the guided-filtered luminance channel back into the multi-channel representation. #### 6. Deep Learning Inpainting Integration (`lama_engine.py`) * **Objective:** Provide a neural synthesis fallback leveraging Fast Fourier Convolutions (FFCs) for wide receptive field contextual understanding. * **Requirements:** * Implement an inference pipeline loading a pre-trained Large Mask Inpainting (LaMa) model. * Format inputs with appropriate padding (modulo 8 or 16), tensor normalization, and device allocation (CUDA, MPS, or CPU). * Feed the composite image and the extracted directional hair mask to synthesize pore-level skin reconstruction. #### 7. Pipeline CLI and Execution Orchestrator (`main.py`) * **Objective:** Expose a unified, robust Command Line Interface (CLI) supporting single-image and batch execution. * **Requirements:** * Parse arguments for input path, output path, algorithm selection (`mca_guided` vs. `deep_learning`), kernel length, threshold sensitivity, and guided filter radius/epsilon. * Include rigorous input validation, color profile sanity checks, exception handling, and progress logging. * Save both the final processed image and diagnostic intermediate artifacts (directional response map, dilated mask, raw inpainting stage). --- ### Deliverable Requirements Generate all modules listed above with full, production-grade Python code, complete docstrings, precise type annotations, and standard dependencies (`numpy`, `opencv-python`, `scipy`, `torch`, `torchvision`, `pillow`). No function may contain truncated logic, incomplete implementations, or placeholder comments.