本文整理汇总了Python中cv2.OPTFLOW_FARNEBACK_GAUSSIAN属性的典型用法代码示例。如果您正苦于以下问题:Python cv2.OPTFLOW_FARNEBACK_GAUSSIAN属性的具体用法?Python cv2.OPTFLOW_FARNEBACK_GAUSSIAN怎么用?Python cv2.OPTFLOW_FARNEBACK_GAUSSIAN使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类cv2
的用法示例。
在下文中一共展示了cv2.OPTFLOW_FARNEBACK_GAUSSIAN属性的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run_farneback
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import OPTFLOW_FARNEBACK_GAUSSIAN [as 别名]
def run_farneback(frames):
try:
return cv2.calcOpticalFlowFarneback(
frames[0], frames[1],
# options, defaults
None, # output
0.5, # pyr_scale, 0.5
10, # levels, 3
min(frames[0].shape[:2]) // 5, # winsize, 15
10, # iterations, 3
7, # poly_n, 5
1.5, # poly_sigma, 1.2
cv2.OPTFLOW_FARNEBACK_GAUSSIAN, # flags, 0
)
except cv2.error:
return None
示例2: extract_optical_flow
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import OPTFLOW_FARNEBACK_GAUSSIAN [as 别名]
def extract_optical_flow(fn, times, frames=8, scale_factor=1.0):
cap = cv2.VideoCapture(fn)
if not cap.isOpened():
return []
n_frames = cap.get(cv2.CAP_PROP_FRAME_COUNT)
outputs = []
if n_frames < frames * 2:
return outputs
def resize(im):
if scale_factor != 1.0:
new_size = (int(im.shape[1] * scale_factor), int(im.shape[0] * scale_factor))
return cv2.resize(im, new_size, interpolation=cv2.INTER_LINEAR)
else:
return im
for t in times:
cap.set(cv2.CAP_PROP_POS_FRAMES, min(t * n_frames, n_frames - 1 - frames))
ret, frame0 = cap.read()
im0 = resize(cv2.cvtColor(frame0, cv2.COLOR_BGR2GRAY))
mags = []
middle_frame = frame0
flows = []
for f in range(frames - 1):
ret, frame1 = cap.read()
if f == frames // 2:
middle_frame = frame1
im1 = resize(cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY))
flow = cv2.calcOpticalFlowFarneback(im0, im1,
None,
0.5, # py_scale
8, # levels
int(40 * scale_factor), # winsize
10, # iterations
5, # poly_n
1.1, # poly_sigma
cv2.OPTFLOW_FARNEBACK_GAUSSIAN)
#mag, ang = cv2.cartToPolar(flow[...,0], flow[...,1])
#mags.append(mag)
flows.append(flow)
im0 = im1
flow = (np.mean(flows, 0) / 100).clip(-1, 1)
#flow = np.mean(flows, 0)
#flow /= (flow.mean() * 5 + 1e-5)
#flow = flow.clip(-1, 1)
#flows = flows / (np.mean(flows, 0, keepdims=True) + 1e-5)
x = middle_frame[..., ::-1].astype(np.float32) / 255
outputs.append((x, flow))
return outputs