Ask Your Question

copyTo() works only the first time

asked 2017-11-03 10:05:47 -0500

Andriezel gravatar image


I have a problem with my copyTo() function. I have a mask and I'm editing that mask using the cv::circle() function.

It works very well the first time but when I use it the second time the image just doesn't change. The mask is working allright though, that's the weird part!

Here's some code:

 //init Point erasor
    erasor.x = x;
    erasor.y = y;
    if (isEraserActive) {
        circle(result, erasor, 5, GC_BGD, -1); //edit mask
        maskGC = true;
//debugging to see if x and y point are actually in the vector
    if (isEraserActive == false) {

        std::ofstream stream;"D:/biem.txt");

        for (int i = 0; i < vec.size(); i++) {
            stream << << "\n";


         if (maskGC != true) {

    // GrabCut segmentation
    cv::grabCut(imgDebayer,    // input image
        result,   // segmentation result
        rect,// rectangle containing foreground
        bgModel, fgModel, // models
        3,        // number of iterations
        GC_INIT_WITH_RECT); // use rectangle

    // Get the pixels marked as likely foreground
    cv::compare(result, cv::GC_PR_FGD, result, cv::CMP_EQ);

    imgDebayer.copyTo(foreground, result); // bg pixels not copied
    imgResult = foreground;
    cv::rectangle(imgResult, rect, cv::Scalar(255, 0, 255), 1);

else {

    //imshow("result", result);
    imgDebayer.copyTo(res, result);
    imgResult = res;


I don't know why it doesn't work after the first time. It has to work right?

edit retag flag offensive close merge delete


I think your grabcut flags are wrong, among other things. You have INIT_WITH_RECT, but not INIT_WITH_MASK, so result is ignored as input.

Secondly, in the compare function, you only keep the PR_FGD pixels, so if something is marked definitely foreground, it's lost.

Tetragramm gravatar imageTetragramm ( 2017-11-03 10:29:58 -0500 )edit

Hi, thanks for the comment. I have INIT_WITH_RECT because I'm using the grabcut function only once. After that it sets maskGC to true so it doesn't use the Grabcut function anymore but directly copies the mask to res after I have edited it with my 'eraser'.

Andriezel gravatar imageAndriezel ( 2017-11-06 01:34:15 -0500 )edit

So, where does this loop? Around both of these snippets? Around just the second one? Is there any other place you touch the contents of result?

Tetragramm gravatar imageTetragramm ( 2017-11-06 18:12:16 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2017-11-08 01:40:15 -0500

Andriezel gravatar image

I already figured it out. What I did was delete the if else part and put this code in the ToggleEraser() function:

    Mat res;
    image.copy(res, result);

in that way it creates a new Mat for me everytime I want to use my erasor and 'overwrites' the previous image thus making it work.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2017-11-03 10:05:47 -0500

Seen: 116 times

Last updated: Nov 08 '17