def track(self, im0, im1, p0):
if p0 is None or not len(p0):
return np.array([])
fflow = cv2.calcOpticalFlowFarneback(im0, im1, **self.farneback_params_)
fflow = cv2.medianBlur(fflow, 5)
# Initialize forward flow and propagated points
p1 = np.ones(shape=p0.shape) * np.nan
flow_p0 = np.ones(shape=p0.shape) * np.nan
flow_good = np.ones(shape=p0.shape, dtype=bool)
# Check finite value for pts, and within image bounds
valid0 = finite_and_within_bounds(p0, im0.shape)
# Determine finite flow at points
xys0 = p0[valid0].astype(int)
flow_p0[valid0] = fflow[xys0[:,1], xys0[:,0]]
# Propagate
p1 = p0 + flow_p0
# FWD-BWD check
if self.fb_check_:
# Initialize reverse flow and propagated points
p0r = np.ones(shape=p0.shape) * np.nan
flow_p1 = np.ones(shape=p0.shape) * np.nan
rflow = cv2.calcOpticalFlowFarneback(im1, im0, **self.farneback_params_)
rflow = cv2.medianBlur(rflow, 5)
# Check finite value for pts, and within image bounds
valid1 = finite_and_within_bounds(p1, im0.shape)
# Determine finite flow at points
xys1 = p1[valid1].astype(int)
flow_p1[valid1] = rflow[xys1[:,1], xys1[:,0]]
# Check diff
p0r = p1 + flow_p1
fb_good = (np.fabs(p0r-p0) < 3).all(axis=1)
# Set only good flow
flow_p0[~fb_good] = np.nan
p1 = p0 + flow_p0
return p1
评论列表
文章目录