"""OpenCV raster / SciPy registration with global orientation initialization.""" import numpy as np, cv2, shapely from shapely.geometry import Polygon from scipy.optimize import minimize from catalog_templates import bicycle def fit_bicycle(p,all_hypotheses=False,proposal_only=False): if p.is_empty or p.area<.025:return None rect=cv2.minAreaRect(np.asarray(p.convex_hull.exterior.coords,'float32')[:,:2]);corners=cv2.boxPoints(rect);edges=np.roll(corners,-1,axis=0)-corners;k=np.argmax(np.linalg.norm(edges,axis=1));L=float(np.linalg.norm(edges[k]));W=float(min(rect[1]));u=edges[k]/L;ang=np.arctan2(u[1],u[0]);origin=np.asarray(rect[0]);fill=p.area/max(L*W,1e-6) if not(.35=quality(from_center(x))[2]else x);q=quality(z) clean=z.copy();clean[5]=0. hypothesis=dict(polygon=bicycle(clean),score=q[2],support=q[0],recall=q[1],parameters=clean.tolist(),raster_blur_m=float(z[5]));hypotheses.append(hypothesis) return sorted(hypotheses,key=lambda x:-x['score'])if all_hypotheses else max(hypotheses,key=lambda x:x['score'],default=None)