Files
DCCONSTRUCTIONS 69dc566665 feat(research): preserve urban geometry pipelines and qualification suites
Preserve offline geometry, marking, replay and evaluation pipelines with source manifests and synthetic tests. Research qualification does not activate these methods as production navigation.
2026-10-29 09:10:00 +03:00

62 lines
5.2 KiB
Python

from paint_channels import paint_mask
import numpy as np,cv2,shapely
from discovery_rules import POLICY
def glyph_score(rgb,allowed,points,polygon,cell=.04,kind=None,parameters=None,observed=None,paint_override=None,connectivity_open_m=0.,tolerance_m=.03):
"""Check positive paint, unpainted negative space, and unexplained pixels."""
h,w=allowed.shape;observed=np.ones((h,w),bool)if observed is None else observed;gray=cv2.cvtColor(rgb,cv2.COLOR_RGB2GRAY)
bg=cv2.morphologyEx(gray,cv2.MORPH_OPEN,cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(19,19)))
hsv=cv2.cvtColor(rgb,cv2.COLOR_RGB2HSV)
paint=paint_mask(rgb,channel='white',observed=observed)if paint_override is None else paint_override
target=shapely.contains_xy(polygon,points[:,0],points[:,1]).reshape(h,w)
near=cv2.dilate(paint.astype('uint8'),cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(2*int(np.ceil(tolerance_m/cell))+1,)*2))>0
wide=cv2.dilate(target.astype('uint8'),cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(2*int(np.ceil(tolerance_m/cell))+1,)*2))>0
region=shapely.contains_xy(polygon.convex_hull.buffer(.15),points[:,0],points[:,1]).reshape(h,w)
negative=region&~wide
result=dict(valid=float(allowed[region&observed].mean())if (region&observed).any()else 0.,
support=float(near[target&observed].mean())if (target&observed).any()else 0.,
recall=float(wide[paint&region&allowed].mean())if (paint&region&allowed).any()else 0.,
negative_paint=float(paint[negative&allowed].mean())if (negative&allowed).any()else 1.)
# A complete template must explain the connected paint object, not just
# one head cut out of a longer curved arrow. Unfinished image edges veto it.
connectivity=paint.astype('uint8')
if connectivity_open_m>0:
k=max(3,int(round(connectivity_open_m/cell))//2*2+1)
connectivity=cv2.morphologyEx(connectivity,cv2.MORPH_OPEN,cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(k,k)))
count,components=cv2.connectedComponents(connectivity,8)
touching=set(np.unique(components[target&near]))-{0}
connected=np.isin(components,list(touching))&observed
result['connected_recall']=float(wide[connected].mean())if connected.any()else 0.
result['connected_at_window_edge']=bool(connected[0].any()or connected[-1].any()or connected[:,0].any()or connected[:,-1].any())
if kind is not None and parameters is not None:
px,py,a,L,W,stroke=parameters;u=np.array([np.cos(a),np.sin(a)]);n=np.array([-u[1],u[0]]);local=points-[px,py];t=(local@u).reshape(h,w);side=(local@n).reshape(h,w)
regions=[('head',t>L*.80),('tail',t<L*.25)]
if kind.startswith('straight_')and kind!='straight_arrow':regions.append(('branch',side*(1 if 'left'in kind else -1)>W*.24))
result['parts']=[]
for name,where in regions:
select=target&where;coverage=float(observed[select].mean())if select.any()else 0.;select=select&observed
result['parts'].append(dict(part=name,coverage=coverage,valid=float(allowed[select].mean())if select.any()else 0.,support=float(near[select].mean())if select.any()else 0.))
result['ends']=[]
for name,interior,exterior in [('tail',t<L*.20,(t<-POLICY['glyph_end_probe_start_m'])&(t>-POLICY['glyph_end_probe_stop_m'])),('head',t>L*.90,(t>L+POLICY['glyph_end_probe_start_m'])&(t<L+POLICY['glyph_end_probe_stop_m']))]:
observed_side=side[target&interior]
offset=float(np.median(observed_side))if len(observed_side)else 0.
select=exterior&(abs(side-offset)<max(.12,stroke*.6));coverage=float(observed[select].mean())if select.any()else 0.;select=select&observed
result['ends'].append(dict(end=name,coverage=coverage,valid=float(allowed[select].mean())if select.any()else 0.,paint_fraction=float(paint[select].mean())if select.any()else 1.))
return result
def directional_score(rgb,allowed,points,polygon,cell=.04,kind=None,parameters=None,observed=None,paint_override=None,connectivity_open_m=0.,tolerance_m=.03):
result=glyph_score(rgb,allowed,points,polygon,cell,kind,parameters,observed,paint_override,connectivity_open_m,tolerance_m)
if kind!='straight_arrow' or parameters is None:return result
# A reversed template on a detached triangular head can have good positive
# overlap and clear axial end probes. Its tip flanks still contain broad paint.
h,w=allowed.shape;observed=np.ones((h,w),bool)if observed is None else observed;gray=cv2.cvtColor(rgb,cv2.COLOR_RGB2GRAY);hsv=cv2.cvtColor(rgb,cv2.COLOR_RGB2HSV)
bg=cv2.morphologyEx(gray,cv2.MORPH_OPEN,cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(19,19)))
paint=paint_mask(rgb,channel='white',observed=observed)if paint_override is None else paint_override
target=shapely.contains_xy(polygon,points[:,0],points[:,1]).reshape(h,w)
wide=cv2.dilate(target.astype('uint8'),cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(2*int(np.ceil(tolerance_m/cell))+1,)*2))>0
px,py,a,L,W,stroke=parameters;u=np.array([np.cos(a),np.sin(a)]);n=np.array([-u[1],u[0]]);local=points-[px,py];t=(local@u).reshape(h,w);side=(local@n).reshape(h,w)
flank=(t>L*.80)&(t<L*1.05)&(abs(side)<W*.55)&~wide&allowed&observed
result['head_flank_paint']=float(paint[flank].mean())if flank.any()else 1.
return result
base_score=glyph_score