OpenShot Library | libopenshot  0.3.3
CVStabilization.h
Go to the documentation of this file.
1 
10 // Copyright (c) 2008-2019 OpenShot Studios, LLC
11 //
12 // SPDX-License-Identifier: LGPL-3.0-or-later
13 
14 #ifndef OPENSHOT_STABILIZATION_H
15 #define OPENSHOT_STABILIZATION_H
16 
17 #define int64 opencv_broken_int
18 #define uint64 opencv_broken_uint
19 #include <opencv2/opencv.hpp>
20 #include <opencv2/core.hpp>
21 #undef uint64
22 #undef int64
23 
24 #include <map>
25 #include <string>
26 #include <vector>
27 
28 #include "ProcessingController.h"
29 
30 #include "Clip.h"
31 #include "Json.h"
32 
33 // Forward decl
34 namespace pb_stabilize {
35  class Frame;
36 }
37 
38 // Store the relative transformation parameters between consecutive frames
40 {
42  TransformParam(double _dx, double _dy, double _da) {
43  dx = _dx;
44  dy = _dy;
45  da = _da;
46  }
47 
48  double dx;
49  double dy;
50  double da; // angle
51 };
52 
53 // Stores the global camera trajectory for one frame
55 {
57  CamTrajectory(double _x, double _y, double _a) {
58  x = _x;
59  y = _y;
60  a = _a;
61  }
62 
63  double x;
64  double y;
65  double a; // angle
66 };
67 
68 
76 
77  private:
78 
79  int smoothingWindow; // In frames. The larger the more stable the video, but less reactive to sudden panning
80 
81  size_t start;
82  size_t end;
83  double avr_dx, avr_dy, avr_da, max_dx, max_dy, max_da;
84 
85  cv::Mat last_T;
86  cv::Mat prev_grey;
87  std::vector <TransformParam> prev_to_cur_transform; // Previous to current
88  std::string protobuf_data_path;
89 
90  uint progress;
91  bool error = false;
92 
94  ProcessingController *processingController;
95 
97  bool TrackFrameFeatures(cv::Mat frame, size_t frameNum);
98 
99  std::vector<CamTrajectory> ComputeFramesTrajectory();
100  std::map<size_t,CamTrajectory> SmoothTrajectory(std::vector <CamTrajectory> &trajectory);
101 
103  std::map<size_t,TransformParam> GenNewCamPosition(std::map <size_t,CamTrajectory> &smoothed_trajectory);
104 
105  public:
106 
107  std::map <size_t,CamTrajectory> trajectoryData; // Save camera trajectory data
108  std::map <size_t,TransformParam> transformationData; // Save transormation data
109 
111  CVStabilization(std::string processInfoJson, ProcessingController &processingController);
112 
114  void stabilizeClip(openshot::Clip& video, size_t _start=0, size_t _end=0, bool process_interval=false);
115 
118  bool SaveStabilizedData();
120  void AddFrameDataToProto(pb_stabilize::Frame* pbFrameData, CamTrajectory& trajData, TransformParam& transData, size_t frame_number);
121 
122  // Return requested struct info for a given frame
123  TransformParam GetTransformParamData(size_t frameId);
125 
126  // Get and Set JSON methods
127  void SetJson(const std::string value);
128  void SetJsonValue(const Json::Value root);
129 
130  // Load protobuf data file (ONLY FOR MAKE TEST)
131  bool _LoadStabilizedData();
132 };
133 
134 #endif
TransformParam::da
double da
Definition: CVStabilization.h:50
CamTrajectory::x
double x
Definition: CVStabilization.h:63
CVStabilization::SetJson
void SetJson(const std::string value)
Load JSON string into this object.
Definition: CVStabilization.cpp:339
Clip.h
Header file for Clip class.
CVStabilization::GetCamTrajectoryTrackedData
CamTrajectory GetCamTrajectoryTrackedData(size_t frameId)
Definition: CVStabilization.cpp:326
openshot::Clip
This class represents a clip (used to arrange readers on the timeline)
Definition: Clip.h:89
CVStabilization::stabilizeClip
void stabilizeClip(openshot::Clip &video, size_t _start=0, size_t _end=0, bool process_interval=false)
Process clip and store necessary stabilization data.
Definition: CVStabilization.cpp:37
TransformParam::TransformParam
TransformParam(double _dx, double _dy, double _da)
Definition: CVStabilization.h:42
CamTrajectory::CamTrajectory
CamTrajectory()
Definition: CVStabilization.h:56
CVStabilization::AddFrameDataToProto
void AddFrameDataToProto(pb_stabilize::Frame *pbFrameData, CamTrajectory &trajData, TransformParam &transData, size_t frame_number)
Add frame stabilization data into protobuf message.
Definition: CVStabilization.cpp:298
CVStabilization::_LoadStabilizedData
bool _LoadStabilizedData()
Definition: CVStabilization.cpp:374
CVStabilization::SaveStabilizedData
bool SaveStabilizedData()
Definition: CVStabilization.cpp:268
CVStabilization::trajectoryData
std::map< size_t, CamTrajectory > trajectoryData
Definition: CVStabilization.h:107
TransformParam::TransformParam
TransformParam()
Definition: CVStabilization.h:41
CVStabilization::CVStabilization
CVStabilization(std::string processInfoJson, ProcessingController &processingController)
Set default smoothing window value to compute stabilization.
Definition: CVStabilization.cpp:29
CVStabilization::SetJsonValue
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: CVStabilization.cpp:356
ProcessingController.h
This is a message class for thread safe comunication between ClipProcessingJobs and OpenCV classes.
CVStabilization::GetTransformParamData
TransformParam GetTransformParamData(size_t frameId)
Definition: CVStabilization.cpp:314
CamTrajectory::CamTrajectory
CamTrajectory(double _x, double _y, double _a)
Definition: CVStabilization.h:57
TransformParam::dx
double dx
Definition: CVStabilization.h:48
CVStabilization::transformationData
std::map< size_t, TransformParam > transformationData
Definition: CVStabilization.h:108
pb_stabilize
Definition: CVStabilization.h:34
ProcessingController
Definition: ProcessingController.h:20
TransformParam
Definition: CVStabilization.h:39
TransformParam::dy
double dy
Definition: CVStabilization.h:49
Json.h
Header file for JSON class.
CamTrajectory::y
double y
Definition: CVStabilization.h:64
CVStabilization
This class stabilizes a video frame using optical flow.
Definition: CVStabilization.h:75
CamTrajectory
Definition: CVStabilization.h:54
CamTrajectory::a
double a
Definition: CVStabilization.h:65