gpt4 book ai didi

opencv - 如何在整个视频中保存Farnebacks方法的流量估计?

转载 作者:行者123 更新时间:2023-12-02 17:43:01 28 4
gpt4 key购买 nike

我在视频上使用了Farneback的方法,但最终结果是光流的参数为flow.Magnitude和flow.Orientation仅适用于最后两帧。如何保存整个视频的结果?我的意思是我想保存第一到第二帧的计算结果,然后第二到第三,第三到第四,...,n-1 n

最佳答案

与上一帧相比,通常在while循环中为每一帧计算光流。因此,如果您保存流,则每次在循环中使用单独的文件名保存流文件时,将保存整个视频。

#pseudo code
params= {}#fill in the params needed to calculate flow
prev_frame=get_frame()
i_frame=1
while(1)
cur_frame=get_frame()
flow=calculate_flow(prev_frame, cur_frame, **params)
save_path='frame_%d' % i_frame
np.save(save_path, flow)
i_frame += 1
prev_frame=cur_frame

这是一些工作代码
def draw_optical_flow(current_frame_rgb, flow, step=20, scale=1.0, thickness=4):
assert(len(flow.shape)==3)
assert(current_frame_rgb.shape[0:2] == flow.shape[0:2])
assert(flow.shape[2] == 2)
for r in range(0, current_frame_rgb.shape[0], step):
for c in range(0, current_frame_rgb.shape[1], step):
cv2.arrowedLine(current_frame_rgb, (c,r), (int(c+scale*flow[r,c,0]), int(r+scale*flow[r,c,1])), (0,0,255), thickness=thickness )
return current_frame_rgb

这是主要功能
def main():
def graceful_exit():
cv2.destroyAllWindows()
sys.exit(0)

cap = cv2.VideoCapture('data/sample.mov')
ret, prev_frame_rgb = cap.read()
flow=np.zeros((prev_frame_rgb.shape[0], prev_frame_rgb.shape[1], 2), dtype=np.float32)
prev_frame_gray = cv2.cvtColor(prev_frame_rgb, cv2.COLOR_BGR2GRAY)
farneback_params={
'pyr_scale':0.5,'levels':3,
'winsize':15,'iterations': 3,
'poly_n': 5,'poly_sigma':1.2,
'flags':cv2.OPTFLOW_USE_INITIAL_FLOW}
num_frames_to_consider=180
iFrame=1
while(iFrame < num_frames_to_consider):
ret, current_frame_rgb = cap.read()
if(not ret):
break
current_frame_gray = cv2.cvtColor(current_frame_rgb, cv2.COLOR_BGR2GRAY)
flow = cv2.calcOpticalFlowFarneback(prev_frame_gray, current_frame_gray, flow, **farneback_params)
save_path='data/farneback_output/out_frame_%d.npy' % iFrame
if not os.path.isdir(os.path.dirname(save_path)):
os.makedirs(os.path.dirname(save_path))
#save flow, and load it back
np.save(save_path, flow)
flow=np.load(save_path)
prev_frame_gray = current_frame_gray
#show
current_frame_rgb=draw_optical_flow(current_frame_rgb, flow)
cv2.imshow('flow', current_frame_rgb)
k=cv2.waitKey(0)
if k == 27:
graceful_exit()
iFrame += 1

graceful_exit()

关于opencv - 如何在整个视频中保存Farnebacks方法的流量估计?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41881006/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com