Dewobble
Video motion stabilization with awareness of lens projection
rotation_detector.hpp
1 #ifndef DEWOBBLE_ROTATION_DETECTOR_HPP
2 #define DEWOBBLE_ROTATION_DETECTOR_HPP
3 
4 #include <list>
5 #include <opencv2/core.hpp>
6 #include <set>
7 
8 #include "camera.hpp"
9 #include "frame.hpp"
10 
11 namespace dewobble
12 {
13 class FrameDebugInfo
14 {
15  public:
16  std::vector<cv::Point2f> points_prev;
17  std::vector<cv::Point2f> points;
18  std::vector<bool> inliers;
19  int num_inliers = 0;
20  bool rotation_found = false;
21  int attempts = 0;
22  int frames_since_keyframe = 0;
23 };
24 
25 class FrameWithOrientation : public Frame
26 {
27  public:
28  FrameWithOrientation(
29  Frame frame,
30  cv::Mat orientation,
31  FrameDebugInfo debug_info);
32  cv::Mat orientation;
33  FrameDebugInfo debug_info;
34 };
35 
36 class RotationDetector
37 {
38  // Camera
39  const Camera m_camera;
40 
41  // Buffered output frames (with orientation)
42  std::list<FrameWithOrientation> m_buffered_frames;
43 
44  // Current frame index
45  long m_frame_index = 0;
46 
47  // The last input frame in grayscale
48  cv::UMat m_last_frame_gray;
49 
50  std::vector<cv::Mat> m_last_frame_pyramid;
51 
52  // The rotation that occurred in the last input frame
53  cv::Mat m_last_frame_rotation = cv::Mat::eye(3, 3, CV_64F);
54 
55  // The last input frame for which corners were detected from scratch
56  long m_last_key_frame_index = 0;
57 
58  // Good corners to track from the last key frame
59  std::vector<cv::Point2f> m_last_frame_corners;
60 
61  // The maximum number of frames to look ahead to interpolate the rotation
62  // of the current frame
63  unsigned int m_interpolation_horizon;
64 
65  // The calculated rotation of the last output frame,
66  // relative to the previous output frame
67  cv::Mat m_last_output_frame_rotation = cv::Mat::eye(3, 3, CV_64F);
68 
69  // The orientation of the last frame, relative to the first frame
70  cv::Mat m_last_output_frame_orientation = cv::Mat::eye(3, 3, CV_64F);
71 
72  // Whether the input has ended
73  bool m_input_ended = false;
74 
75  std::set<int> guess_camera_rotation(
76  std::vector<cv::Point2f> points_prev,
77  std::vector<cv::Point2f> points_current,
78  cv::OutputArray &rotation);
79 
80  public:
81  RotationDetector(Camera camera, unsigned int interpolation_horizon);
82  void push_frame(Frame frame);
83  void end_input();
84  FrameWithOrientation pull_frame();
85  bool frame_ready();
86 };
87 } // namespace dewobble
88 
89 #endif // DEWOBBLE_ROTATION_DETECTOR_HPP