This method is required for all derived classes of EffectBase, and returns a modified openshot::Frame object
| 47 | // This method is required for all derived classes of EffectBase, and returns a |
| 48 | // modified openshot::Frame object |
| 49 | std::shared_ptr<openshot::Frame> Outline::GetFrame(std::shared_ptr<openshot::Frame> frame, int64_t frame_number) |
| 50 | { |
| 51 | float widthValue = width.GetValue(frame_number); |
| 52 | int blueValue = color.blue.GetValue(frame_number); |
| 53 | int greenValue = color.green.GetValue(frame_number); |
| 54 | int redValue = color.red.GetValue(frame_number); |
| 55 | int alphaValue = color.alpha.GetValue(frame_number); |
| 56 | |
| 57 | if (widthValue <= 0.0 || alphaValue <= 0) { |
| 58 | // If alpha or width is zero, return the original frame |
| 59 | return frame; |
| 60 | } |
| 61 | |
| 62 | // Get the frame's image |
| 63 | std::shared_ptr<QImage> frame_image = frame->GetImage(); |
| 64 | |
| 65 | float sigmaValue = widthValue / 3.0; |
| 66 | if (sigmaValue <= 0.0) |
| 67 | sigmaValue = 0.01; |
| 68 | cv::Mat cv_image = QImageToBGRACvMat(frame_image); |
| 69 | |
| 70 | // Extract alpha channel for the mask |
| 71 | std::vector<cv::Mat> channels(4); |
| 72 | cv::split(cv_image, channels); |
| 73 | cv::Mat alpha_mask = channels[3].clone(); |
| 74 | |
| 75 | // Create the outline mask |
| 76 | cv::Mat outline_mask; |
| 77 | cv::GaussianBlur(alpha_mask, outline_mask, cv::Size(0, 0), sigmaValue, sigmaValue, cv::BorderTypes::BORDER_DEFAULT); |
| 78 | cv::threshold(outline_mask, outline_mask, 0, 255, cv::ThresholdTypes::THRESH_BINARY); |
| 79 | |
| 80 | // Antialias the outline edge & apply Canny edge detection |
| 81 | cv::Mat edge_mask; |
| 82 | cv::Canny(outline_mask, edge_mask, 250, 255); |
| 83 | |
| 84 | // Apply Gaussian blur only to the edge mask |
| 85 | cv::Mat blurred_edge_mask; |
| 86 | cv::GaussianBlur(edge_mask, blurred_edge_mask, cv::Size(0, 0), 0.8, 0.8, cv::BorderTypes::BORDER_DEFAULT); |
| 87 | cv::bitwise_or(outline_mask, blurred_edge_mask, outline_mask); |
| 88 | |
| 89 | cv::Mat final_image; |
| 90 | |
| 91 | // Create solid color source mat (cv::Scalar: red, green, blue, alpha) |
| 92 | cv::Mat solid_color_mat(cv::Size(cv_image.cols, cv_image.rows), CV_8UC4, cv::Scalar(redValue, greenValue, blueValue, alphaValue)); |
| 93 | |
| 94 | // Place outline first, then the original image on top |
| 95 | solid_color_mat.copyTo(final_image, outline_mask); |
| 96 | cv_image.copyTo(final_image, alpha_mask); |
| 97 | |
| 98 | std::shared_ptr<QImage> new_frame_image = BGRACvMatToQImage(final_image); |
| 99 | |
| 100 | // FIXME: The shared_ptr::swap does not work somehow |
| 101 | *frame_image = *new_frame_image; |
| 102 | return frame; |
| 103 | } |
| 104 | |
| 105 | cv::Mat Outline::QImageToBGRACvMat(std::shared_ptr<QImage>& qimage) { |
| 106 | cv::Mat cv_img(qimage->height(), qimage->width(), CV_8UC4, (uchar*)qimage->constBits(), qimage->bytesPerLine()); |