OpenShot Library | libopenshot 0.3.3
Loading...
Searching...
No Matches
Stabilizer.cpp
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#include <fstream>
15#include <iomanip>
16#include <iostream>
17
18#include "effects/Stabilizer.h"
19#include "Exceptions.h"
20#include "stabilizedata.pb.h"
21
22#include <google/protobuf/util/time_util.h>
23
24using namespace std;
25using namespace openshot;
26using google::protobuf::util::TimeUtil;
27
29Stabilizer::Stabilizer(std::string clipStabilizedDataPath):protobuf_data_path(clipStabilizedDataPath)
30{
31 // Init effect properties
32 init_effect_details();
33 // Tries to load the stabilization data from protobuf
34 LoadStabilizedData(clipStabilizedDataPath);
35}
36
37// Default constructor
39{
40 // Init effect properties
41 init_effect_details();
42}
43
44// Init effect settings
45void Stabilizer::init_effect_details()
46{
47
50
52 info.class_name = "Stabilizer";
53 info.name = "Stabilizer";
54 info.description = "Stabilize video clip to remove undesired shaking and jitter.";
55 info.has_audio = false;
56 info.has_video = true;
57 protobuf_data_path = "";
58 zoom = 1.0;
59}
60
61// This method is required for all derived classes of EffectBase, and returns a
62// modified openshot::Frame object
63std::shared_ptr<Frame> Stabilizer::GetFrame(std::shared_ptr<Frame> frame, int64_t frame_number)
64{
65
66 // Grab OpenCV Mat image
67 cv::Mat frame_image = frame->GetImageCV();
68
69 // If frame is NULL, return itself
70 if(!frame_image.empty()){
71
72 // Check if track data exists for the requested frame
73 if(transformationData.find(frame_number) != transformationData.end()){
74
75 float zoom_value = zoom.GetValue(frame_number);
76
77 // Create empty rotation matrix
78 cv::Mat T(2,3,CV_64F);
79
80 // Set rotation matrix values
81 T.at<double>(0,0) = cos(transformationData[frame_number].da);
82 T.at<double>(0,1) = -sin(transformationData[frame_number].da);
83 T.at<double>(1,0) = sin(transformationData[frame_number].da);
84 T.at<double>(1,1) = cos(transformationData[frame_number].da);
85
86 T.at<double>(0,2) = transformationData[frame_number].dx * frame_image.size().width;
87 T.at<double>(1,2) = transformationData[frame_number].dy * frame_image.size().height;
88
89 // Apply rotation matrix to image
90 cv::Mat frame_stabilized;
91 cv::warpAffine(frame_image, frame_stabilized, T, frame_image.size());
92
93 // Scale up the image to remove black borders
94 cv::Mat T_scale = cv::getRotationMatrix2D(cv::Point2f(frame_stabilized.cols/2, frame_stabilized.rows/2), 0, zoom_value);
95 cv::warpAffine(frame_stabilized, frame_stabilized, T_scale, frame_stabilized.size());
96 frame_image = frame_stabilized;
97 }
98 }
99 // Set stabilized image to frame
100 // If the input image is NULL or doesn't have tracking data, it's returned as it came
101 frame->SetImageCV(frame_image);
102 return frame;
103}
104
105// Load protobuf data file
106bool Stabilizer::LoadStabilizedData(std::string inputFilePath){
107 using std::ios;
108 // Create stabilization message
109 pb_stabilize::Stabilization stabilizationMessage;
110
111 // Read the existing tracker message.
112 std::fstream input(inputFilePath, ios::in | ios::binary);
113 if (!stabilizationMessage.ParseFromIstream(&input)) {
114 std::cerr << "Failed to parse protobuf message." << std::endl;
115 return false;
116 }
117
118 // Make sure the data maps are empty
119 transformationData.clear();
120 trajectoryData.clear();
121
122 // Iterate over all frames of the saved message and assign to the data maps
123 for (size_t i = 0; i < stabilizationMessage.frame_size(); i++) {
124
125 // Create stabilization message
126 const pb_stabilize::Frame& pbFrameData = stabilizationMessage.frame(i);
127
128 // Load frame number
129 size_t id = pbFrameData.id();
130
131 // Load camera trajectory data
132 float x = pbFrameData.x();
133 float y = pbFrameData.y();
134 float a = pbFrameData.a();
135
136 // Assign data to trajectory map
138
139 // Load transformation data
140 float dx = pbFrameData.dx();
141 float dy = pbFrameData.dy();
142 float da = pbFrameData.da();
143
144 // Assing data to transformation map
146 }
147
148 // Delete all global objects allocated by libprotobuf.
149 google::protobuf::ShutdownProtobufLibrary();
150
151 return true;
152}
153
154
155
156// Generate JSON string of this object
157std::string Stabilizer::Json() const {
158
159 // Return formatted string
160 return JsonValue().toStyledString();
161}
162
163// Generate Json::Value for this object
164Json::Value Stabilizer::JsonValue() const {
165
166 // Create root json object
167 Json::Value root = EffectBase::JsonValue(); // get parent properties
168 root["type"] = info.class_name;
169 root["protobuf_data_path"] = protobuf_data_path;
170 root["zoom"] = zoom.JsonValue();
171
172 // return JsonValue
173 return root;
174}
175
176// Load JSON string into this object
177void Stabilizer::SetJson(const std::string value) {
178 // Parse JSON string into JSON objects
179 try
180 {
181 const Json::Value root = openshot::stringToJson(value);
182 // Set all values that match
183
184 SetJsonValue(root);
185 }
186 catch (const std::exception& e)
187 {
188 // Error parsing JSON (or missing keys)
189 throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
190 }
191}
192
193// Load Json::Value into this object
194void Stabilizer::SetJsonValue(const Json::Value root) {
195
196 // Set parent data
198
199 // Set data from Json (if key is found)
200 if (!root["protobuf_data_path"].isNull()){
201 protobuf_data_path = (root["protobuf_data_path"].asString());
202
203 if(!LoadStabilizedData(protobuf_data_path)){
204 std::cout<<"Invalid protobuf data path";
205 protobuf_data_path = "";
206 }
207 }
208 if(!root["zoom"].isNull())
209 zoom.SetJsonValue(root["zoom"]);
210}
211
212// Get all properties for a specific frame
213std::string Stabilizer::PropertiesJSON(int64_t requested_frame) const {
214
215 // Generate JSON properties list
216 Json::Value root = BasePropertiesJSON(requested_frame);
217
218 root["zoom"] = add_property_json("Zoom", zoom.GetValue(requested_frame), "float", "", &zoom, 0.0, 2.0, false, requested_frame);
219
220 // Return formatted string
221 return root.toStyledString();
222}
Header file for all Exception classes.
Header file for Stabilizer effect class.
std::string id
ID Property for all derived Clip and Effect classes.
Definition ClipBase.h:35
Json::Value add_property_json(std::string name, float value, std::string type, std::string memo, const Keyframe *keyframe, float min_value, float max_value, bool readonly, int64_t requested_frame) const
Generate JSON for a property.
Definition ClipBase.cpp:96
virtual Json::Value JsonValue() const
Generate Json::Value for this object.
Json::Value BasePropertiesJSON(int64_t requested_frame) const
Generate JSON object of base properties (recommended to be used by all effects)
virtual void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
EffectInfoStruct info
Information about the current effect.
Definition EffectBase.h:69
Exception for invalid JSON.
Definition Exceptions.h:218
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition KeyFrame.cpp:372
double GetValue(int64_t index) const
Get the value at a specific index.
Definition KeyFrame.cpp:258
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition KeyFrame.cpp:339
Json::Value JsonValue() const override
Generate Json::Value for this object.
std::shared_ptr< Frame > GetFrame(std::shared_ptr< Frame > frame, int64_t frame_number) override
This method is required for all derived classes of EffectBase, and returns a modified openshot::Frame...
std::map< size_t, EffectTransformParam > transformationData
Definition Stabilizer.h:77
std::string PropertiesJSON(int64_t requested_frame) const override
void SetJson(const std::string value) override
Load JSON string into this object.
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
std::map< size_t, EffectCamTrajectory > trajectoryData
Definition Stabilizer.h:76
bool LoadStabilizedData(std::string inputFilePath)
Load protobuf data file.
std::string Json() const override
Generate JSON string of this object.
This namespace is the default namespace for all code in the openshot library.
Definition Compressor.h:29
const Json::Value stringToJson(const std::string value)
Definition Json.cpp:16
bool has_video
Determines if this effect manipulates the image of a frame.
Definition EffectBase.h:40
bool has_audio
Determines if this effect manipulates the audio of a frame.
Definition EffectBase.h:41
std::string class_name
The class name of the effect.
Definition EffectBase.h:36
std::string name
The name of the effect.
Definition EffectBase.h:37
std::string description
The description of this effect and what it does.
Definition EffectBase.h:38