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