diff --git a/src/Frame.cpp b/src/Frame.cpp index f799bcea9..7c6357100 100644 --- a/src/Frame.cpp +++ b/src/Frame.cpp @@ -2,6 +2,7 @@ * @file * @brief Source file for Frame class * @author Jonathan Thomas + * @author HaiVQ * * @ref License */ @@ -112,6 +113,7 @@ Frame::~Frame() { audio.reset(); #ifdef USE_OPENCV imagecv.release(); + brga_image_cv.release(); #endif } @@ -890,6 +892,13 @@ cv::Mat Frame::GetImageCV() return imagecv; } +// Set pointer to OpenCV image object +void Frame::SetImageCV(cv::Mat _image) +{ + imagecv = _image; + image = Mat2Qimage(_image); +} + std::shared_ptr Frame::Mat2Qimage(cv::Mat img){ cv::cvtColor(img, img, cv::COLOR_BGR2RGB); QImage qimg((uchar*) img.data, img.cols, img.rows, img.step, QImage::Format_RGB888); @@ -903,11 +912,39 @@ std::shared_ptr Frame::Mat2Qimage(cv::Mat img){ return imgIn; } -// Set pointer to OpenCV image object -void Frame::SetImageCV(cv::Mat _image) -{ - imagecv = _image; - image = Mat2Qimage(_image); +// Convert QImage to cv::Mat and vice versa +// Frame class has GetImageCV, but it does not include alpha channel +// so we need a separate methods which preserve alpha channel +cv::Mat Frame::QImage2BGRACvMat(std::shared_ptr& qimage) { + cv::Mat cv_img( + qimage->height(), qimage->width(), + CV_8UC4, (uchar*)qimage->constBits(), + qimage->bytesPerLine() + ); + return cv_img; +} + +// Convert cv::Mat back to QImage +std::shared_ptr Frame::BGRACvMat2QImage(cv::Mat img) { + cv::Mat final_img; + cv::cvtColor(img, final_img, cv::COLOR_BGRA2RGBA); + QImage qimage(final_img.data, final_img.cols, final_img.rows, final_img.step, QImage::Format_ARGB32); + std::shared_ptr imgIn = std::make_shared(qimage.convertToFormat(QImage::Format_RGBA8888_Premultiplied)); + return imgIn; +} + +// Get BGRA +cv::Mat Frame::GetBGRACvMat() { + if (!image) + // Fill with black + AddColor(width, height, color); + brga_image_cv = QImage2BGRACvMat(image); + return brga_image_cv; +} + +void Frame::SetBGRACvMat(cv::Mat _image) { + brga_image_cv = _image; + image = BGRACvMat2QImage(_image); } #endif diff --git a/src/Frame.h b/src/Frame.h index 528a69b85..08cafc2b6 100644 --- a/src/Frame.h +++ b/src/Frame.h @@ -2,6 +2,7 @@ * @file * @brief Header file for Frame class * @author Jonathan Thomas + * @author HaiVQ * * @ref License */ @@ -106,6 +107,7 @@ namespace openshot #ifdef USE_OPENCV cv::Mat imagecv; ///< OpenCV image. It will always be in BGR format + cv::Mat brga_image_cv; ///< OpenCV image. It will always be in BGR format #endif /// Constrain a color value from 0 to 255 @@ -277,6 +279,18 @@ namespace openshot /// Set pointer to OpenCV image object void SetImageCV(cv::Mat _image); + + /// Convert QImage to OpenCV Mat (alpha channel included) + cv::Mat QImage2BGRACvMat(std::shared_ptr& qimage); + + /// Convert OpenCV Mat to QImage (alpha channel included) + std::shared_ptr BGRACvMat2QImage(cv::Mat img); + + /// Get pointer to OpenCV Mat image object (with alpha channel) + cv::Mat GetBGRACvMat(); + + /// Set pointer to OpenCV image object (with alpha channel) + void SetBGRACvMat(cv::Mat _image); #endif }; diff --git a/src/effects/Outline.cpp b/src/effects/Outline.cpp index bcd9e18ba..2948d8dd4 100644 --- a/src/effects/Outline.cpp +++ b/src/effects/Outline.cpp @@ -1,8 +1,9 @@ /** * @file * @brief Source file for Outline effect class - * @author Jonathan Thomas , HaiVQ - * + * @author Jonathan Thomas + * @author HaiVQ + * * @ref License */ @@ -60,12 +61,11 @@ std::shared_ptr Outline::GetFrame(std::shared_ptr frame_image = frame->GetImage(); - + cv::Mat cv_image = frame->GetBGRACvMat(); + 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 channels(4); @@ -95,25 +95,24 @@ std::shared_ptr Outline::GetFrame(std::shared_ptr new_frame_image = BGRACvMatToQImage(final_image); - - // FIXME: The shared_ptr::swap does not work somehow - *frame_image = *new_frame_image; + frame->SetBGRACvMat(final_image); + return frame; } -cv::Mat Outline::QImageToBGRACvMat(std::shared_ptr& qimage) { - cv::Mat cv_img(qimage->height(), qimage->width(), CV_8UC4, (uchar*)qimage->constBits(), qimage->bytesPerLine()); - return cv_img; -} - -std::shared_ptr 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 imgIn = std::make_shared(qimage.convertToFormat(QImage::Format_RGBA8888_Premultiplied)); - return imgIn; -} +// Moved to Frame.cpp +// cv::Mat Outline::QImageToBGRACvMat(std::shared_ptr& qimage) { +// cv::Mat cv_img(qimage->height(), qimage->width(), CV_8UC4, (uchar*)qimage->constBits(), qimage->bytesPerLine()); +// return cv_img; +// } + +// std::shared_ptr 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 imgIn = std::make_shared(qimage.convertToFormat(QImage::Format_RGBA8888_Premultiplied)); +// return imgIn; +// } // Generate JSON string of this object std::string Outline::Json() const { diff --git a/src/effects/Outline.h b/src/effects/Outline.h index 99cb91eeb..3e45cad31 100644 --- a/src/effects/Outline.h +++ b/src/effects/Outline.h @@ -1,7 +1,8 @@ /** * @file * @brief Header file for Outline effect class - * @author Jonathan Thomas , HaiVQ + * @author Jonathan Thomas + * @author HaiVQ * * @ref License */ @@ -33,6 +34,7 @@ namespace openshot * with openshot::Keyframe curves over time. * * Outlines can be added around any image or text, and animated over time. + * Idea from: https://stackoverflow.com/a/78480103 */ class Outline : public EffectBase { @@ -40,12 +42,9 @@ namespace openshot /// 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); - std::shared_ptr BGRACvMatToQImage(cv::Mat img); + // Moved to Frame.h + // cv::Mat QImageToBGRACvMat(std::shared_ptr& qimage); + // std::shared_ptr BGRACvMatToQImage(cv::Mat img); public: Keyframe width; ///< Width of the outline diff --git a/tests/Frame.cpp b/tests/Frame.cpp index ffe4d84dd..ba280fff8 100644 --- a/tests/Frame.cpp +++ b/tests/Frame.cpp @@ -3,6 +3,7 @@ * @brief Unit tests for openshot::Frame * @author Jonathan Thomas * @author FeRD (Frank Dana) + * @author HaiVQ * * @ref License */ @@ -160,4 +161,26 @@ TEST_CASE( "Convert_Image", "[libopenshot][opencv][frame]" ) CHECK(f1->GetHeight() == cvimage.rows); CHECK(cvimage.channels() == 3); } + +TEST_CASE( "Convert_Image_Alpha", "[libopenshot][opencv][frame]" ) +{ + // Create a video clip + std::stringstream path; + path << TEST_MEDIA_PATH << "sintel_trailer-720p.mp4"; + Clip c1(path.str()); + c1.Open(); + + // Get first frame + auto f1 = c1.GetFrame(1); + + // Get first Mat image + cv::Mat cvimage = f1->GetBGRACvMat(); + + CHECK_FALSE(cvimage.empty()); + + CHECK(f1->number == 1); + CHECK(f1->GetWidth() == cvimage.cols); + CHECK(f1->GetHeight() == cvimage.rows); + CHECK(cvimage.channels() == 4); +} #endif