Merge pull request #996 from OpenShot/outline-effect

Improvements to Outline Effect
This commit is contained in:
Jonathan Thomas
2025-03-14 22:30:53 -05:00
committed by GitHub
8 changed files with 349 additions and 1 deletions

View File

@@ -114,6 +114,7 @@
#include "effects/Stabilizer.h"
#include "effects/Tracker.h"
#include "effects/ObjectDetection.h"
#include "effects/Outline.h"
#include "TrackedObjectBase.h"
#include "TrackedObjectBBox.h"
%}
@@ -351,4 +352,5 @@
%include "effects/Stabilizer.h"
%include "effects/Tracker.h"
%include "effects/ObjectDetection.h"
%include "effects/Outline.h"
#endif

View File

@@ -99,6 +99,7 @@ set(OPENSHOT_CV_SOURCES
effects/Stabilizer.cpp
effects/Tracker.cpp
effects/ObjectDetection.cpp
effects/Outline.cpp
./sort_filter/sort.cpp
./sort_filter/Hungarian.cpp
./sort_filter/KalmanTracker.cpp)

View File

@@ -98,6 +98,9 @@ EffectBase* EffectInfo::CreateEffect(std::string effect_type) {
return new Whisperization();
#ifdef USE_OPENCV
else if (effect_type == "Outline")
return new Outline();
else if(effect_type == "Stabilizer")
return new Stabilizer();
@@ -145,7 +148,8 @@ Json::Value EffectInfo::JsonValue() {
root.append(Whisperization().JsonInfo());
#ifdef USE_OPENCV
root.append(Stabilizer().JsonInfo());
root.append(Outline().JsonInfo());
root.append(Stabilizer().JsonInfo());
root.append(Tracker().JsonInfo());
root.append(ObjectDetection().JsonInfo());
#endif

View File

@@ -43,6 +43,7 @@
/* OpenCV Effects */
#ifdef USE_OPENCV
#include "effects/Outline.h"
#include "effects/ObjectDetection.h"
#include "effects/Tracker.h"
#include "effects/Stabilizer.h"

184
src/effects/Outline.cpp Normal file
View File

@@ -0,0 +1,184 @@
/**
* @file
* @brief Source file for Outline effect class
* @author Jonathan Thomas <jonathan@openshot.org>, 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
std::shared_ptr<QImage> frame_image = frame->GetImage();
float sigmaValue = widthValue / 3.0;
if (sigmaValue <= 0.0)
sigmaValue = 0.01;
cv::Mat cv_image = QImageToBGRACvMat(frame_image);
// 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);
std::shared_ptr<QImage> new_frame_image = BGRACvMatToQImage(final_image);
// FIXME: The shared_ptr::swap does not work somehow
*frame_image = *new_frame_image;
return frame;
}
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();
}

94
src/effects/Outline.h Normal file
View File

@@ -0,0 +1,94 @@
/**
* @file
* @brief Header file for Outline effect class
* @author Jonathan Thomas <jonathan@openshot.org>, HaiVQ <me@haivq.com>
*
* @ref License
*/
// Copyright (c) 2008-2019 OpenShot Studios, LLC
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#ifndef OPENSHOT_OUTLINE_EFFECT_H
#define OPENSHOT_OUTLINE_EFFECT_H
#include <Color.h>
#include "../EffectBase.h"
#include "../Frame.h"
#include "../Json.h"
#include "../KeyFrame.h"
#include <memory>
#include <string>
namespace openshot
{
/**
* @brief This class add an outline around image with transparent background and can be animated
* with openshot::Keyframe curves over time.
*
* Outlines can be added around any image or text, and animated over time.
*/
class Outline : public EffectBase
{
private:
/// Init effect settings
void init_effect_details();
// Convert QImage to cv::Mat and vice versa
// Although Frame class has GetImageCV, but it does not include alpha channel
// so we need a separate methods which preserve alpha channel
// Idea from: https://stackoverflow.com/a/78480103
cv::Mat QImageToBGRACvMat(std::shared_ptr<QImage>& qimage);
std::shared_ptr<QImage> BGRACvMatToQImage(cv::Mat img);
public:
Keyframe width; ///< Width of the outline
Color color; ///< Color of the outline
/// Blank constructor, useful when using Json to load the effect properties
Outline();
/// Default constructor, which require width, red, green, blue, alpha
///
/// @param width The width of the outline (between 0 and 100, rounded to int)
/// @param color The color of the outline
Outline(Keyframe width, Color color);
/// @brief This method is required for all derived classes of ClipBase, and returns a
/// new openshot::Frame object. All Clip keyframes and effects are resolved into
/// pixels.
///
/// @returns A new openshot::Frame object
/// @param frame_number The frame number (starting at 1) of the clip or effect on the timeline.
std::shared_ptr<openshot::Frame> GetFrame(int64_t frame_number) override { return GetFrame(std::make_shared<openshot::Frame>(), frame_number); }
/// @brief This method is required for all derived classes of ClipBase, and returns a
/// modified openshot::Frame object
///
/// The frame object is passed into this method and used as a starting point (pixels and audio).
/// All Clip keyframes and effects are resolved into pixels.
///
/// @returns The modified openshot::Frame object
/// @param frame The frame object that needs the clip or effect applied to it
/// @param frame_number The frame number (starting at 1) of the clip or effect on the timeline.
std::shared_ptr<openshot::Frame> GetFrame(std::shared_ptr<openshot::Frame> frame, int64_t frame_number) override;
// Get and Set JSON methods
std::string Json() const override; ///< Generate JSON string of this object
void SetJson(const std::string value) override; ///< Load JSON string into this object
Json::Value JsonValue() const override; ///< Generate Json::Value for this object
void SetJsonValue(const Json::Value root) override; ///< Load Json::Value into this object
/// Get all properties for a specific frame (perfect for a UI to display the current state
/// of all properties at any time)
std::string PropertiesJSON(int64_t requested_frame) const override;
};
}
#endif

View File

@@ -58,6 +58,7 @@ if($CACHE{HAVE_OPENCV})
list(APPEND OPENSHOT_TESTS
CVTracker
CVStabilizer
CVOutline
# CVObjectDetection
)
endif()

61
tests/CVOutline.cpp Normal file
View File

@@ -0,0 +1,61 @@
/**
* @file
* @brief Unit tests for OpenCV Outline effect
* @author Jonathan Thomas <jonathan@openshot.org>
*
* @ref License
*/
// Copyright (c) 2008-2025 OpenShot Studios, LLC
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#include <sstream>
#include <memory>
#include <cmath>
#include "openshot_catch.h"
#include "Clip.h"
#include "effects/Outline.h"
using namespace openshot;
TEST_CASE( "Outline_Tests", "[libopenshot][opencv][outline]" )
{
// Create a video clip
std::stringstream path;
path << TEST_MEDIA_PATH << "1F0CF.svg";
// Open clip
openshot::Clip c(path.str());
c.Open();
auto f = c.GetFrame(1);
// Create effect constructor (default values)
openshot::Outline e1{};
// Get frame from effect
auto f1 = e1.GetFrame(f, 1);
std::shared_ptr<QImage> i1 = f1->GetImage();
// Check effect colors
QColor pix1 = i1->pixelColor(3, 32);
QColor compare1{0, 0, 0, 0};
CHECK(pix1 == compare1);
// Test another effect constructor
openshot::Outline e2(Keyframe(3.0), Color(0, 0, 255, 128));
// Get frame from effect
auto f2 = e2.GetFrame(f, 1);
std::shared_ptr<QImage> i2 = f2->GetImage();
// Check effect colors
QColor pix2 = i2->pixelColor(11, 35);
QColor compare2{0, 0, 255, 128};
CHECK(pix2 == compare2);
// Close clip
c.Close();
}