2017-01-26 5 views
0

imビデオでファーンバックの方法を使用していますが、最終結果はflowであるopticflowのパラメータです。マグニチュードとフロー。最後の2つのフレームだけのオリエンテーション。動画全体の結果を保存するにはどうすればよいですか?つまり、第1〜第2フレームの計算結果を保存したいということです。第2〜3、第3〜第4、第n-1 nビデオ全体にfarnebacksメソッドのフロー推定を保存するにはどうすればよいですか?

答えて

0

オプティカルフローは通常、前のフレームと比較して、 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 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() 
ある

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 

です

関連する問題