Preserve offline geometry, marking, replay and evaluation pipelines with source manifests and synthetic tests. Research qualification does not activate these methods as production navigation.
72 lines
3.7 KiB
Python
72 lines
3.7 KiB
Python
"""Automatic mask-initialized OpenCV GrabCut for road material context.
|
|
|
|
Urban class scores provide seeds, never manually drawn foreground/background.
|
|
The output supplies two-sided context, not a physical curb observation.
|
|
All dimensions are metric and independent of scene identity.
|
|
"""
|
|
from dataclasses import dataclass, asdict
|
|
import cv2
|
|
import numpy as np
|
|
from scipy.ndimage import distance_transform_edt, gaussian_filter, label
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class MaterialPolicy:
|
|
cell_m: float = .20
|
|
core_distance_m: float = 1.2
|
|
support_margin_m: float = 4.0
|
|
appearance_smooth_m: float = .20
|
|
iterations: int = 1
|
|
|
|
|
|
def road_material(rgb, road, side, alpha, vehicle, cell_m,
|
|
policy=MaterialPolicy()):
|
|
shape=road.shape
|
|
scale=cell_m/policy.cell_m
|
|
size=(max(2,round(shape[1]*scale)),max(2,round(shape[0]*scale)))
|
|
small=lambda a:cv2.resize(np.asarray(a,dtype='float32'),size,interpolation=cv2.INTER_AREA)
|
|
rr,ss,aa,vv=map(small,[road,side,alpha,vehicle])
|
|
image=cv2.resize(rgb,size,interpolation=cv2.INTER_AREA)
|
|
image=cv2.GaussianBlur(image,(0,0),policy.appearance_smooth_m/policy.cell_m)
|
|
rc=(rr>.35)&(rr>=ss);sc=(ss>.4)&(ss>rr)
|
|
observed=(aa>.8)&(vv<.15)
|
|
# Distances ignore occluders; exclusion applies to seeds, not road extent.
|
|
fg=(distance_transform_edt(rc)*policy.cell_m>policy.core_distance_m)&observed&(rr>.6)
|
|
bg=(distance_transform_edt(sc)*policy.cell_m>policy.core_distance_m)&observed&(ss>.6)
|
|
active=distance_transform_edt(~(rc|sc))*policy.cell_m<=policy.support_margin_m
|
|
stats=dict(policy=asdict(policy),road_seeds=int(fg.sum()),side_seeds=int(bg.sum()),manual_seeds=False)
|
|
if fg.sum()<10 or bg.sum()<10:
|
|
stats['status']='insufficient-automatic-seeds'
|
|
return None,stats
|
|
mask=np.where(rc,cv2.GC_PR_FGD,cv2.GC_PR_BGD).astype('uint8')
|
|
mask[~active]=cv2.GC_BGD
|
|
mask[bg]=cv2.GC_BGD;mask[fg]=cv2.GC_FGD
|
|
cv2.setRNGSeed(0)
|
|
# Initialise OpenCV's colour mixtures from definite cores only. Otherwise
|
|
# a displaced probable mask trains both models on the same road colour and
|
|
# preserves its own error. The two-row seed image is used only for models;
|
|
# its labels are all definite and never supply spatial road geometry.
|
|
samples=[image[m] for m in [bg,fg]]
|
|
count=min(10000,min(map(len,samples)))
|
|
train=np.stack([a[np.linspace(0,len(a)-1,count).astype(int)] for a in samples])
|
|
train_mask=np.tile(np.array([cv2.GC_BGD,cv2.GC_FGD],np.uint8)[:,None],(1,count))
|
|
bg_model=np.zeros((1,65),np.float64);fg_model=bg_model.copy()
|
|
cv2.grabCut(train,train_mask,None,bg_model,fg_model,1,cv2.GC_INIT_WITH_MASK)
|
|
cv2.grabCut(image,mask,None,bg_model,fg_model,policy.iterations,cv2.GC_EVAL_FREEZE_MODEL)
|
|
material=(mask==cv2.GC_FGD)|(mask==cv2.GC_PR_FGD)
|
|
# Enclosed paint/car holes cannot author a new road boundary. Observed
|
|
# sidewalk seeds protect genuine islands, and exterior background stays open.
|
|
labs,n=label(~material)
|
|
protect=np.unique(np.r_[labs[0],labs[-1],labs[:,0],labs[:,-1],labs[bg|~active]])
|
|
counts=np.bincount(labs.ravel());road_sum=np.bincount(labs.ravel(),weights=rr.ravel())
|
|
fill=(road_sum/np.maximum(counts,1)>.35);fill[protect]=False;fill[0]=False
|
|
material|=fill[labs]
|
|
# Detached appearance regions need at least one automatic road seed.
|
|
components,_=label(material)
|
|
keep=np.unique(components[fg]);keep=keep[keep>0]
|
|
material=np.isin(components,keep)
|
|
result=cv2.resize(material.astype('float32'),(shape[1],shape[0]),interpolation=cv2.INTER_LINEAR)
|
|
result=gaussian_filter(result,.15/cell_m).astype('float32')
|
|
stats.update(status='grabcut',road_cells=int(material.sum()),changed_cells=int((material!=rc).sum()))
|
|
return result,stats
|