Caffe2 - C++ API
A deep learning, cross platform ML framework
optical_flow.cc
1 
17 #include <caffe2/video/optical_flow.h>
18 
19 namespace caffe2 {
20 
21 void OpticalFlowExtractor(
22  const cv::Mat& prev_gray,
23  const cv::Mat& curr_gray,
24  const int flow_alg_type,
25  cv::Mat& flow) {
26  cv::Ptr<cv::DualTVL1OpticalFlow> tvl1 = cv::DualTVL1OpticalFlow::create();
27  switch (flow_alg_type) {
28  case FLowAlgType::FarnebackOpticalFlow:
29  cv::calcOpticalFlowFarneback(
30  prev_gray,
31  curr_gray,
32  flow,
33  std::sqrt(2) / 2.0,
34  5,
35  10,
36  2,
37  7,
38  1.5,
39  cv::OPTFLOW_FARNEBACK_GAUSSIAN);
40  break;
41  case FLowAlgType::DensePyrLKOpticalFlow:
42  LOG(ERROR) << "DensePyrLKOpticalFlow only has sparse version on CPU";
43  break;
44  case FLowAlgType::BroxOpticalFlow:
45  LOG(ERROR) << "BroxOpticalFlow on CPU is not available";
46  break;
47  case FLowAlgType::OpticalFlowDual_TVL1:
48  tvl1->calc(prev_gray, curr_gray, flow);
49  break;
50  default:
51  LOG(ERROR) << "Unsupported optical flow type " << flow_alg_type;
52  break;
53  }
54 }
55 
56 void MergeOpticalFlow(cv::Mat& prev_flow, const cv::Mat& curr_flow) {
57  const int rows = prev_flow.rows;
58  const int cols = prev_flow.cols;
59 
60  // merge two optical flows into one
61  for (int y = 0; y < rows; y++) {
62  for (int x = 0; x < cols; x++) {
63  cv::Point2f u = prev_flow.at<cv::Point2f>(y, x);
64  // get the new location
65  int x_new = std::min(cols - 1, std::max(0, cvRound(u.x + x)));
66  int y_new = std::min(rows - 1, std::max(0, cvRound(u.y + y)));
67  cv::Point2f u_new = curr_flow.at<cv::Point2f>(y_new, x_new);
68 
69  // update the flow
70  prev_flow.at<cv::Point2f>(y, x) += u_new;
71  }
72  }
73 }
74 
75 void MultiFrameOpticalFlowExtractor(
76  const std::vector<cv::Mat>& grays,
77  const int optical_flow_alg_type,
78  cv::Mat& flow) {
79  int num_frames = grays.size();
80  CAFFE_ENFORCE_GE(num_frames, 2, "need at least 2 frames!");
81 
82  // compute optical flow for every two frames
83  std::vector<cv::Mat> flows;
84  for (int i = 0; i < num_frames - 1; i++) {
85  cv::Mat tmp;
86  OpticalFlowExtractor(grays[i], grays[i + 1], optical_flow_alg_type, tmp);
87  flows.push_back(tmp);
88  }
89 
90  flows[0].copyTo(flow);
91  // aggregate optical flow across multiple frame
92  for (int i = 1; i < num_frames - 1; i++) {
93  MergeOpticalFlow(flow, flows[i]);
94  }
95 }
96 
97 } // namespace caffe2
Copyright (c) 2016-present, Facebook, Inc.