• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #ifndef TENSORFLOW_EXAMPLES_ANDROID_JNI_OBJECT_TRACKING_OPTICAL_FLOW_H_
17 #define TENSORFLOW_EXAMPLES_ANDROID_JNI_OBJECT_TRACKING_OPTICAL_FLOW_H_
18 
19 #include "tensorflow/examples/android/jni/object_tracking/geom.h"
20 #include "tensorflow/examples/android/jni/object_tracking/image-inl.h"
21 #include "tensorflow/examples/android/jni/object_tracking/image.h"
22 #include "tensorflow/examples/android/jni/object_tracking/utils.h"
23 
24 #include "tensorflow/examples/android/jni/object_tracking/config.h"
25 #include "tensorflow/examples/android/jni/object_tracking/frame_pair.h"
26 #include "tensorflow/examples/android/jni/object_tracking/image_data.h"
27 #include "tensorflow/examples/android/jni/object_tracking/keypoint.h"
28 
29 namespace tf_tracking {
30 
31 class FlowCache;
32 
33 // Class encapsulating all the data and logic necessary for performing optical
34 // flow.
35 class OpticalFlow {
36  public:
37   explicit OpticalFlow(const OpticalFlowConfig* const config);
38 
39   // Add a new frame to the optical flow.  Will update all the non-keypoint
40   // related member variables.
41   //
42   // new_frame should be a buffer of grayscale values, one byte per pixel,
43   // at the original frame_width and frame_height used to initialize the
44   // OpticalFlow object.  Downsampling will be handled internally.
45   //
46   // time_stamp should be a time in milliseconds that later calls to this and
47   // other methods will be relative to.
48   void NextFrame(const ImageData* const image_data);
49 
50   // An implementation of the Lucas-Kanade Optical Flow algorithm.
51   static bool FindFlowAtPoint_LK(const Image<uint8_t>& img_I,
52                                  const Image<uint8_t>& img_J,
53                                  const Image<int32_t>& I_x,
54                                  const Image<int32_t>& I_y, const float p_x,
55                                  const float p_y, float* out_g_x,
56                                  float* out_g_y);
57 
58   // Pointwise flow using translational 2dof ESM.
59   static bool FindFlowAtPoint_ESM(
60       const Image<uint8_t>& img_I, const Image<uint8_t>& img_J,
61       const Image<int32_t>& I_x, const Image<int32_t>& I_y,
62       const Image<int32_t>& J_x, const Image<int32_t>& J_y, const float p_x,
63       const float p_y, float* out_g_x, float* out_g_y);
64 
65   // Finds the flow using a specific level, in either direction.
66   // If reversed, the coordinates are in the context of the latest
67   // frame, not the frame before it.
68   // All coordinates used in parameters are global, not scaled.
69   bool FindFlowAtPointReversible(
70       const int level, const float u_x, const float u_y,
71       const bool reverse_flow,
72       float* final_x, float* final_y) const;
73 
74   // Finds the flow using a specific level, filterable by forward-backward
75   // error. All coordinates used in parameters are global, not scaled.
76   bool FindFlowAtPointSingleLevel(const int level,
77                                   const float u_x, const float u_y,
78                                   const bool filter_by_fb_error,
79                                   float* flow_x, float* flow_y) const;
80 
81   // Pyramidal optical-flow using all levels.
82   bool FindFlowAtPointPyramidal(const float u_x, const float u_y,
83                                 const bool filter_by_fb_error,
84                                 float* flow_x, float* flow_y) const;
85 
86  private:
87   const OpticalFlowConfig* const config_;
88 
89   const ImageData* frame1_;
90   const ImageData* frame2_;
91 
92   // Size of the internally allocated images (after original is downsampled).
93   const Size working_size_;
94 
95   TF_DISALLOW_COPY_AND_ASSIGN(OpticalFlow);
96 };
97 
98 }  // namespace tf_tracking
99 
100 #endif  // TENSORFLOW_EXAMPLES_ANDROID_JNI_OBJECT_TRACKING_OPTICAL_FLOW_H_
101