forked from OpenShot/libopenshot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOutline.cpp
More file actions
183 lines (147 loc) · 5.82 KB
/
Copy pathOutline.cpp
File metadata and controls
183 lines (147 loc) · 5.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* @file
* @brief Source file for Outline effect class
* @author Jonathan Thomas <jonathan@openshot.org>
* @author HaiVQ <me@haivq.com>
*
* @ref License
*/
// Copyright (c) 2008-2019 OpenShot Studios, LLC
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#include "Outline.h"
#include "Exceptions.h"
using namespace openshot;
/// Blank constructor, useful when using Json to load the effect properties
Outline::Outline() : width(0.0) {
// Init effect properties
color = Color("#FFFFFF");
init_effect_details();
}
// Default constructor
Outline::Outline(Keyframe width, Color color) :
width(width), color(color)
{
// Init effect properties
init_effect_details();
}
// Init effect settings
void Outline::init_effect_details()
{
/// Initialize the values of the EffectInfo struct.
InitEffectInfo();
/// Set the effect info
info.class_name = "Outline";
info.name = "Outline";
info.description = "Add outline around any image or text.";
info.has_audio = false;
info.has_video = true;
}
// This method is required for all derived classes of EffectBase, and returns a
// modified openshot::Frame object
std::shared_ptr<openshot::Frame> Outline::GetFrame(std::shared_ptr<openshot::Frame> frame, int64_t frame_number)
{
float widthValue = width.GetValue(frame_number);
int blueValue = color.blue.GetValue(frame_number);
int greenValue = color.green.GetValue(frame_number);
int redValue = color.red.GetValue(frame_number);
int alphaValue = color.alpha.GetValue(frame_number);
if (widthValue <= 0.0 || alphaValue <= 0) {
// If alpha or width is zero, return the original frame
return frame;
}
// Get the frame's image
cv::Mat cv_image = frame->GetBGRACvMat();
float sigmaValue = widthValue / 3.0;
if (sigmaValue <= 0.0)
sigmaValue = 0.01;
// Extract alpha channel for the mask
std::vector<cv::Mat> channels(4);
cv::split(cv_image, channels);
cv::Mat alpha_mask = channels[3].clone();
// Create the outline mask
cv::Mat outline_mask;
cv::GaussianBlur(alpha_mask, outline_mask, cv::Size(0, 0), sigmaValue, sigmaValue, cv::BorderTypes::BORDER_DEFAULT);
cv::threshold(outline_mask, outline_mask, 0, 255, cv::ThresholdTypes::THRESH_BINARY);
// Antialias the outline edge & apply Canny edge detection
cv::Mat edge_mask;
cv::Canny(outline_mask, edge_mask, 250, 255);
// Apply Gaussian blur only to the edge mask
cv::Mat blurred_edge_mask;
cv::GaussianBlur(edge_mask, blurred_edge_mask, cv::Size(0, 0), 0.8, 0.8, cv::BorderTypes::BORDER_DEFAULT);
cv::bitwise_or(outline_mask, blurred_edge_mask, outline_mask);
cv::Mat final_image;
// Create solid color source mat (cv::Scalar: red, green, blue, alpha)
cv::Mat solid_color_mat(cv::Size(cv_image.cols, cv_image.rows), CV_8UC4, cv::Scalar(redValue, greenValue, blueValue, alphaValue));
// Place outline first, then the original image on top
solid_color_mat.copyTo(final_image, outline_mask);
cv_image.copyTo(final_image, alpha_mask);
frame->SetBGRACvMat(final_image);
return frame;
}
// Moved to Frame.cpp
// cv::Mat Outline::QImageToBGRACvMat(std::shared_ptr<QImage>& qimage) {
// cv::Mat cv_img(qimage->height(), qimage->width(), CV_8UC4, (uchar*)qimage->constBits(), qimage->bytesPerLine());
// return cv_img;
// }
// std::shared_ptr<QImage> Outline::BGRACvMatToQImage(cv::Mat img) {
// cv::Mat final_img;
// cv::cvtColor(img, final_img, cv::COLOR_RGBA2BGRA);
// QImage qimage(final_img.data, final_img.cols, final_img.rows, final_img.step, QImage::Format_ARGB32);
// std::shared_ptr<QImage> imgIn = std::make_shared<QImage>(qimage.convertToFormat(QImage::Format_RGBA8888_Premultiplied));
// return imgIn;
// }
// Generate JSON string of this object
std::string Outline::Json() const {
// Return formatted string
return JsonValue().toStyledString();
}
// Generate Json::Value for this object
Json::Value Outline::JsonValue() const {
// Create root json object
Json::Value root = EffectBase::JsonValue(); // get parent properties
root["type"] = info.class_name;
root["width"] = width.JsonValue();
root["color"] = color.JsonValue();
// return JsonValue
return root;
}
// Load JSON string into this object
void Outline::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 Outline::SetJsonValue(const Json::Value root) {
// Set parent data
EffectBase::SetJsonValue(root);
// Set data from Json (if key is found)
if (!root["width"].isNull())
width.SetJsonValue(root["width"]);
if (!root["color"].isNull())
color.SetJsonValue(root["color"]);
}
// Get all properties for a specific frame
std::string Outline::PropertiesJSON(int64_t requested_frame) const {
// Generate JSON properties list
Json::Value root = BasePropertiesJSON(requested_frame);
// Keyframes
root["width"] = add_property_json("Width", width.GetValue(requested_frame), "float", "", &width, 0, 100, false, requested_frame);
root["color"] = add_property_json("Key Color", 0.0, "color", "", &color.red, 0, 255, false, requested_frame);
root["color"]["red"] = add_property_json("Red", color.red.GetValue(requested_frame), "float", "", &color.red, 0, 255, false, requested_frame);
root["color"]["blue"] = add_property_json("Blue", color.blue.GetValue(requested_frame), "float", "", &color.blue, 0, 255, false, requested_frame);
root["color"]["green"] = add_property_json("Green", color.green.GetValue(requested_frame), "float", "", &color.green, 0, 255, false, requested_frame);
root["color"]["alpha"] = add_property_json("Alpha", color.alpha.GetValue(requested_frame), "float", "", &color.alpha, 0, 255, false, requested_frame);
// Return formatted string
return root.toStyledString();
}