github-actions[bot] commited on
Commit ·
c989fa6
1
Parent(s): 7ef7332
Automated Deploy: 599c59e5f990c2ee3ee1a6ecfb9c4842e22c57c8
Browse files- app.py +116 -32
- floor_direction.py +229 -7
- test_floor_geometry.py +137 -3
app.py
CHANGED
|
@@ -367,7 +367,7 @@ SHOWROOM_SHADE_MAP_MIN = 0.85
|
|
| 367 |
SHOWROOM_SHADE_MAP_MAX = 1.12
|
| 368 |
REFLECTION_RELATIVE_FLOOR = 1.04
|
| 369 |
REFLECTION_RELATIVE_CEILING = 1.95
|
| 370 |
-
REFLECTION_MAP_MIN =
|
| 371 |
REFLECTION_MAP_MAX = 1.0
|
| 372 |
|
| 373 |
|
|
@@ -900,7 +900,7 @@ def build_reflection_map(
|
|
| 900 |
broad_sigma = max(6.0, min(h, w) / 44.0)
|
| 901 |
broad = cv2.GaussianBlur(rel, (0, 0), sigmaX=broad_sigma, sigmaY=broad_sigma)
|
| 902 |
detail = rel / np.maximum(broad, 1e-3)
|
| 903 |
-
reflection_candidates.append(np.clip((detail - 1.
|
| 904 |
|
| 905 |
luminance = image_luminance(img_np) / 255.0
|
| 906 |
floor_values = luminance[mask > 0]
|
|
@@ -913,12 +913,15 @@ def build_reflection_map(
|
|
| 913 |
broad_sigma = max(8.0, min(h, w) / 36.0)
|
| 914 |
broad = cv2.GaussianBlur(filled, (0, 0), sigmaX=broad_sigma, sigmaY=broad_sigma)
|
| 915 |
detail = filled / np.maximum(broad, 1e-3)
|
| 916 |
-
reflection_candidates.append(np.clip((detail - 1.
|
| 917 |
|
| 918 |
if not reflection_candidates:
|
| 919 |
return None
|
| 920 |
|
| 921 |
-
reflection
|
|
|
|
|
|
|
|
|
|
| 922 |
if min(h, w) > 120:
|
| 923 |
sigma = max(0.6, min(h, w) / 520.0)
|
| 924 |
reflection = cv2.GaussianBlur(reflection, (0, 0), sigmaX=sigma, sigmaY=sigma)
|
|
@@ -993,7 +996,7 @@ def extract_intrinsic_shading(results: dict) -> np.ndarray | None:
|
|
| 993 |
return shading.astype(np.float32)
|
| 994 |
|
| 995 |
|
| 996 |
-
def normalize_relative_shading(
|
| 997 |
shading: np.ndarray,
|
| 998 |
surface_mask: np.ndarray,
|
| 999 |
) -> np.ndarray | None:
|
|
@@ -1013,8 +1016,100 @@ def normalize_relative_shading(
|
|
| 1013 |
|
| 1014 |
relative = shading / median_val
|
| 1015 |
relative[~np.isfinite(relative)] = 1.0
|
| 1016 |
-
relative[surface_mask == 0] = 1.0
|
| 1017 |
-
return relative.astype(np.float32)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1018 |
|
| 1019 |
|
| 1020 |
def build_intrinsic_lighting_maps(
|
|
@@ -1032,30 +1127,19 @@ def build_intrinsic_lighting_maps(
|
|
| 1032 |
if shading is None:
|
| 1033 |
return None
|
| 1034 |
|
| 1035 |
-
relative = normalize_relative_shading(shading, surface_mask)
|
| 1036 |
-
if relative is None:
|
| 1037 |
-
return None
|
| 1038 |
-
|
| 1039 |
-
|
| 1040 |
-
smoothed
|
| 1041 |
-
|
| 1042 |
-
|
| 1043 |
-
|
| 1044 |
-
|
| 1045 |
-
|
| 1046 |
-
|
| 1047 |
-
)
|
| 1048 |
-
except cv2.error:
|
| 1049 |
-
smoothed = relative
|
| 1050 |
-
sigma = max(0.6, min(h, w) / 420.0)
|
| 1051 |
-
smoothed = cv2.GaussianBlur(smoothed, (0, 0), sigmaX=sigma, sigmaY=sigma)
|
| 1052 |
-
smoothed = np.clip(smoothed, SHADE_MAP_MIN, SHADE_MAP_MAX)
|
| 1053 |
-
smoothed[surface_mask == 0] = 1.0
|
| 1054 |
-
|
| 1055 |
-
return {
|
| 1056 |
-
"shadeMap": encode_relative_map(smoothed, SHADE_MAP_MIN, SHADE_MAP_MAX),
|
| 1057 |
-
"showroomShadeMap": build_showroom_shade_map_from_relative(relative, surface_mask),
|
| 1058 |
-
"reflectionMap": build_reflection_map(img_np, surface_mask, relative),
|
| 1059 |
}
|
| 1060 |
except Exception as exc:
|
| 1061 |
print(f"Intrinsic shading decomposition failed: {exc}. Falling back to default luminance shading.", flush=True)
|
|
@@ -2945,7 +3029,7 @@ def build_segmentation_bundle(contents: bytes):
|
|
| 2945 |
"showroomShadeMapRange": [SHOWROOM_SHADE_MAP_MIN, SHOWROOM_SHADE_MAP_MAX],
|
| 2946 |
"renderPresetDefaults": {
|
| 2947 |
"preset": "realistic",
|
| 2948 |
-
"shadingStrength": 0.
|
| 2949 |
"reflectionTransfer": True,
|
| 2950 |
"edgeFeatherPixels": 2,
|
| 2951 |
},
|
|
|
|
| 367 |
SHOWROOM_SHADE_MAP_MAX = 1.12
|
| 368 |
REFLECTION_RELATIVE_FLOOR = 1.04
|
| 369 |
REFLECTION_RELATIVE_CEILING = 1.95
|
| 370 |
+
REFLECTION_MAP_MIN = 0.0
|
| 371 |
REFLECTION_MAP_MAX = 1.0
|
| 372 |
|
| 373 |
|
|
|
|
| 900 |
broad_sigma = max(6.0, min(h, w) / 44.0)
|
| 901 |
broad = cv2.GaussianBlur(rel, (0, 0), sigmaX=broad_sigma, sigmaY=broad_sigma)
|
| 902 |
detail = rel / np.maximum(broad, 1e-3)
|
| 903 |
+
reflection_candidates.append(np.clip((detail - 1.05) / 0.30, 0.0, 1.0))
|
| 904 |
|
| 905 |
luminance = image_luminance(img_np) / 255.0
|
| 906 |
floor_values = luminance[mask > 0]
|
|
|
|
| 913 |
broad_sigma = max(8.0, min(h, w) / 36.0)
|
| 914 |
broad = cv2.GaussianBlur(filled, (0, 0), sigmaX=broad_sigma, sigmaY=broad_sigma)
|
| 915 |
detail = filled / np.maximum(broad, 1e-3)
|
| 916 |
+
reflection_candidates.append(np.clip((detail - 1.08) / 0.30, 0.0, 1.0))
|
| 917 |
|
| 918 |
if not reflection_candidates:
|
| 919 |
return None
|
| 920 |
|
| 921 |
+
# A reflection is additive light. Dark source-floor detail belongs to the
|
| 922 |
+
# illumination map; transferring it here would turn old material color or
|
| 923 |
+
# texture into an artificial dark reflection on the replacement material.
|
| 924 |
+
reflection = np.maximum.reduce(reflection_candidates).astype(np.float32)
|
| 925 |
if min(h, w) > 120:
|
| 926 |
sigma = max(0.6, min(h, w) / 520.0)
|
| 927 |
reflection = cv2.GaussianBlur(reflection, (0, 0), sigmaX=sigma, sigmaY=sigma)
|
|
|
|
| 996 |
return shading.astype(np.float32)
|
| 997 |
|
| 998 |
|
| 999 |
+
def normalize_relative_shading(
|
| 1000 |
shading: np.ndarray,
|
| 1001 |
surface_mask: np.ndarray,
|
| 1002 |
) -> np.ndarray | None:
|
|
|
|
| 1016 |
|
| 1017 |
relative = shading / median_val
|
| 1018 |
relative[~np.isfinite(relative)] = 1.0
|
| 1019 |
+
relative[surface_mask == 0] = 1.0
|
| 1020 |
+
return relative.astype(np.float32)
|
| 1021 |
+
|
| 1022 |
+
|
| 1023 |
+
def masked_gaussian_blur(
|
| 1024 |
+
values: np.ndarray,
|
| 1025 |
+
surface_mask: np.ndarray,
|
| 1026 |
+
sigma: float,
|
| 1027 |
+
) -> np.ndarray:
|
| 1028 |
+
mask = surface_mask.astype(np.float32)
|
| 1029 |
+
numerator = cv2.GaussianBlur(values.astype(np.float32) * mask, (0, 0), sigmaX=sigma, sigmaY=sigma)
|
| 1030 |
+
denominator = cv2.GaussianBlur(mask, (0, 0), sigmaX=sigma, sigmaY=sigma)
|
| 1031 |
+
return numerator / np.maximum(denominator, 1e-4)
|
| 1032 |
+
|
| 1033 |
+
|
| 1034 |
+
def normalize_relative_field(values: np.ndarray, surface_mask: np.ndarray) -> np.ndarray | None:
|
| 1035 |
+
relative = values.astype(np.float32).copy()
|
| 1036 |
+
valid = relative[surface_mask > 0]
|
| 1037 |
+
valid = valid[np.isfinite(valid) & (valid > 1e-6)]
|
| 1038 |
+
if valid.size == 0:
|
| 1039 |
+
return None
|
| 1040 |
+
median = float(np.median(valid))
|
| 1041 |
+
if median < 1e-6:
|
| 1042 |
+
return None
|
| 1043 |
+
relative /= median
|
| 1044 |
+
relative[~np.isfinite(relative)] = 1.0
|
| 1045 |
+
relative[surface_mask == 0] = 1.0
|
| 1046 |
+
return relative
|
| 1047 |
+
|
| 1048 |
+
|
| 1049 |
+
def broad_luminance_relative(
|
| 1050 |
+
img_np: np.ndarray,
|
| 1051 |
+
surface_mask: np.ndarray,
|
| 1052 |
+
) -> np.ndarray | None:
|
| 1053 |
+
luminance = image_luminance(img_np) / 255.0
|
| 1054 |
+
floor_values = luminance[surface_mask > 0]
|
| 1055 |
+
floor_values = floor_values[np.isfinite(floor_values) & (floor_values > 1e-6)]
|
| 1056 |
+
if floor_values.size == 0:
|
| 1057 |
+
return None
|
| 1058 |
+
sigma = max(12.0, min(surface_mask.shape[:2]) / 32.0)
|
| 1059 |
+
broad = masked_gaussian_blur(luminance, surface_mask, sigma)
|
| 1060 |
+
return normalize_relative_field(broad, surface_mask)
|
| 1061 |
+
|
| 1062 |
+
|
| 1063 |
+
def align_intrinsic_shading_to_luminance(
|
| 1064 |
+
relative_shading: np.ndarray,
|
| 1065 |
+
img_np: np.ndarray,
|
| 1066 |
+
surface_mask: np.ndarray,
|
| 1067 |
+
) -> tuple[np.ndarray, str]:
|
| 1068 |
+
"""Correct intrinsic maps that encode lightness with the opposite polarity."""
|
| 1069 |
+
observed = broad_luminance_relative(img_np, surface_mask)
|
| 1070 |
+
if observed is None:
|
| 1071 |
+
return relative_shading, "intrinsic-unchecked"
|
| 1072 |
+
|
| 1073 |
+
sigma = max(12.0, min(surface_mask.shape[:2]) / 32.0)
|
| 1074 |
+
intrinsic_broad = masked_gaussian_blur(relative_shading, surface_mask, sigma)
|
| 1075 |
+
intrinsic_broad = normalize_relative_field(intrinsic_broad, surface_mask)
|
| 1076 |
+
if intrinsic_broad is None:
|
| 1077 |
+
return relative_shading, "intrinsic-unchecked"
|
| 1078 |
+
|
| 1079 |
+
valid = (
|
| 1080 |
+
(surface_mask > 0)
|
| 1081 |
+
& np.isfinite(observed)
|
| 1082 |
+
& np.isfinite(intrinsic_broad)
|
| 1083 |
+
& (observed > 1e-4)
|
| 1084 |
+
& (intrinsic_broad > 1e-4)
|
| 1085 |
+
)
|
| 1086 |
+
if int(valid.sum()) < 256:
|
| 1087 |
+
return relative_shading, "intrinsic-unchecked"
|
| 1088 |
+
|
| 1089 |
+
observed_log = np.log(observed[valid])
|
| 1090 |
+
intrinsic_log = np.log(intrinsic_broad[valid])
|
| 1091 |
+
if float(np.std(observed_log)) < 1e-4 or float(np.std(intrinsic_log)) < 1e-4:
|
| 1092 |
+
return relative_shading, "intrinsic-unchecked"
|
| 1093 |
+
|
| 1094 |
+
correlation = float(np.corrcoef(observed_log, intrinsic_log)[0, 1])
|
| 1095 |
+
if correlation < -0.12:
|
| 1096 |
+
corrected = normalize_relative_field(1.0 / np.maximum(relative_shading, 1e-4), surface_mask)
|
| 1097 |
+
if corrected is not None:
|
| 1098 |
+
return corrected, "intrinsic-inverted-corrected"
|
| 1099 |
+
return relative_shading, "intrinsic-aligned" if correlation >= 0.10 else "intrinsic-low-confidence"
|
| 1100 |
+
|
| 1101 |
+
|
| 1102 |
+
def smooth_relative_lighting(relative_shading: np.ndarray, surface_mask: np.ndarray) -> np.ndarray | None:
|
| 1103 |
+
"""Keep room-scale illumination while rejecting old floor texture and grout."""
|
| 1104 |
+
sigma = max(12.0, min(surface_mask.shape[:2]) / 32.0)
|
| 1105 |
+
bounded = np.clip(relative_shading.astype(np.float32), 0.25, 4.0)
|
| 1106 |
+
smooth = masked_gaussian_blur(bounded, surface_mask, sigma)
|
| 1107 |
+
normalized = normalize_relative_field(smooth, surface_mask)
|
| 1108 |
+
if normalized is None:
|
| 1109 |
+
return None
|
| 1110 |
+
normalized = np.clip(normalized, 0.60, 1.45)
|
| 1111 |
+
normalized[surface_mask == 0] = 1.0
|
| 1112 |
+
return normalized
|
| 1113 |
|
| 1114 |
|
| 1115 |
def build_intrinsic_lighting_maps(
|
|
|
|
| 1127 |
if shading is None:
|
| 1128 |
return None
|
| 1129 |
|
| 1130 |
+
relative = normalize_relative_shading(shading, surface_mask)
|
| 1131 |
+
if relative is None:
|
| 1132 |
+
return None
|
| 1133 |
+
relative, alignment = align_intrinsic_shading_to_luminance(relative, img_np, surface_mask)
|
| 1134 |
+
smoothed = smooth_relative_lighting(relative, surface_mask)
|
| 1135 |
+
if smoothed is None:
|
| 1136 |
+
return None
|
| 1137 |
+
print(f"[LIGHTING] intrinsic shading alignment={alignment}", flush=True)
|
| 1138 |
+
|
| 1139 |
+
return {
|
| 1140 |
+
"shadeMap": encode_relative_map(smoothed, SHADE_MAP_MIN, SHADE_MAP_MAX),
|
| 1141 |
+
"showroomShadeMap": build_showroom_shade_map_from_relative(smoothed, surface_mask),
|
| 1142 |
+
"reflectionMap": build_reflection_map(img_np, surface_mask, smoothed),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1143 |
}
|
| 1144 |
except Exception as exc:
|
| 1145 |
print(f"Intrinsic shading decomposition failed: {exc}. Falling back to default luminance shading.", flush=True)
|
|
|
|
| 3029 |
"showroomShadeMapRange": [SHOWROOM_SHADE_MAP_MIN, SHOWROOM_SHADE_MAP_MAX],
|
| 3030 |
"renderPresetDefaults": {
|
| 3031 |
"preset": "realistic",
|
| 3032 |
+
"shadingStrength": 0.55,
|
| 3033 |
"reflectionTransfer": True,
|
| 3034 |
"edgeFeatherPixels": 2,
|
| 3035 |
},
|
floor_direction.py
CHANGED
|
@@ -305,6 +305,7 @@ def analyze_floor_direction(
|
|
| 305 |
angle_degrees, orientation_source = selected_orientation
|
| 306 |
render_angle_degrees = estimate_surface_uv_grout_rotation(
|
| 307 |
lines,
|
|
|
|
| 308 |
surface_uv=surface_uv,
|
| 309 |
surface_indices=surface_indices,
|
| 310 |
surface_plane_ids=surface_plane_ids,
|
|
@@ -735,30 +736,251 @@ def estimate_rectified_grout_rotation(
|
|
| 735 |
def estimate_surface_uv_grout_rotation(
|
| 736 |
lines: list[DetectedLine],
|
| 737 |
*,
|
|
|
|
| 738 |
surface_uv: np.ndarray | None,
|
| 739 |
surface_indices: np.ndarray | None,
|
| 740 |
surface_plane_ids: np.ndarray | None,
|
| 741 |
image_shape: tuple[int, int],
|
| 742 |
) -> float | None:
|
| 743 |
-
"""Measure
|
| 744 |
uv_grid, plane_grid = build_surface_uv_grids(
|
| 745 |
surface_uv=surface_uv,
|
| 746 |
surface_indices=surface_indices,
|
| 747 |
surface_plane_ids=surface_plane_ids,
|
| 748 |
image_shape=image_shape,
|
| 749 |
)
|
| 750 |
-
if uv_grid is None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 751 |
return None
|
| 752 |
|
| 753 |
uv_lines = project_lines_to_surface_uv(lines, uv_grid, plane_grid)
|
| 754 |
-
|
| 755 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 756 |
return None
|
| 757 |
|
| 758 |
-
|
| 759 |
-
if
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 760 |
return None
|
| 761 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 762 |
|
| 763 |
|
| 764 |
def build_surface_uv_grids(
|
|
|
|
| 305 |
angle_degrees, orientation_source = selected_orientation
|
| 306 |
render_angle_degrees = estimate_surface_uv_grout_rotation(
|
| 307 |
lines,
|
| 308 |
+
image_bgr=image_bgr,
|
| 309 |
surface_uv=surface_uv,
|
| 310 |
surface_indices=surface_indices,
|
| 311 |
surface_plane_ids=surface_plane_ids,
|
|
|
|
| 736 |
def estimate_surface_uv_grout_rotation(
|
| 737 |
lines: list[DetectedLine],
|
| 738 |
*,
|
| 739 |
+
image_bgr: np.ndarray | None = None,
|
| 740 |
surface_uv: np.ndarray | None,
|
| 741 |
surface_indices: np.ndarray | None,
|
| 742 |
surface_plane_ids: np.ndarray | None,
|
| 743 |
image_shape: tuple[int, int],
|
| 744 |
) -> float | None:
|
| 745 |
+
"""Measure grout in the same surface-UV plane sampled by the renderer."""
|
| 746 |
uv_grid, plane_grid = build_surface_uv_grids(
|
| 747 |
surface_uv=surface_uv,
|
| 748 |
surface_indices=surface_indices,
|
| 749 |
surface_plane_ids=surface_plane_ids,
|
| 750 |
image_shape=image_shape,
|
| 751 |
)
|
| 752 |
+
if uv_grid is None:
|
| 753 |
+
return None
|
| 754 |
+
|
| 755 |
+
# Image-space Hough families are unreliable for oblique views: parallel floor
|
| 756 |
+
# seams converge, while furniture edges remain parallel. Rectifying the source
|
| 757 |
+
# into surface UV makes the actual grout parallel before it is scored.
|
| 758 |
+
if image_bgr is not None:
|
| 759 |
+
rectified = rectify_floor_image_to_surface_uv(image_bgr, uv_grid, plane_grid)
|
| 760 |
+
if rectified is not None:
|
| 761 |
+
rectified_image, rectified_mask = rectified
|
| 762 |
+
rectified_lines = detect_candidate_lines(rectified_image, rectified_mask)
|
| 763 |
+
rectified_angle = estimate_repeated_grout_orientation(
|
| 764 |
+
rectified_lines,
|
| 765 |
+
image_shape=rectified_mask.shape[:2],
|
| 766 |
+
)
|
| 767 |
+
if rectified_angle is not None:
|
| 768 |
+
return rectified_angle
|
| 769 |
+
|
| 770 |
+
if len(lines) < 4:
|
| 771 |
return None
|
| 772 |
|
| 773 |
uv_lines = project_lines_to_surface_uv(lines, uv_grid, plane_grid)
|
| 774 |
+
return estimate_repeated_grout_orientation(uv_lines)
|
| 775 |
+
|
| 776 |
+
|
| 777 |
+
def rectify_floor_image_to_surface_uv(
|
| 778 |
+
image_bgr: np.ndarray,
|
| 779 |
+
uv_grid: np.ndarray,
|
| 780 |
+
plane_grid: np.ndarray | None,
|
| 781 |
+
) -> tuple[np.ndarray, np.ndarray] | None:
|
| 782 |
+
"""Splat source pixels into a uniform, metric surface-UV image.
|
| 783 |
+
|
| 784 |
+
The output uses one scale for U and V so line angles are directly usable as
|
| 785 |
+
renderer rotations. When the floor contains multiple surfaces, retain only
|
| 786 |
+
the largest assigned plane: their local UV bases are intentionally separate.
|
| 787 |
+
"""
|
| 788 |
+
if image_bgr.shape[:2] != uv_grid.shape[:2]:
|
| 789 |
return None
|
| 790 |
|
| 791 |
+
valid = np.isfinite(uv_grid).all(axis=2)
|
| 792 |
+
if plane_grid is not None:
|
| 793 |
+
plane_values = plane_grid[valid & (plane_grid != 255)]
|
| 794 |
+
if plane_values.size:
|
| 795 |
+
dominant_plane = int(np.bincount(plane_values).argmax())
|
| 796 |
+
valid &= plane_grid == dominant_plane
|
| 797 |
+
if int(valid.sum()) < 600:
|
| 798 |
+
return None
|
| 799 |
+
|
| 800 |
+
uv = uv_grid[valid].astype(np.float64)
|
| 801 |
+
lower = np.percentile(uv, 0.5, axis=0)
|
| 802 |
+
upper = np.percentile(uv, 99.5, axis=0)
|
| 803 |
+
span = upper - lower
|
| 804 |
+
if not np.isfinite(span).all() or float(np.min(span)) <= 1e-5:
|
| 805 |
+
return None
|
| 806 |
+
|
| 807 |
+
# Keep the rectified image detailed enough for grout, without making this
|
| 808 |
+
# CPU-only preprocessing grow with the uploaded image resolution.
|
| 809 |
+
aspect = float(span[0] / span[1])
|
| 810 |
+
target_long_side = int(
|
| 811 |
+
np.clip(
|
| 812 |
+
round(math.sqrt(int(valid.sum()) * max(aspect, 1.0 / aspect))),
|
| 813 |
+
256,
|
| 814 |
+
1024,
|
| 815 |
+
)
|
| 816 |
+
)
|
| 817 |
+
scale = target_long_side / float(np.max(span))
|
| 818 |
+
rectified_width = int(np.clip(round(float(span[0]) * scale) + 1, 96, 1024))
|
| 819 |
+
rectified_height = int(np.clip(round(float(span[1]) * scale) + 1, 96, 1024))
|
| 820 |
+
if rectified_width < 96 or rectified_height < 96:
|
| 821 |
+
return None
|
| 822 |
+
|
| 823 |
+
source_gray = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2GRAY)
|
| 824 |
+
source_values = source_gray[valid].astype(np.float64)
|
| 825 |
+
local_u = uv[:, 0] - lower[0]
|
| 826 |
+
local_v = uv[:, 1] - lower[1]
|
| 827 |
+
xs = np.clip(np.rint(local_u * scale).astype(np.int32), 0, rectified_width - 1)
|
| 828 |
+
ys = np.clip(np.rint(local_v * scale).astype(np.int32), 0, rectified_height - 1)
|
| 829 |
+
flat_indices = ys * rectified_width + xs
|
| 830 |
+
pixel_count = rectified_width * rectified_height
|
| 831 |
+
counts = np.bincount(flat_indices, minlength=pixel_count)
|
| 832 |
+
values = np.bincount(flat_indices, weights=source_values, minlength=pixel_count)
|
| 833 |
+
covered = counts > 0
|
| 834 |
+
if int(covered.sum()) < max(500, int(pixel_count * 0.08)):
|
| 835 |
+
return None
|
| 836 |
+
|
| 837 |
+
fill_value = int(np.median(source_values)) if source_values.size else 0
|
| 838 |
+
rectified_gray = np.full(pixel_count, fill_value, dtype=np.uint8)
|
| 839 |
+
rectified_gray[covered] = np.clip(values[covered] / counts[covered], 0, 255).astype(np.uint8)
|
| 840 |
+
rectified_mask = (covered.reshape(rectified_height, rectified_width).astype(np.uint8) * 255)
|
| 841 |
+
rectified_gray = rectified_gray.reshape(rectified_height, rectified_width)
|
| 842 |
+
return cv2.cvtColor(rectified_gray, cv2.COLOR_GRAY2BGR), rectified_mask
|
| 843 |
+
|
| 844 |
+
|
| 845 |
+
def estimate_repeated_grout_orientation(
|
| 846 |
+
lines: list[DetectedLine],
|
| 847 |
+
*,
|
| 848 |
+
image_shape: tuple[int, int] | None = None,
|
| 849 |
+
) -> float | None:
|
| 850 |
+
"""Select a direction only when it has repeated, regularly spaced seams."""
|
| 851 |
+
if len(lines) < 4:
|
| 852 |
+
return None
|
| 853 |
+
|
| 854 |
+
candidates: list[tuple[float, float]] = []
|
| 855 |
+
for cluster in cluster_line_families(lines, image_shape=image_shape):
|
| 856 |
+
if len(cluster.line_indices) < 4 or cluster.concentration < 0.76:
|
| 857 |
+
continue
|
| 858 |
+
|
| 859 |
+
offsets, offset_weights, median_length = consolidated_line_offsets(
|
| 860 |
+
lines,
|
| 861 |
+
cluster.line_indices,
|
| 862 |
+
angle_degrees=cluster.angle_degrees,
|
| 863 |
+
image_shape=image_shape,
|
| 864 |
+
)
|
| 865 |
+
if len(offsets) < 3:
|
| 866 |
+
continue
|
| 867 |
+
|
| 868 |
+
periodicity = repeated_offset_periodicity(offsets, offset_weights)
|
| 869 |
+
if periodicity < 0.76:
|
| 870 |
+
continue
|
| 871 |
+
|
| 872 |
+
support = min(1.0, len(cluster.line_indices) / 12.0)
|
| 873 |
+
offset_support = min(1.0, (len(offsets) - 1) / 6.0)
|
| 874 |
+
length_support = min(1.0, median_length / max(1.0, offset_span_reference(offsets, image_shape)))
|
| 875 |
+
quality = (
|
| 876 |
+
0.28 * periodicity
|
| 877 |
+
+ 0.24 * cluster.concentration
|
| 878 |
+
+ 0.22 * support
|
| 879 |
+
+ 0.18 * offset_support
|
| 880 |
+
+ 0.08 * length_support
|
| 881 |
+
)
|
| 882 |
+
candidates.append((quality, cluster.angle_degrees))
|
| 883 |
+
|
| 884 |
+
if not candidates:
|
| 885 |
return None
|
| 886 |
+
quality, angle_degrees = max(candidates, key=lambda candidate: candidate[0])
|
| 887 |
+
return angle_degrees if quality >= 0.68 else None
|
| 888 |
+
|
| 889 |
+
|
| 890 |
+
def consolidated_line_offsets(
|
| 891 |
+
lines: list[DetectedLine],
|
| 892 |
+
line_indices: list[int],
|
| 893 |
+
*,
|
| 894 |
+
angle_degrees: float,
|
| 895 |
+
image_shape: tuple[int, int] | None,
|
| 896 |
+
) -> tuple[np.ndarray, np.ndarray, float]:
|
| 897 |
+
center_x, center_y, _min_dimension = hough_offset_reference_frame(
|
| 898 |
+
lines,
|
| 899 |
+
line_indices,
|
| 900 |
+
image_shape=image_shape,
|
| 901 |
+
)
|
| 902 |
+
radians = math.radians(angle_degrees)
|
| 903 |
+
normal_x = -math.sin(radians)
|
| 904 |
+
normal_y = math.cos(radians)
|
| 905 |
+
values = []
|
| 906 |
+
for index in line_indices:
|
| 907 |
+
line = lines[index]
|
| 908 |
+
midpoint_x = (line.x1 + line.x2) * 0.5
|
| 909 |
+
midpoint_y = (line.y1 + line.y2) * 0.5
|
| 910 |
+
offset = (midpoint_x - center_x) * normal_x + (midpoint_y - center_y) * normal_y
|
| 911 |
+
values.append((offset, max(1.0, line.length)))
|
| 912 |
+
if not values:
|
| 913 |
+
return np.empty(0), np.empty(0), 0.0
|
| 914 |
+
|
| 915 |
+
lengths = np.array([weight for _offset, weight in values], dtype=np.float64)
|
| 916 |
+
median_length = float(np.median(lengths))
|
| 917 |
+
tolerance = max(1.5, median_length * 0.035)
|
| 918 |
+
if image_shape is not None:
|
| 919 |
+
# Canny commonly returns the two sides of one grout seam as separate
|
| 920 |
+
# Hough segments after rectification. Merge that pair before testing
|
| 921 |
+
# inter-seam periodicity, while keeping adjacent physical seams apart.
|
| 922 |
+
tolerance = max(tolerance, min(image_shape) * 0.024)
|
| 923 |
+
groups: list[list[float]] = []
|
| 924 |
+
for offset, weight in sorted(values, key=lambda item: item[0]):
|
| 925 |
+
if not groups or abs(offset - groups[-1][0]) > tolerance:
|
| 926 |
+
groups.append([offset, weight])
|
| 927 |
+
continue
|
| 928 |
+
previous_offset, previous_weight = groups[-1]
|
| 929 |
+
total_weight = previous_weight + weight
|
| 930 |
+
groups[-1][0] = (previous_offset * previous_weight + offset * weight) / total_weight
|
| 931 |
+
groups[-1][1] = total_weight
|
| 932 |
+
|
| 933 |
+
return (
|
| 934 |
+
np.array([group[0] for group in groups], dtype=np.float64),
|
| 935 |
+
np.array([group[1] for group in groups], dtype=np.float64),
|
| 936 |
+
median_length,
|
| 937 |
+
)
|
| 938 |
+
|
| 939 |
+
|
| 940 |
+
def repeated_offset_periodicity(offsets: np.ndarray, weights: np.ndarray) -> float:
|
| 941 |
+
"""Return how well seam offsets fit one regularly repeating lattice."""
|
| 942 |
+
if len(offsets) < 3:
|
| 943 |
+
return 0.0
|
| 944 |
+
span = float(offsets[-1] - offsets[0])
|
| 945 |
+
if span <= 1e-6:
|
| 946 |
+
return 0.0
|
| 947 |
+
|
| 948 |
+
pairwise = offsets[np.newaxis, :] - offsets[:, np.newaxis]
|
| 949 |
+
distances = pairwise[np.triu_indices(len(offsets), k=1)]
|
| 950 |
+
distances = distances[distances > 1e-6]
|
| 951 |
+
if not len(distances):
|
| 952 |
+
return 0.0
|
| 953 |
+
|
| 954 |
+
minimum_spacing = span / max(40.0, float(len(offsets) * 4))
|
| 955 |
+
candidates = []
|
| 956 |
+
for distance in distances:
|
| 957 |
+
for divisor in range(1, 7):
|
| 958 |
+
spacing = float(distance / divisor)
|
| 959 |
+
if minimum_spacing <= spacing <= span * 0.85:
|
| 960 |
+
candidates.append(spacing)
|
| 961 |
+
if not candidates:
|
| 962 |
+
return 0.0
|
| 963 |
+
|
| 964 |
+
best = 0.0
|
| 965 |
+
weights = np.maximum(np.asarray(weights, dtype=np.float64), 1.0)
|
| 966 |
+
for spacing in candidates:
|
| 967 |
+
normalized = (offsets - offsets[0]) / spacing
|
| 968 |
+
residual = np.abs(normalized - np.rint(normalized))
|
| 969 |
+
alignment = np.exp(-((residual / 0.10) ** 2))
|
| 970 |
+
support = float(np.average(alignment, weights=weights))
|
| 971 |
+
occupied_steps = len(np.unique(np.rint(normalized)))
|
| 972 |
+
coverage = min(1.0, occupied_steps / max(3, len(offsets)))
|
| 973 |
+
best = max(best, support * (0.82 + 0.18 * coverage))
|
| 974 |
+
return best
|
| 975 |
+
|
| 976 |
+
|
| 977 |
+
def offset_span_reference(
|
| 978 |
+
offsets: np.ndarray,
|
| 979 |
+
image_shape: tuple[int, int] | None,
|
| 980 |
+
) -> float:
|
| 981 |
+
if image_shape is not None:
|
| 982 |
+
return float(min(image_shape)) * 0.20
|
| 983 |
+
return max(1e-6, float(offsets[-1] - offsets[0]))
|
| 984 |
|
| 985 |
|
| 986 |
def build_surface_uv_grids(
|
test_floor_geometry.py
CHANGED
|
@@ -86,7 +86,7 @@ class MetricFloorGeometryTests(unittest.TestCase):
|
|
| 86 |
self.assertLessEqual(float(decoded.max()), app.SHOWROOM_SHADE_MAP_MAX)
|
| 87 |
self.assertGreater(float(decoded[52, 64]), 0.90)
|
| 88 |
|
| 89 |
-
def
|
| 90 |
height, width = 128, 160
|
| 91 |
image = np.full((height, width, 3), 128, dtype=np.uint8)
|
| 92 |
image[42:86, 28:68, :] = 52
|
|
@@ -99,8 +99,46 @@ class MetricFloorGeometryTests(unittest.TestCase):
|
|
| 99 |
decoded = app.REFLECTION_MAP_MIN + (reflection.astype(np.float32) / 255.0) * (
|
| 100 |
app.REFLECTION_MAP_MAX - app.REFLECTION_MAP_MIN
|
| 101 |
)
|
| 102 |
-
self.
|
| 103 |
-
self.assertGreater(float(decoded[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
def test_surface_uv_quality_keeps_smooth_complete_uvs(self):
|
| 106 |
height, width = 60, 80
|
|
@@ -669,6 +707,102 @@ class MetricFloorGeometryTests(unittest.TestCase):
|
|
| 669 |
self.assertIsNotNone(render_rotation)
|
| 670 |
self.assertLess(floor_direction.angular_distance_degrees(render_rotation or 0.0, 23.0), 1.0)
|
| 671 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 672 |
def test_floor_direction_metadata_prefers_surface_uv_rotation(self):
|
| 673 |
height, width = 180, 240
|
| 674 |
image = np.full((height, width, 3), 225, dtype=np.uint8)
|
|
|
|
| 86 |
self.assertLessEqual(float(decoded.max()), app.SHOWROOM_SHADE_MAP_MAX)
|
| 87 |
self.assertGreater(float(decoded[52, 64]), 0.90)
|
| 88 |
|
| 89 |
+
def test_reflection_map_only_transfers_bright_highlights(self):
|
| 90 |
height, width = 128, 160
|
| 91 |
image = np.full((height, width, 3), 128, dtype=np.uint8)
|
| 92 |
image[42:86, 28:68, :] = 52
|
|
|
|
| 99 |
decoded = app.REFLECTION_MAP_MIN + (reflection.astype(np.float32) / 255.0) * (
|
| 100 |
app.REFLECTION_MAP_MAX - app.REFLECTION_MAP_MIN
|
| 101 |
)
|
| 102 |
+
self.assertLessEqual(float(decoded[64, 48]), 0.02)
|
| 103 |
+
self.assertGreater(float(decoded[42:86, 92:132].max()), 0.0)
|
| 104 |
+
|
| 105 |
+
def test_intrinsic_shading_inversion_is_corrected_against_room_luminance(self):
|
| 106 |
+
height, width = 160, 180
|
| 107 |
+
image = np.zeros((height, width, 3), dtype=np.uint8)
|
| 108 |
+
image[: height // 2, :, :] = 210
|
| 109 |
+
image[height // 2 :, :, :] = 80
|
| 110 |
+
mask = np.ones((height, width), dtype=np.uint8)
|
| 111 |
+
inverted_intrinsic = np.ones((height, width), dtype=np.float32)
|
| 112 |
+
inverted_intrinsic[: height // 2, :] = 0.55
|
| 113 |
+
inverted_intrinsic[height // 2 :, :] = 1.45
|
| 114 |
+
|
| 115 |
+
aligned, source = app.align_intrinsic_shading_to_luminance(
|
| 116 |
+
inverted_intrinsic,
|
| 117 |
+
image,
|
| 118 |
+
mask,
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
self.assertEqual(source, "intrinsic-inverted-corrected")
|
| 122 |
+
self.assertGreater(float(aligned[32, width // 2]), float(aligned[128, width // 2]))
|
| 123 |
+
|
| 124 |
+
def test_intrinsic_shading_keeps_matching_room_luminance(self):
|
| 125 |
+
height, width = 160, 180
|
| 126 |
+
image = np.zeros((height, width, 3), dtype=np.uint8)
|
| 127 |
+
image[: height // 2, :, :] = 210
|
| 128 |
+
image[height // 2 :, :, :] = 80
|
| 129 |
+
mask = np.ones((height, width), dtype=np.uint8)
|
| 130 |
+
matching_intrinsic = np.ones((height, width), dtype=np.float32)
|
| 131 |
+
matching_intrinsic[: height // 2, :] = 1.45
|
| 132 |
+
matching_intrinsic[height // 2 :, :] = 0.55
|
| 133 |
+
|
| 134 |
+
aligned, source = app.align_intrinsic_shading_to_luminance(
|
| 135 |
+
matching_intrinsic,
|
| 136 |
+
image,
|
| 137 |
+
mask,
|
| 138 |
+
)
|
| 139 |
+
|
| 140 |
+
self.assertEqual(source, "intrinsic-aligned")
|
| 141 |
+
self.assertGreater(float(aligned[32, width // 2]), float(aligned[128, width // 2]))
|
| 142 |
|
| 143 |
def test_surface_uv_quality_keeps_smooth_complete_uvs(self):
|
| 144 |
height, width = 60, 80
|
|
|
|
| 707 |
self.assertIsNotNone(render_rotation)
|
| 708 |
self.assertLess(floor_direction.angular_distance_degrees(render_rotation or 0.0, 23.0), 1.0)
|
| 709 |
|
| 710 |
+
def test_surface_uv_rotation_prefers_regular_grout_lattice_over_long_irregular_edges(self):
|
| 711 |
+
lines = [
|
| 712 |
+
floor_direction.DetectedLine(40, y, 280, y, 0.0, 240.0)
|
| 713 |
+
for y in (80, 125, 170, 215, 260)
|
| 714 |
+
]
|
| 715 |
+
for offset in (0, 17, 49, 91, 143, 208, 260, 313):
|
| 716 |
+
radians = np.deg2rad(45.0)
|
| 717 |
+
normal = np.array([-np.sin(radians), np.cos(radians)])
|
| 718 |
+
direction = np.array([np.cos(radians), np.sin(radians)])
|
| 719 |
+
center = np.array([500.0, 350.0]) + normal * offset
|
| 720 |
+
start, end = center - direction * 130.0, center + direction * 130.0
|
| 721 |
+
lines.append(
|
| 722 |
+
floor_direction.DetectedLine(
|
| 723 |
+
float(start[0]),
|
| 724 |
+
float(start[1]),
|
| 725 |
+
float(end[0]),
|
| 726 |
+
float(end[1]),
|
| 727 |
+
45.0,
|
| 728 |
+
260.0,
|
| 729 |
+
)
|
| 730 |
+
)
|
| 731 |
+
|
| 732 |
+
angle = floor_direction.estimate_repeated_grout_orientation(
|
| 733 |
+
lines,
|
| 734 |
+
image_shape=(700, 800),
|
| 735 |
+
)
|
| 736 |
+
|
| 737 |
+
self.assertIsNotNone(angle)
|
| 738 |
+
self.assertLess(floor_direction.angular_distance_degrees(angle or 0.0, 0.0), 1.0)
|
| 739 |
+
|
| 740 |
+
def test_surface_uv_rotation_rectifies_source_before_detecting_grout(self):
|
| 741 |
+
height, width = 180, 240
|
| 742 |
+
image = np.full((height, width, 3), 225, dtype=np.uint8)
|
| 743 |
+
for y in range(30, height - 20, 30):
|
| 744 |
+
cv2.line(image, (20, y), (width - 20, y), (55, 55, 55), 2, cv2.LINE_AA)
|
| 745 |
+
yy, xx = np.mgrid[0:height, 0:width]
|
| 746 |
+
surface_uv = np.column_stack((xx.ravel(), yy.ravel())).astype(np.float32)
|
| 747 |
+
surface_indices = np.arange(height * width, dtype=np.uint32)
|
| 748 |
+
source_lines = [
|
| 749 |
+
floor_direction.DetectedLine(20, 30, width - 20, 30, 0.0, width - 40.0),
|
| 750 |
+
floor_direction.DetectedLine(20, 60, width - 20, 60, 0.0, width - 40.0),
|
| 751 |
+
floor_direction.DetectedLine(20, 90, width - 20, 90, 0.0, width - 40.0),
|
| 752 |
+
floor_direction.DetectedLine(20, 120, width - 20, 120, 0.0, width - 40.0),
|
| 753 |
+
]
|
| 754 |
+
|
| 755 |
+
angle = floor_direction.estimate_surface_uv_grout_rotation(
|
| 756 |
+
source_lines,
|
| 757 |
+
image_bgr=image,
|
| 758 |
+
surface_uv=surface_uv,
|
| 759 |
+
surface_indices=surface_indices,
|
| 760 |
+
surface_plane_ids=np.zeros(height * width, dtype=np.uint8),
|
| 761 |
+
image_shape=(height, width),
|
| 762 |
+
)
|
| 763 |
+
|
| 764 |
+
self.assertIsNotNone(angle)
|
| 765 |
+
self.assertLess(floor_direction.angular_distance_degrees(angle or 0.0, 0.0), 2.0)
|
| 766 |
+
|
| 767 |
+
def test_surface_uv_rotation_handles_perspective_converging_grout(self):
|
| 768 |
+
floor_height, floor_width = 180, 240
|
| 769 |
+
source_height, source_width = 250, 320
|
| 770 |
+
floor = np.full((floor_height, floor_width, 3), 220, dtype=np.uint8)
|
| 771 |
+
for y in range(28, floor_height - 18, 30):
|
| 772 |
+
cv2.line(floor, (12, y), (floor_width - 12, y), (45, 45, 45), 2, cv2.LINE_AA)
|
| 773 |
+
|
| 774 |
+
source_corners = np.array([[62, 42], [278, 68], [248, 218], [24, 194]], dtype=np.float32)
|
| 775 |
+
floor_corners = np.array(
|
| 776 |
+
[[0, 0], [floor_width - 1, 0], [floor_width - 1, floor_height - 1], [0, floor_height - 1]],
|
| 777 |
+
dtype=np.float32,
|
| 778 |
+
)
|
| 779 |
+
uv_to_source = cv2.getPerspectiveTransform(floor_corners, source_corners)
|
| 780 |
+
source = cv2.warpPerspective(floor, uv_to_source, (source_width, source_height))
|
| 781 |
+
surface_mask = np.zeros((source_height, source_width), dtype=np.uint8)
|
| 782 |
+
cv2.fillConvexPoly(surface_mask, source_corners.astype(np.int32), 1)
|
| 783 |
+
yy, xx = np.mgrid[0:source_height, 0:source_width]
|
| 784 |
+
source_points = np.column_stack((xx.ravel(), yy.ravel())).astype(np.float32).reshape(1, -1, 2)
|
| 785 |
+
source_to_uv = np.linalg.inv(uv_to_source)
|
| 786 |
+
uv_all = cv2.perspectiveTransform(source_points, source_to_uv)[0]
|
| 787 |
+
surface_indices = np.flatnonzero(surface_mask.ravel()).astype(np.uint32)
|
| 788 |
+
surface_uv = uv_all[surface_indices]
|
| 789 |
+
distractor_lines = [
|
| 790 |
+
floor_direction.DetectedLine(30, y, 290, y + 45, 10.0, 264.0)
|
| 791 |
+
for y in (20, 54, 101, 158, 215)
|
| 792 |
+
]
|
| 793 |
+
|
| 794 |
+
angle = floor_direction.estimate_surface_uv_grout_rotation(
|
| 795 |
+
distractor_lines,
|
| 796 |
+
image_bgr=source,
|
| 797 |
+
surface_uv=surface_uv,
|
| 798 |
+
surface_indices=surface_indices,
|
| 799 |
+
surface_plane_ids=np.zeros(len(surface_indices), dtype=np.uint8),
|
| 800 |
+
image_shape=(source_height, source_width),
|
| 801 |
+
)
|
| 802 |
+
|
| 803 |
+
self.assertIsNotNone(angle)
|
| 804 |
+
self.assertLess(floor_direction.angular_distance_degrees(angle or 0.0, 0.0), 3.0)
|
| 805 |
+
|
| 806 |
def test_floor_direction_metadata_prefers_surface_uv_rotation(self):
|
| 807 |
height, width = 180, 240
|
| 808 |
image = np.full((height, width, 3), 225, dtype=np.uint8)
|