Loading...
Searching...
No Matches
Stabilizer.cpp
Go to the documentation of this file.
// Copyright (c) 2008-2019 OpenShot Studios, LLC
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#include <fstream>
#include <iomanip>
#include <iostream>
#include "effects/Stabilizer.h"
#include "Exceptions.h"
#include "stabilizedata.pb.h"
#include <google/protobuf/util/time_util.h>
using namespace std;
using namespace openshot;
using google::protobuf::util::TimeUtil;
Stabilizer::Stabilizer(std::string clipStabilizedDataPath):protobuf_data_path(clipStabilizedDataPath)
{
// Init effect properties
init_effect_details();
// Tries to load the stabilization data from protobuf
LoadStabilizedData(clipStabilizedDataPath);
}
// Default constructor
Stabilizer::Stabilizer()
{
// Init effect properties
init_effect_details();
}
// Init effect settings
void Stabilizer::init_effect_details()
{
InitEffectInfo();
info.class_name = "Stabilizer";
info.name = "Stabilizer";
info.description = "Stabilize video clip to remove undesired shaking and jitter.";
info.has_audio = false;
info.has_video = true;
protobuf_data_path = "";
zoom = 1.0;
}
// This method is required for all derived classes of EffectBase, and returns a
// modified openshot::Frame object
std::shared_ptr<Frame> Stabilizer::GetFrame(std::shared_ptr<Frame> frame, int64_t frame_number)
{
// Grab OpenCV Mat image
cv::Mat frame_image = frame->GetImageCV();
// If frame is NULL, return itself
if(!frame_image.empty()){
// Check if track data exists for the requested frame
if(transformationData.find(frame_number) != transformationData.end()){
float zoom_value = zoom.GetValue(frame_number);
// Create empty rotation matrix
cv::Mat T(2,3,CV_64F);
// Set rotation matrix values
T.at<double>(0,0) = cos(transformationData[frame_number].da);
T.at<double>(0,1) = -sin(transformationData[frame_number].da);
T.at<double>(1,0) = sin(transformationData[frame_number].da);
T.at<double>(1,1) = cos(transformationData[frame_number].da);
T.at<double>(0,2) = transformationData[frame_number].dx * frame_image.size().width;
T.at<double>(1,2) = transformationData[frame_number].dy * frame_image.size().height;
// Apply rotation matrix to image
cv::Mat frame_stabilized;
cv::warpAffine(frame_image, frame_stabilized, T, frame_image.size());
// Scale up the image to remove black borders
cv::Mat T_scale = cv::getRotationMatrix2D(cv::Point2f(frame_stabilized.cols/2, frame_stabilized.rows/2), 0, zoom_value);
cv::warpAffine(frame_stabilized, frame_stabilized, T_scale, frame_stabilized.size());
frame_image = frame_stabilized;
}
}
// Set stabilized image to frame
// If the input image is NULL or doesn't have tracking data, it's returned as it came
frame->SetImageCV(frame_image);
return frame;
}
// Load protobuf data file
bool Stabilizer::LoadStabilizedData(std::string inputFilePath){
using std::ios;
// Create stabilization message
pb_stabilize::Stabilization stabilizationMessage;
// Read the existing tracker message.
std::fstream input(inputFilePath, ios::in | ios::binary);
if (!stabilizationMessage.ParseFromIstream(&input)) {
std::cerr << "Failed to parse protobuf message." << std::endl;
return false;
}
// Make sure the data maps are empty
transformationData.clear();
trajectoryData.clear();
// Iterate over all frames of the saved message and assign to the data maps
for (size_t i = 0; i < stabilizationMessage.frame_size(); i++) {
// Create stabilization message
const pb_stabilize::Frame& pbFrameData = stabilizationMessage.frame(i);
// Load frame number
size_t id = pbFrameData.id();
// Load camera trajectory data
float x = pbFrameData.x();
float y = pbFrameData.y();
float a = pbFrameData.a();
// Assign data to trajectory map
trajectoryData[i] = EffectCamTrajectory(x,y,a);
// Load transformation data
float dx = pbFrameData.dx();
float dy = pbFrameData.dy();
float da = pbFrameData.da();
// Assing data to transformation map
transformationData[id] = EffectTransformParam(dx,dy,da);
}
// Delete all global objects allocated by libprotobuf.
google::protobuf::ShutdownProtobufLibrary();
return true;
}
// Generate JSON string of this object
std::string Stabilizer::Json() const {
// Return formatted string
return JsonValue().toStyledString();
}
// Generate Json::Value for this object
Json::Value Stabilizer::JsonValue() const {
// Create root json object
Json::Value root = EffectBase::JsonValue(); // get parent properties
root["type"] = info.class_name;
root["protobuf_data_path"] = protobuf_data_path;
root["zoom"] = zoom.JsonValue();
// return JsonValue
return root;
}
// Load JSON string into this object
void Stabilizer::SetJson(const std::string value) {
// Parse JSON string into JSON objects
try
{
const Json::Value root = openshot::stringToJson(value);
// Set all values that match
SetJsonValue(root);
}
catch (const std::exception& e)
{
// Error parsing JSON (or missing keys)
throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
}
}
// Load Json::Value into this object
void Stabilizer::SetJsonValue(const Json::Value root) {
// Set parent data
EffectBase::SetJsonValue(root);
// Set data from Json (if key is found)
if (!root["protobuf_data_path"].isNull()){
protobuf_data_path = (root["protobuf_data_path"].asString());
if(!LoadStabilizedData(protobuf_data_path)){
std::cout<<"Invalid protobuf data path";
protobuf_data_path = "";
}
}
if(!root["zoom"].isNull())
zoom.SetJsonValue(root["zoom"]);
}
// Get all properties for a specific frame
std::string Stabilizer::PropertiesJSON(int64_t requested_frame) const {
// Generate JSON properties list
Json::Value root;
root["id"] = add_property_json("ID", 0.0, "string", Id(), NULL, -1, -1, true, requested_frame);
root["position"] = add_property_json("Position", Position(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
root["layer"] = add_property_json("Track", Layer(), "int", "", NULL, 0, 20, false, requested_frame);
root["start"] = add_property_json("Start", Start(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
root["end"] = add_property_json("End", End(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
root["duration"] = add_property_json("Duration", Duration(), "float", "", NULL, 0, 1000 * 60 * 30, true, requested_frame);
root["zoom"] = add_property_json("Zoom", zoom.GetValue(requested_frame), "float", "", &zoom, 0.0, 2.0, false, requested_frame);
// Set the parent effect which properties this effect will inherit
root["parent_effect_id"] = add_property_json("Parent", 0.0, "string", info.parent_effect_id, NULL, -1, -1, false, requested_frame);
// Return formatted string
return root.toStyledString();
}
Exceptions.h
Header file for all Exception classes.
Stabilizer.h
Header file for Stabilizer effect class.
openshot::ClipBase::Start
float Start() const
Get start position (in seconds) of clip (trim start of video)
Definition ClipBase.h:88
openshot::ClipBase::Duration
float Duration() const
Get the length of this clip (in seconds)
Definition ClipBase.h:90
openshot::ClipBase::End
virtual float End() const
Get end position (in seconds) of clip (trim end of video)
Definition ClipBase.h:89
openshot::ClipBase::Id
std::string Id() const
Get the Id of this clip object.
Definition ClipBase.h:85
openshot::ClipBase::Layer
int Layer() const
Get layer of clip on timeline (lower number is covered by higher numbers)
Definition ClipBase.h:87
openshot::ClipBase::Position
float Position() const
Get position on timeline (in seconds)
Definition ClipBase.h:86
openshot::ClipBase::id
std::string id
ID Property for all derived Clip and Effect classes.
Definition ClipBase.h:35
openshot::ClipBase::add_property_json
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
openshot::EffectBase::JsonValue
virtual Json::Value JsonValue() const
Generate Json::Value for this object.
Definition EffectBase.cpp:77
openshot::EffectBase::InitEffectInfo
void InitEffectInfo()
Definition EffectBase.cpp:24
openshot::EffectBase::SetJsonValue
virtual void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition EffectBase.cpp:112
openshot::EffectBase::info
EffectInfoStruct info
Information about the current effect.
Definition EffectBase.h:69
openshot::InvalidJSON
Exception for invalid JSON.
Definition Exceptions.h:218
openshot::Keyframe::SetJsonValue
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition KeyFrame.cpp:372
openshot::Keyframe::GetValue
double GetValue(int64_t index) const
Get the value at a specific index.
Definition KeyFrame.cpp:258
openshot::Keyframe::JsonValue
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition KeyFrame.cpp:339
openshot::Stabilizer::JsonValue
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition Stabilizer.cpp:164
openshot::Stabilizer::GetFrame
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:63
openshot::Stabilizer::transformationData
std::map< size_t, EffectTransformParam > transformationData
Definition Stabilizer.h:77
openshot::Stabilizer::PropertiesJSON
std::string PropertiesJSON(int64_t requested_frame) const override
Definition Stabilizer.cpp:213
openshot::Stabilizer::SetJson
void SetJson(const std::string value) override
Load JSON string into this object.
Definition Stabilizer.cpp:177
openshot::Stabilizer::SetJsonValue
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition Stabilizer.cpp:194
openshot::Stabilizer::trajectoryData
std::map< size_t, EffectCamTrajectory > trajectoryData
Definition Stabilizer.h:76
openshot::Stabilizer::Stabilizer
Stabilizer()
Definition Stabilizer.cpp:38
openshot::Stabilizer::LoadStabilizedData
bool LoadStabilizedData(std::string inputFilePath)
Load protobuf data file.
Definition Stabilizer.cpp:106
openshot::Stabilizer::Json
std::string Json() const override
Generate JSON string of this object.
Definition Stabilizer.cpp:157
openshot
This namespace is the default namespace for all code in the openshot library.
Definition Compressor.h:29
openshot::stringToJson
const Json::Value stringToJson(const std::string value)
Definition Json.cpp:16
EffectCamTrajectory
Definition Stabilizer.h:41
EffectTransformParam
Definition Stabilizer.h:26
openshot::EffectInfoStruct::has_video
bool has_video
Determines if this effect manipulates the image of a frame.
Definition EffectBase.h:40
openshot::EffectInfoStruct::parent_effect_id
std::string parent_effect_id
Id of the parent effect (if there is one)
Definition EffectBase.h:39
openshot::EffectInfoStruct::has_audio
bool has_audio
Determines if this effect manipulates the audio of a frame.
Definition EffectBase.h:41
openshot::EffectInfoStruct::class_name
std::string class_name
The class name of the effect.
Definition EffectBase.h:36
openshot::EffectInfoStruct::name
std::string name
The name of the effect.
Definition EffectBase.h:37
openshot::EffectInfoStruct::description
std::string description
The description of this effect and what it does.
Definition EffectBase.h:38