Ask Your Question

How to speed up seamlessClone? (incredibly slow)

asked 2015-07-22 10:08:30 -0500

Fragender gravatar image


I am using seamlessClone to integrate a logo into a video and it is incredibly slow. Actually creating a video with 1000 frames with a resolution of 1280 x 800 takes 30 seconds. While using seamlessClone it takes round about 15 seconds to write one single frame!

I am not sure whether I use this function in a wrong way or whether seamlessClone is really that heavy.

This is basically what I do:

int main() {
cv::Mat logo= imread("logo.png", CV_LOAD_IMAGE_COLOR);

Size size(100,120);
Mat LogoResized;
Point center(300,300);

Mat mask_forCloning = 255 * Mat::ones(LogoResized.rows, LogoResized.cols, LogoResized.depth());
Mat normal_clone;
Mat image;

for (int i=0; i<1000; i++)
    image = imread(pictureName, CV_LOAD_IMAGE_COLOR);  //Of course the name is changing with every frame ;-)

seamlessClone(LogoResized, image, mask_forCloning, center, normal_clone, NORMAL_CLONE);



Is there a possibility to speed this up?

Thank you very much :-)

edit retag flag offensive close merge delete


let me try some other way if you share your logo. ( or any png having same characteristic)

sturkmen gravatar imagesturkmen ( 2015-08-05 05:00:22 -0500 )edit

Hi, thank you very much. Due to rights I cannot provide you the original logo, but the following has the same characteristics and I just tested the cloning with it. It is comparable slow like with the logo I use. Image

Thanks a lot :-)

Fragender gravatar imageFragender ( 2015-08-05 06:40:14 -0500 )edit

2 answers

Sort by » oldest newest most voted

answered 2015-08-05 18:33:09 -0500

updated 2015-08-05 19:04:00 -0500

with the code below i tried to achive desired result instead of using cv::seamlessClone. i hope it helps.

references: @Alexander Shishkov @berak Martin Peris

thanks all.

used image opencv-logo-150.png

#include "opencv2/opencv.hpp"

using namespace cv;

int main(int argc, char** argv)
    VideoCapture cap(0);

        return 0;

    int x, y;
    x = 100;
    y = 100;

    Mat mask;
    Mat c[4];

    Mat logo= imread("opencv-logo-150.png", -1);
    //Mat logo= imread("bmp.bmp", -1); // must have white background

    Size nsize(logo.cols / 2, logo.rows / 2);


        split(logo,c);         // seperate channels
        Mat cs[3] = { c[0],c[1],c[2] };
        merge(cs,3,logo);  // glue together again
        mask = c[3];       // png's alpha channel used as mask
    cvtColor( logo, mask, COLOR_BGR2GRAY );
    mask = mask < 255;

        Mat frame;

        cap >> frame;
        if( frame.empty() ) break; // end of video stream

        logo.copyTo(frame(cv::Rect(x,y,logo.cols, logo.rows)),mask);

        imshow("VideoCapture", frame);
        if( waitKey(1) == 27 ) break; // stop capturing by pressing ESC

    return 0;
edit flag offensive delete link more

answered 2015-08-07 06:14:48 -0500

Fragender gravatar image

updated 2015-08-07 07:05:56 -0500

Hi sturkmen,

thank you very much for the nice code!

No just the background of the normal seamlessClone is missing.

This can be achieved by increasing the brightness and then blurring.

I just added this code before the logo.copyTo:

    double alpha=1.0; //alpha value [1.0-3.0]
    int beta=70; //beta value [0-100]

    /// Increasing brightness:
    /// Do the operation image(i,j) = alpha*image(i,j) + beta
     for( int y2 = y; y2 < mask.rows+y; y2++ )
         for( int x2 = x; x2 < mask.cols+x; x2++ )
                for( int c = 0; c < 3; c++ )
          <Vec3b>(y2,x2)[c] =
                    saturate_cast<uchar>( alpha*(<Vec3b>(y2,x2)[c] ) + beta );
    cv::Rect region (x,y,logo.cols, logo.rows);

    GaussianBlur(frame(region), frame(region), Size(0, 0), 6);


There is just one thing I do not know how to manage. As you can see I used manual values in the first two for loops (mask.rows+100). I do not know where to get the real dimensions of the logo automatically as it is copied using a mask.

Still... the solution works now quite nice :-) Thanks a lot!

edit flag offensive delete link more



your welcome. your addition is good. i made correction to your code as

for( int y2 = y; y2 < mask.rows+y; y2++ ) { for( int x2 = x; x2 < mask.cols+x; x2++ )

sturkmen gravatar imagesturkmen ( 2015-08-07 06:24:50 -0500 )edit

a short way to increas brightness instead loop is frame(region).convertTo(frame(region),-1,1,70);

sturkmen gravatar imagesturkmen ( 2015-08-07 09:04:19 -0500 )edit

Thank you very much for the perfect help :-)

Fragender gravatar imageFragender ( 2015-08-11 03:18:21 -0500 )edit

Question Tools



Asked: 2015-07-22 10:08:30 -0500

Seen: 2,214 times

Last updated: Aug 07 '15