copyTo() works only the first time
Hello.
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) {
vec.push_back(erasor);
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;
stream.open("D:/biem.txt");
for (int i = 0; i < vec.size(); i++) {
stream << vec.at(i) << "\n";
}
stream.close();
}
[...]
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?
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.
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'.
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?