from pathlib import Path import cv2, json,numpy as np root=Path(__file__).resolve().parent d={} for k in ['08-bicycle','09-moto']: frames=[] for f in sorted((root.parent/'IP资料包/透明动作序列'/k).glob('*.png')): im=cv2.imread(str(f),cv2.IMREAD_UNCHANGED) a=im[:,:,3,None]/255 gray=cv2.cvtColor((im[:,:,:3]*a+210*(1-a)).astype('uint8'),cv2.COLOR_BGR2GRAY) circ=cv2.HoughCircles(cv2.GaussianBlur(gray,(5,5),1.1),cv2.HOUGH_GRADIENT,1.3,88,param1=70,param2=30,minRadius=37,maxRadius=110) hits=[] if circ is None else [list(map(float,c)) for c in circ[0] if c[1]>333 and c[1]<470] sel=[] for side in [0,1]: cand=[c for c in hits if (c[0]>360)==bool(side)] cand.sort(key=lambda v:abs((v[1]+v[2])-492)+.6*abs(v[2]-(83 if k.startswith('08') else 65))) sel.append([round(v,2) for v in cand[0]] if cand else None) frames.append(sel) for j,pair in enumerate(frames): for side,c in enumerate(pair): if not c: available=[(abs(i-j),a[side]) for i,a in enumerate(frames) if a[side]] pair[side]=min(available)[1] d[k]=frames;print(k,frames[0],frames[-1]) (root/'轮心.json').write_text(json.dumps(d))