File size: 4,416 Bytes
1fe7075
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
"""Reflection map extraction for specular highlights on replacement materials."""

from __future__ import annotations

import cv2
import numpy as np

from config.lighting import (
    REFLECTION_MAP_MAX,
    REFLECTION_MAP_MIN,
)
from services.lighting.luminance import image_luminance
from services.lighting.shading import suppress_replaced_material_lighting


def build_reflection_map(
    img_np: np.ndarray,
    surface_mask: np.ndarray,
    relative_shading: np.ndarray | None = None,
    replaced_material_mask: np.ndarray | None = None,
) -> np.ndarray | None:
    if not surface_mask.any():
        return None

    mask = surface_mask.astype(np.uint8)
    h, w = mask.shape[:2]
    reflection_candidates: list[np.ndarray] = []
    min_side = min(h, w)

    # High-pass filtering before removing a rug or carpet creates a bright halo
    # just outside its mask. Exclude a scale-aware feather around replaced
    # material, and a narrow surface boundary, before extracting reflections.
    boundary_radius = max(1, int(round(min_side / 320.0)))
    boundary_kernel = cv2.getStructuringElement(
        cv2.MORPH_ELLIPSE,
        (boundary_radius * 2 + 1, boundary_radius * 2 + 1),
    )
    reflection_surface = cv2.erode(mask, boundary_kernel) > 0
    replacement_exclusion = np.zeros((h, w), dtype=bool)
    if replaced_material_mask is not None and replaced_material_mask.shape[:2] == (h, w):
        replacement = ((replaced_material_mask > 0) & (mask > 0)).astype(np.uint8)
        if replacement.any():
            replacement_radius = max(3, int(round(min_side / 80.0)))
            replacement_kernel = cv2.getStructuringElement(
                cv2.MORPH_ELLIPSE,
                (replacement_radius * 2 + 1, replacement_radius * 2 + 1),
            )
            replacement_exclusion = cv2.dilate(replacement, replacement_kernel) > 0
            reflection_surface &= ~replacement_exclusion

    if relative_shading is not None:
        rel = relative_shading.astype(np.float32).copy()
        rel = suppress_replaced_material_lighting(
            rel,
            mask,
            replacement_exclusion.astype(np.uint8),
        )
        rel[mask == 0] = 1.0
        broad_sigma = max(6.0, min(h, w) / 44.0)
        broad = cv2.GaussianBlur(rel, (0, 0), sigmaX=broad_sigma, sigmaY=broad_sigma)
        detail = rel / np.maximum(broad, 1e-3)
        candidate = np.clip((detail - 1.05) / 0.30, 0.0, 1.0)
        broad_excess = np.clip((broad - 1.05) / 0.45, 0.0, 1.0)
        reflection_candidates.append(candidate * (1.0 - 0.75 * broad_excess))

    luminance = image_luminance(img_np) / 255.0
    floor_values = luminance[mask > 0]
    floor_values = floor_values[np.isfinite(floor_values)]
    if floor_values.size >= max(256, int(h * w * 0.002)):
        median_lum = float(np.median(floor_values))
        if median_lum > 1e-3:
            filled = luminance / median_lum
            filled = suppress_replaced_material_lighting(
                filled,
                mask,
                replacement_exclusion.astype(np.uint8),
            )
            filled[mask == 0] = 1.0
            broad_sigma = max(8.0, min(h, w) / 36.0)
            broad = cv2.GaussianBlur(filled, (0, 0), sigmaX=broad_sigma, sigmaY=broad_sigma)
            detail = filled / np.maximum(broad, 1e-3)
            candidate = np.clip((detail - 1.08) / 0.30, 0.0, 1.0)
            broad_excess = np.clip((broad - 1.05) / 0.45, 0.0, 1.0)
            reflection_candidates.append(candidate * (1.0 - 0.75 * broad_excess))

    if not reflection_candidates:
        return None

    # A reflection is additive light. Dark source-floor detail belongs to the
    # illumination map; transferring it here would turn old material color or
    # texture into an artificial dark reflection on the replacement material.
    reflection = np.maximum.reduce(reflection_candidates).astype(np.float32)
    if min(h, w) > 120:
        sigma = max(0.6, min(h, w) / 520.0)
        reflection = cv2.GaussianBlur(reflection, (0, 0), sigmaX=sigma, sigmaY=sigma)
    reflection = np.clip(reflection, REFLECTION_MAP_MIN, min(REFLECTION_MAP_MAX, 0.20))
    reflection[~reflection_surface] = 0.0
    if float(np.max(np.abs(reflection[mask > 0]))) < 0.025:
        return None
    encoded = (reflection - REFLECTION_MAP_MIN) / (REFLECTION_MAP_MAX - REFLECTION_MAP_MIN)
    return np.round(encoded * 255.0).clip(0, 255).astype(np.uint8)