Codex Preview Deploy commited on
Commit
c728d08
·
1 Parent(s): 35e8281

Preserve observed shadows with intrinsic lighting (eb70748)

Browse files
Files changed (2) hide show
  1. app.py +41 -5
  2. test_floor_geometry.py +15 -0
app.py CHANGED
@@ -1679,7 +1679,33 @@ def lighting_map_statistics(encoded: np.ndarray | None, surface_mask: np.ndarray
1679
  }
1680
 
1681
 
1682
- def build_intrinsic_lighting_maps(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1683
  img_np: np.ndarray,
1684
  surface_mask: np.ndarray,
1685
  ) -> dict[str, np.ndarray | None] | None:
@@ -1698,10 +1724,20 @@ def build_intrinsic_lighting_maps(
1698
  if relative is None:
1699
  return None
1700
  relative, alignment = align_intrinsic_shading_to_luminance(relative, img_np, surface_mask)
1701
- smoothed = smooth_relative_lighting(relative, surface_mask)
1702
- if smoothed is None:
1703
- return None
1704
- print(f"[LIGHTING] intrinsic shading alignment={alignment}", flush=True)
 
 
 
 
 
 
 
 
 
 
1705
 
1706
  return {
1707
  "shadeMap": encode_relative_map(smoothed, SHADE_MAP_MIN, SHADE_MAP_MAX),
 
1679
  }
1680
 
1681
 
1682
+ def decode_relative_map(encoded: np.ndarray, minimum: float, maximum: float) -> np.ndarray:
1683
+ return minimum + (encoded.astype(np.float32) / 255.0) * (maximum - minimum)
1684
+
1685
+
1686
+ def preserve_observed_lighting(
1687
+ intrinsic_relative: np.ndarray,
1688
+ observed_relative: np.ndarray,
1689
+ surface_mask: np.ndarray,
1690
+ ) -> np.ndarray:
1691
+ """Keep observed room shadows and highlights from being weakened by intrinsic decomposition."""
1692
+ fused = intrinsic_relative.astype(np.float32).copy()
1693
+ shadow_pixels = (surface_mask > 0) & (observed_relative < 1.0)
1694
+ highlight_pixels = (surface_mask > 0) & (observed_relative > 1.0)
1695
+ fused[shadow_pixels] = np.minimum(
1696
+ fused[shadow_pixels],
1697
+ observed_relative[shadow_pixels],
1698
+ )
1699
+ fused[highlight_pixels] = np.maximum(
1700
+ fused[highlight_pixels],
1701
+ observed_relative[highlight_pixels],
1702
+ )
1703
+ fused = np.clip(fused, SHADE_MAP_MIN, 1.65)
1704
+ fused[surface_mask == 0] = 1.0
1705
+ return fused
1706
+
1707
+
1708
+ def build_intrinsic_lighting_maps(
1709
  img_np: np.ndarray,
1710
  surface_mask: np.ndarray,
1711
  ) -> dict[str, np.ndarray | None] | None:
 
1724
  if relative is None:
1725
  return None
1726
  relative, alignment = align_intrinsic_shading_to_luminance(relative, img_np, surface_mask)
1727
+ smoothed = smooth_relative_lighting(relative, surface_mask)
1728
+ if smoothed is None:
1729
+ return None
1730
+
1731
+ luminance_maps = build_luminance_lighting_maps(img_np, surface_mask)
1732
+ if luminance_maps and luminance_maps.get("shadeMap") is not None:
1733
+ observed = decode_relative_map(
1734
+ luminance_maps["shadeMap"],
1735
+ SHADE_MAP_MIN,
1736
+ SHADE_MAP_MAX,
1737
+ )
1738
+ smoothed = preserve_observed_lighting(smoothed, observed, surface_mask)
1739
+ alignment = f"{alignment}+observed-lighting"
1740
+ print(f"[LIGHTING] intrinsic shading alignment={alignment}", flush=True)
1741
 
1742
  return {
1743
  "shadeMap": encode_relative_map(smoothed, SHADE_MAP_MIN, SHADE_MAP_MAX),
test_floor_geometry.py CHANGED
@@ -132,6 +132,21 @@ class MetricFloorGeometryTests(unittest.TestCase):
132
  self.assertGreater(shadow_contrast, 0.25)
133
  self.assertLess(stripe_contrast, 0.035)
134
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135
  def test_reflection_map_only_transfers_bright_highlights(self):
136
  height, width = 128, 160
137
  image = np.full((height, width, 3), 128, dtype=np.uint8)
 
132
  self.assertGreater(shadow_contrast, 0.25)
133
  self.assertLess(stripe_contrast, 0.035)
134
 
135
+ def test_intrinsic_fusion_cannot_weaken_observed_shadow_or_highlight(self):
136
+ mask = np.ones((8, 12), dtype=np.uint8)
137
+ intrinsic = np.ones((8, 12), dtype=np.float32)
138
+ intrinsic[:, :4] = 0.72
139
+ intrinsic[:, 8:] = 1.18
140
+ observed = np.ones((8, 12), dtype=np.float32)
141
+ observed[:, :4] = 0.18
142
+ observed[:, 8:] = 1.55
143
+
144
+ fused = app.preserve_observed_lighting(intrinsic, observed, mask)
145
+
146
+ self.assertTrue(np.all(fused[:, :4] <= observed[:, :4]))
147
+ self.assertTrue(np.all(fused[:, 8:] >= observed[:, 8:]))
148
+ self.assertTrue(np.allclose(fused[:, 4:8], 1.0))
149
+
150
  def test_reflection_map_only_transfers_bright_highlights(self):
151
  height, width = 128, 160
152
  image = np.full((height, width, 3), 128, dtype=np.uint8)