Codex Preview Deploy commited on
Commit
5003ea5
·
1 Parent(s): 74d091d

Fix dark tile lighting and carpet reflection halos

Browse files
Files changed (2) hide show
  1. app.py +64 -30
  2. test_floor_geometry.py +40 -0
app.py CHANGED
@@ -511,7 +511,7 @@ moge_model = None
511
  sam2_predictor = None
512
  intrinsic_models = None
513
 
514
- LIGHTING_TRANSFER_VERSION = 3
515
  SHADE_MAP_MIN = 0.08
516
  SHADE_MAP_MAX = 1.85
517
  SHOWROOM_SHADE_MAP_MIN = 0.85
@@ -1428,30 +1428,66 @@ def build_reflection_map(
1428
  if not surface_mask.any():
1429
  return None
1430
 
1431
- mask = surface_mask.astype(np.uint8)
1432
- h, w = mask.shape[:2]
1433
- reflection_candidates: list[np.ndarray] = []
1434
-
1435
- if relative_shading is not None:
1436
- rel = relative_shading.astype(np.float32).copy()
1437
- rel[mask == 0] = 1.0
1438
- broad_sigma = max(6.0, min(h, w) / 44.0)
1439
- broad = cv2.GaussianBlur(rel, (0, 0), sigmaX=broad_sigma, sigmaY=broad_sigma)
1440
- detail = rel / np.maximum(broad, 1e-3)
1441
- reflection_candidates.append(np.clip((detail - 1.05) / 0.30, 0.0, 1.0))
1442
-
1443
- luminance = image_luminance(img_np) / 255.0
1444
- floor_values = luminance[mask > 0]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1445
  floor_values = floor_values[np.isfinite(floor_values)]
1446
  if floor_values.size >= max(256, int(h * w * 0.002)):
1447
- median_lum = float(np.median(floor_values))
1448
- if median_lum > 1e-3:
1449
- filled = luminance.copy()
1450
- filled[mask == 0] = median_lum
1451
- broad_sigma = max(8.0, min(h, w) / 36.0)
1452
- broad = cv2.GaussianBlur(filled, (0, 0), sigmaX=broad_sigma, sigmaY=broad_sigma)
1453
- detail = filled / np.maximum(broad, 1e-3)
1454
- reflection_candidates.append(np.clip((detail - 1.08) / 0.30, 0.0, 1.0))
 
 
 
 
 
 
 
1455
 
1456
  if not reflection_candidates:
1457
  return None
@@ -1460,13 +1496,11 @@ def build_reflection_map(
1460
  # illumination map; transferring it here would turn old material color or
1461
  # texture into an artificial dark reflection on the replacement material.
1462
  reflection = np.maximum.reduce(reflection_candidates).astype(np.float32)
1463
- if min(h, w) > 120:
1464
- sigma = max(0.6, min(h, w) / 520.0)
1465
- reflection = cv2.GaussianBlur(reflection, (0, 0), sigmaX=sigma, sigmaY=sigma)
1466
- reflection = np.clip(reflection, REFLECTION_MAP_MIN, REFLECTION_MAP_MAX)
1467
- if replaced_material_mask is not None and replaced_material_mask.shape[:2] == (h, w):
1468
- reflection[replaced_material_mask > 0] = 0.0
1469
- reflection[mask == 0] = 0.0
1470
  if float(np.max(np.abs(reflection[mask > 0]))) < 0.025:
1471
  return None
1472
  encoded = (reflection - REFLECTION_MAP_MIN) / (REFLECTION_MAP_MAX - REFLECTION_MAP_MIN)
 
511
  sam2_predictor = None
512
  intrinsic_models = None
513
 
514
+ LIGHTING_TRANSFER_VERSION = 4
515
  SHADE_MAP_MIN = 0.08
516
  SHADE_MAP_MAX = 1.85
517
  SHOWROOM_SHADE_MAP_MIN = 0.85
 
1428
  if not surface_mask.any():
1429
  return None
1430
 
1431
+ mask = surface_mask.astype(np.uint8)
1432
+ h, w = mask.shape[:2]
1433
+ reflection_candidates: list[np.ndarray] = []
1434
+ min_side = min(h, w)
1435
+
1436
+ # High-pass filtering before removing a rug or carpet creates a bright halo
1437
+ # just outside its mask. Exclude a scale-aware feather around replaced
1438
+ # material, and a narrow surface boundary, before extracting reflections.
1439
+ boundary_radius = max(1, int(round(min_side / 320.0)))
1440
+ boundary_kernel = cv2.getStructuringElement(
1441
+ cv2.MORPH_ELLIPSE,
1442
+ (boundary_radius * 2 + 1, boundary_radius * 2 + 1),
1443
+ )
1444
+ reflection_surface = cv2.erode(mask, boundary_kernel) > 0
1445
+ replacement_exclusion = np.zeros((h, w), dtype=bool)
1446
+ if replaced_material_mask is not None and replaced_material_mask.shape[:2] == (h, w):
1447
+ replacement = ((replaced_material_mask > 0) & (mask > 0)).astype(np.uint8)
1448
+ if replacement.any():
1449
+ replacement_radius = max(3, int(round(min_side / 80.0)))
1450
+ replacement_kernel = cv2.getStructuringElement(
1451
+ cv2.MORPH_ELLIPSE,
1452
+ (replacement_radius * 2 + 1, replacement_radius * 2 + 1),
1453
+ )
1454
+ replacement_exclusion = cv2.dilate(replacement, replacement_kernel) > 0
1455
+ reflection_surface &= ~replacement_exclusion
1456
+
1457
+ if relative_shading is not None:
1458
+ rel = relative_shading.astype(np.float32).copy()
1459
+ rel = suppress_replaced_material_lighting(
1460
+ rel,
1461
+ mask,
1462
+ replacement_exclusion.astype(np.uint8),
1463
+ )
1464
+ rel[mask == 0] = 1.0
1465
+ broad_sigma = max(6.0, min(h, w) / 44.0)
1466
+ broad = cv2.GaussianBlur(rel, (0, 0), sigmaX=broad_sigma, sigmaY=broad_sigma)
1467
+ detail = rel / np.maximum(broad, 1e-3)
1468
+ candidate = np.clip((detail - 1.05) / 0.30, 0.0, 1.0)
1469
+ broad_excess = np.clip((broad - 1.05) / 0.45, 0.0, 1.0)
1470
+ reflection_candidates.append(candidate * (1.0 - 0.75 * broad_excess))
1471
+
1472
+ luminance = image_luminance(img_np) / 255.0
1473
+ floor_values = luminance[mask > 0]
1474
  floor_values = floor_values[np.isfinite(floor_values)]
1475
  if floor_values.size >= max(256, int(h * w * 0.002)):
1476
+ median_lum = float(np.median(floor_values))
1477
+ if median_lum > 1e-3:
1478
+ filled = luminance / median_lum
1479
+ filled = suppress_replaced_material_lighting(
1480
+ filled,
1481
+ mask,
1482
+ replacement_exclusion.astype(np.uint8),
1483
+ )
1484
+ filled[mask == 0] = 1.0
1485
+ broad_sigma = max(8.0, min(h, w) / 36.0)
1486
+ broad = cv2.GaussianBlur(filled, (0, 0), sigmaX=broad_sigma, sigmaY=broad_sigma)
1487
+ detail = filled / np.maximum(broad, 1e-3)
1488
+ candidate = np.clip((detail - 1.08) / 0.30, 0.0, 1.0)
1489
+ broad_excess = np.clip((broad - 1.05) / 0.45, 0.0, 1.0)
1490
+ reflection_candidates.append(candidate * (1.0 - 0.75 * broad_excess))
1491
 
1492
  if not reflection_candidates:
1493
  return None
 
1496
  # illumination map; transferring it here would turn old material color or
1497
  # texture into an artificial dark reflection on the replacement material.
1498
  reflection = np.maximum.reduce(reflection_candidates).astype(np.float32)
1499
+ if min(h, w) > 120:
1500
+ sigma = max(0.6, min(h, w) / 520.0)
1501
+ reflection = cv2.GaussianBlur(reflection, (0, 0), sigmaX=sigma, sigmaY=sigma)
1502
+ reflection = np.clip(reflection, REFLECTION_MAP_MIN, min(REFLECTION_MAP_MAX, 0.55))
1503
+ reflection[~reflection_surface] = 0.0
 
 
1504
  if float(np.max(np.abs(reflection[mask > 0]))) < 0.025:
1505
  return None
1506
  encoded = (reflection - REFLECTION_MAP_MIN) / (REFLECTION_MAP_MAX - REFLECTION_MAP_MIN)
test_floor_geometry.py CHANGED
@@ -191,6 +191,46 @@ class MetricFloorGeometryTests(unittest.TestCase):
191
  self.assertLessEqual(float(decoded[64, 48]), 0.02)
192
  self.assertGreater(float(decoded[42:86, 92:132].max()), 0.0)
193
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
194
  def test_intrinsic_shading_inversion_is_corrected_against_room_luminance(self):
195
  height, width = 160, 180
196
  image = np.zeros((height, width, 3), dtype=np.uint8)
 
191
  self.assertLessEqual(float(decoded[64, 48]), 0.02)
192
  self.assertGreater(float(decoded[42:86, 92:132].max()), 0.0)
193
 
194
+ def test_reflection_map_excludes_replaced_carpet_and_its_boundary(self):
195
+ height, width = 240, 320
196
+ image = np.full((height, width, 3), 105, dtype=np.uint8)
197
+ image[55:205, 85:255, :] = 245
198
+ image[18:30, 18:30, :] = 235
199
+ surface_mask = np.ones((height, width), dtype=np.uint8)
200
+ carpet_mask = np.zeros((height, width), dtype=np.uint8)
201
+ carpet_mask[55:205, 85:255] = 1
202
+
203
+ reflection = app.build_reflection_map(
204
+ image,
205
+ surface_mask,
206
+ replaced_material_mask=carpet_mask,
207
+ )
208
+
209
+ self.assertIsNotNone(reflection)
210
+ boundary = np.zeros_like(carpet_mask, dtype=bool)
211
+ boundary[51:209, 81:259] = True
212
+ boundary[55:205, 85:255] = False
213
+ self.assertEqual(int(reflection[carpet_mask > 0].max()), 0)
214
+ self.assertEqual(int(reflection[boundary].max()), 0)
215
+
216
+ def test_broad_highlight_stays_in_shade_instead_of_reflection(self):
217
+ height, width = 240, 320
218
+ image = np.full((height, width, 3), 115, dtype=np.uint8)
219
+ image[:, 170:, :] = 205
220
+ surface_mask = np.ones((height, width), dtype=np.uint8)
221
+
222
+ maps = app.build_luminance_lighting_maps(image, surface_mask)
223
+
224
+ self.assertIsNotNone(maps)
225
+ shade = app.decode_relative_map(
226
+ maps["shadeMap"],
227
+ app.SHADE_MAP_MIN,
228
+ app.SHADE_MAP_MAX,
229
+ )
230
+ reflection = maps["reflectionMap"].astype(np.float32) / 255.0
231
+ self.assertGreater(float(np.median(shade[:, 230:285])), 1.20)
232
+ self.assertLess(float(np.median(reflection[:, 230:285])), 0.03)
233
+
234
  def test_intrinsic_shading_inversion_is_corrected_against_room_luminance(self):
235
  height, width = 160, 180
236
  image = np.zeros((height, width, 3), dtype=np.uint8)