Preserve offline geometry, marking, replay and evaluation pipelines with source manifests and synthetic tests. Research qualification does not activate these methods as production navigation.
38 lines
2.1 KiB
Python
38 lines
2.1 KiB
Python
"""A raw connected outline defines its axis by paint area, not extreme pixels."""
|
|
import numpy as np,cv2
|
|
from beta_profiles import POLICY
|
|
from stroke_seeds import from_component as rectangle_seed
|
|
|
|
|
|
def from_component(polygon):
|
|
if polygon.geom_type!='Polygon' or polygon.is_empty:return None
|
|
# Already measured rectangles have no pixel fringe to reinterpret. Keeping
|
|
# them exact also preserves the independent long-line detector's result.
|
|
if polygon.symmetric_difference(polygon.minimum_rotated_rectangle).area<1e-9:return rectangle_seed(polygon)
|
|
origin=np.array(polygon.centroid.coords[0])[:2]
|
|
names=['m00','m10','m01','m20','m11','m02'];moments={k:0. for k in names}
|
|
for ring,sign in [(polygon.exterior,1)]+[(r,-1)for r in polygon.interiors]:
|
|
measured=cv2.moments((np.asarray(ring.coords)[:,:2]-origin).astype('float32'))
|
|
for k in names:moments[k]+=sign*measured[k]
|
|
if moments['m00']<=0:return None
|
|
center=np.array([moments['m10'],moments['m01']])/moments['m00']
|
|
covariance=np.array([[moments['m20'],moments['m11']],[moments['m11'],moments['m02']]])/moments['m00']-np.outer(center,center)
|
|
values,vectors=np.linalg.eigh(covariance)
|
|
if values[0]<=0 or values[1]<4*values[0]:return None
|
|
axis=vectors[:,1];width=float(np.sqrt(12*values[0]));center=origin+center
|
|
t=(np.asarray(polygon.exterior.coords)[:,:2]-center)@axis;length=float(np.ptp(t))
|
|
if not POLICY['min_seed_length_m']<=length<=POLICY['max_seed_length_m']:return None
|
|
if width>2*POLICY['normal_half_width_m']or length<2*width:return None
|
|
return dict(origin=center+axis*(t.min()+t.max())/2,axis=axis,length=length,width=width,proposal_contract='connected-paint-area-axis; final-width-unmeasured')
|
|
|
|
|
|
def preferred(polygon):
|
|
"""One initial axis per component; choose from shape, never failed search.
|
|
|
|
Area moments resist extreme fringe pixels. Where that estimator cannot
|
|
establish an elongated component, retain the original rectangle estimator.
|
|
Both estimators only propose; all pixel/width/endpoint gates stay downstream.
|
|
"""
|
|
measured=from_component(polygon)
|
|
return measured if measured is not None else rectangle_seed(polygon)
|