OpenShot Library | libopenshot 0.2.7
CVStabilization.h
Go to the documentation of this file.
1/**
2 * @file
3 * @brief Header file for CVStabilization class
4 * @author Jonathan Thomas <jonathan@openshot.org>
5 * @author Brenno Caldato <brenno.caldato@outlook.com>
6 *
7 * @ref License
8 */
9
10/* LICENSE
11 *
12 * Copyright (c) 2008-2019 OpenShot Studios, LLC
13 * <http://www.openshotstudios.com/>. This file is part of
14 * OpenShot Library (libopenshot), an open-source project dedicated to
15 * delivering high quality video editing and animation solutions to the
16 * world. For more information visit <http://www.openshot.org/>.
17 *
18 * OpenShot Library (libopenshot) is free software: you can redistribute it
19 * and/or modify it under the terms of the GNU Lesser General Public License
20 * as published by the Free Software Foundation, either version 3 of the
21 * License, or (at your option) any later version.
22 *
23 * OpenShot Library (libopenshot) is distributed in the hope that it will be
24 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU Lesser General Public License for more details.
27 *
28 * You should have received a copy of the GNU Lesser General Public License
29 * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
30 */
31
32#ifndef OPENSHOT_STABILIZATION_H
33#define OPENSHOT_STABILIZATION_H
34
35#define int64 opencv_broken_int
36#define uint64 opencv_broken_uint
37#include <opencv2/opencv.hpp>
38#include <opencv2/core.hpp>
39#undef uint64
40#undef int64
41#include <cmath>
42#include "protobuf_messages/stabilizedata.pb.h"
44#include "Clip.h"
45#include "Json.h"
46
47// Store the relative transformation parameters between consecutive frames
49{
51 TransformParam(double _dx, double _dy, double _da) {
52 dx = _dx;
53 dy = _dy;
54 da = _da;
55 }
56
57 double dx;
58 double dy;
59 double da; // angle
60};
61
62// Stores the global camera trajectory for one frame
64{
66 CamTrajectory(double _x, double _y, double _a) {
67 x = _x;
68 y = _y;
69 a = _a;
70 }
71
72 double x;
73 double y;
74 double a; // angle
75};
76
77
78/**
79 * @brief This class stabilizes a video frame using optical flow
80 *
81 * The relative motion between two consecutive frames is computed to obtain the global camera trajectory.
82 * The camera trajectory is then smoothed to reduce jittering.
83 */
85
86 private:
87
88 int smoothingWindow; // In frames. The larger the more stable the video, but less reactive to sudden panning
89
90 size_t start;
91 size_t end;
92 double avr_dx, avr_dy, avr_da, max_dx, max_dy, max_da;
93
94 cv::Mat last_T;
95 cv::Mat prev_grey;
96 std::vector <TransformParam> prev_to_cur_transform; // Previous to current
97 std::string protobuf_data_path;
98
99 uint progress;
100 bool error = false;
101
102 /// Will handle a Thread safely comutication between ClipProcessingJobs and the processing effect classes
103 ProcessingController *processingController;
104
105 /// Track current frame features and find the relative transformation
106 bool TrackFrameFeatures(cv::Mat frame, size_t frameNum);
107
108 std::vector<CamTrajectory> ComputeFramesTrajectory();
109 std::map<size_t,CamTrajectory> SmoothTrajectory(std::vector <CamTrajectory> &trajectory);
110
111 /// Generate new transformations parameters for each frame to follow the smoothed trajectory
112 std::map<size_t,TransformParam> GenNewCamPosition(std::map <size_t,CamTrajectory> &smoothed_trajectory);
113
114 public:
115
116 std::map <size_t,CamTrajectory> trajectoryData; // Save camera trajectory data
117 std::map <size_t,TransformParam> transformationData; // Save transormation data
118
119 /// Set default smoothing window value to compute stabilization
120 CVStabilization(std::string processInfoJson, ProcessingController &processingController);
121
122 /// Process clip and store necessary stabilization data
123 void stabilizeClip(openshot::Clip& video, size_t _start=0, size_t _end=0, bool process_interval=false);
124
125 /// Protobuf Save and Load methods
126 /// Save stabilization data to protobuf file
127 bool SaveStabilizedData();
128 /// Add frame stabilization data into protobuf message
129 void AddFrameDataToProto(pb_stabilize::Frame* pbFrameData, CamTrajectory& trajData, TransformParam& transData, size_t frame_number);
130
131 // Return requested struct info for a given frame
134
135 // Get and Set JSON methods
136 void SetJson(const std::string value); ///< Load JSON string into this object
137 void SetJsonValue(const Json::Value root); ///< Load Json::Value into this object
138
139 // Load protobuf data file (ONLY FOR MAKE TEST)
140 bool _LoadStabilizedData();
141};
142
143#endif
Header file for Clip class.
Header file for JSON class.
This is a message class for thread safe comunication between ClipProcessingJobs and OpenCV classes.
This class stabilizes a video frame using optical flow.
TransformParam GetTransformParamData(size_t frameId)
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
CVStabilization(std::string processInfoJson, ProcessingController &processingController)
Set default smoothing window value to compute stabilization.
void AddFrameDataToProto(pb_stabilize::Frame *pbFrameData, CamTrajectory &trajData, TransformParam &transData, size_t frame_number)
Add frame stabilization data into protobuf message.
std::map< size_t, TransformParam > transformationData
void stabilizeClip(openshot::Clip &video, size_t _start=0, size_t _end=0, bool process_interval=false)
Process clip and store necessary stabilization data.
std::map< size_t, CamTrajectory > trajectoryData
CamTrajectory GetCamTrajectoryTrackedData(size_t frameId)
void SetJson(const std::string value)
Load JSON string into this object.
This class represents a clip (used to arrange readers on the timeline)
Definition: Clip.h:109
CamTrajectory(double _x, double _y, double _a)
TransformParam(double _dx, double _dy, double _da)