2D gestalt-edge consistency filter: drop edges with no view support
Browse filesFor each predicted 3D edge, project both endpoints into every COLMAP view
and walk the 2D segment. Count how many sampled pixels fall on a gestalt
edge-class pixel (eave, ridge, rake, valley, hip, flashing, step_flashing)
after a 4px dilation of the mask. If at least 2 views show >=25% of the
projected segment overlapping the edge mask, keep the edge; otherwise drop
it as a hallucination with no 2D evidence.
Precision-only operation: can drop bad edges but cannot add new geometry,
so it can't repeat the plane_wireframe failure mode of polluting predictions.
Uses the same COLMAP cameras + gestalt masks that triangulation tracks
already rely on (validated machinery).
Conservative thresholds chosen for first-evaluation safety:
- min_views_support=2 (needs 2-view agreement to KEEP, not 1)
- min_pixel_frac=0.25 (a truly hallucinated edge projects to random pixels,
~5-10% mask overlap; a real edge after dilation should be >=80%, so 0.25
is a wide margin from both extremes)
- dilate_px=4 (9px-thick edge tolerance)
- sample_steps=20
Applied after hybrid_merge so both learned edges AND triangulation track
edges are filtered uniformly. Floor: never returns an empty graph.
DIAG output extended with `2dfilt=N->M` so we can see drop rates per
sample in the HF Space logs and tune thresholds based on real data.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Files changed (2)
- edge_2d_filter.pyAdded+164
- script.py+21-1
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164"""2D gestalt-edge consistency filter for predicted 3D wireframe edges.
For each predicted edge (v_a, v_b), project the endpoints into every COLMAP viewand walk the 2D segment between them. Count how many sampled pixels fall on agestalt "edge class" pixel (eave, ridge, rake, valley, hip, flashing,step_flashing). If at least ``min_views_support`` views show strong overlap(>= ``min_pixel_frac`` of samples), the edge is kept; otherwise it is droppedas a hallucination unsupported by 2D evidence.
All COLMAP cameras + masks are already validated machinery (triangulationtracks use them). The filter is precision-only — it can drop edges but cannotintroduce new geometry. Conservative defaults: an edge needs evidence in 2+views, with at least 25% of the projected segment lying on edge-class pixels(after dilation), to survive.
Falls back to the unfiltered (vertices, edges) on any failure so the pipelinecannot collapse if a sample's COLMAP / gestalt data is malformed."""
from __future__ import annotations
import numpy as npimport cv2
EDGE_CLASSES = ( "eave", "ridge", "rake", "valley", "hip", "flashing", "step_flashing",)
def _build_edge_masks(good, views, dilate_px: int): """Build per-view dilated binary masks of edge-class pixels.
Returns dict ``img_id -> (mask_bool, H, W)`` for every img_id that has both a registered COLMAP view and a gestalt image. """ from hoho2025.color_mappings import gestalt_color_mapping
out = {} for gest_pil, depth_pil, img_id in zip( good["gestalt"], good["depth"], good["image_ids"] ): if img_id not in views: continue depth_np = np.array(depth_pil) H, W = depth_np.shape[:2] gest_np = np.array(gest_pil.resize((W, H))).astype(np.uint8)
mask = np.zeros((H, W), dtype=np.uint8) for cls in EDGE_CLASSES: color = np.array(gestalt_color_mapping[cls]) m = cv2.inRange(gest_np, color - 0.5, color + 0.5) mask |= m
if dilate_px > 0: k = 2 * dilate_px + 1 mask = cv2.dilate(mask, np.ones((k, k), np.uint8), iterations=1)
out[img_id] = (mask > 0, H, W) return out
def filter_edges_by_2d_support( pv, pe, sample, min_views_support: int = 2, min_pixel_frac: float = 0.25, dilate_px: int = 4, sample_steps: int = 20,): """Drop edges whose 2D projection lacks gestalt-edge mask support.
Args: pv: (N, 3) vertices in world coordinates. pe: list of (u, v) edge indices. sample: raw dataset entry (used to access COLMAP + gestalt views). min_views_support: edge must be supported by this many views to be kept. min_pixel_frac: fraction of sampled pixels along the projected line that must lie on an edge-class pixel for the view to count as supporting. dilate_px: pixel-radius dilation of the edge mask (gives tolerance for slightly-off projections; 4 → 9px-thick edges). sample_steps: number of points sampled along each 2D projected line.
Returns: (pv_filtered, pe_filtered) with orphaned vertices removed. Falls back to the input on any error or if the filter would leave fewer than one edge or two vertices. """ try: from hoho2025.example_solutions import convert_entry_to_human_readable from mvs_utils import collect_views, project_world_to_image
pv_arr = np.asarray(pv, dtype=np.float64) if pv_arr.ndim != 2 or pv_arr.shape[0] < 2 or len(pe) < 1: return pv, pe
good = convert_entry_to_human_readable(sample) colmap_rec = good.get("colmap") or good.get("colmap_binary") if colmap_rec is None: return pv, pe
views = collect_views(colmap_rec, good["image_ids"]) if len(views) < min_views_support: return pv, pe
view_masks = _build_edge_masks(good, views, dilate_px=dilate_px) if not view_masks: return pv, pe
keep_edges = [] for u, v in pe: u, v = int(u), int(v) if u == v or u >= len(pv_arr) or v >= len(pv_arr): continue endpoints = np.stack([pv_arr[u], pv_arr[v]])
supporting = 0 for img_id, view in views.items(): if img_id not in view_masks: continue mask_bool, H, W = view_masks[img_id]
uv, z = project_world_to_image(view["P"], endpoints) # Require both endpoints in front of camera and inside the image. if z[0] <= 0 or z[1] <= 0: continue if not ( 0 <= uv[0, 0] < W and 0 <= uv[0, 1] < H and 0 <= uv[1, 0] < W and 0 <= uv[1, 1] < H ): continue
t = np.linspace(0.0, 1.0, sample_steps) xs = uv[0, 0] + t * (uv[1, 0] - uv[0, 0]) ys = uv[0, 1] + t * (uv[1, 1] - uv[0, 1]) xs_i = np.clip(xs.astype(np.int32), 0, W - 1) ys_i = np.clip(ys.astype(np.int32), 0, H - 1)
hits = int(mask_bool[ys_i, xs_i].sum()) if hits / float(sample_steps) >= min_pixel_frac: supporting += 1 if supporting >= min_views_support: break
if supporting >= min_views_support: keep_edges.append((u, v))
# Safety: never return an empty graph. if len(keep_edges) < 1: return pv, pe
used = sorted({a for e in keep_edges for a in e}) if len(used) < 2: return pv, pe old_to_new = {old: new for new, old in enumerate(used)} new_pv = np.asarray([pv_arr[old] for old in used]) new_pe = [(old_to_new[a], old_to_new[b]) for a, b in keep_edges] return new_pv, new_pe
except Exception: return pv, pe420 unmodified lines4214224234244254264 unmodified lines431432433434435436437438439440420 unmodified linesprint(f" Track ensemble failed for {order_id}: {track_e_err}")pred_status = "track_failed"except Exception as e:import tracebackprint(f" Predict failed for {order_id}:\n{traceback.format_exc()}")4 unmodified linesn_pred_v = len(pred_v) if hasattr(pred_v, '__len__') else 0n_pred_e = len(pred_e) if hasattr(pred_e, '__len__') else 0print(f"[DIAG] order_id={order_id} colmap={n_colmap_pts} fused={n_fused_pts} "f"track_v={track_v_count} track_e={track_e_count} "f"pred_v={n_pred_v} pred_e={n_pred_e} status={pred_status}")solution.append({420 unmodified lines4214224234244254264274284294304314324334344354364374384394404414424434 unmodified lines448449450451452453454455456457458459460420 unmodified linesprint(f" Track ensemble failed for {order_id}: {track_e_err}")pred_status = "track_failed"# 2D gestalt-edge consistency filter: drop predicted 3D edges# whose projection has no support in any view's edge-class mask.# Precision-only; can only drop edges, never add bad ones.edges_before_2d = len(pred_e) if hasattr(pred_e, '__len__') else 0try:from edge_2d_filter import filter_edges_by_2d_supportpred_v, pred_e = filter_edges_by_2d_support(pred_v, pred_e, sample,min_views_support=2,min_pixel_frac=0.25,dilate_px=4,sample_steps=20,)except Exception as filt_err:print(f" 2D edge filter failed for {order_id}: {filt_err}")edges_after_2d = len(pred_e) if hasattr(pred_e, '__len__') else 0except Exception as e:import tracebackprint(f" Predict failed for {order_id}:\n{traceback.format_exc()}")4 unmodified linesn_pred_v = len(pred_v) if hasattr(pred_v, '__len__') else 0n_pred_e = len(pred_e) if hasattr(pred_e, '__len__') else 0edges_before = locals().get('edges_before_2d', n_pred_e)edges_after = locals().get('edges_after_2d', n_pred_e)print(f"[DIAG] order_id={order_id} colmap={n_colmap_pts} fused={n_fused_pts} "f"track_v={track_v_count} track_e={track_e_count} "f"pred_v={n_pred_v} pred_e={n_pred_e} "f"2dfilt={edges_before}->{edges_after} status={pred_status}")solution.append({