Skip to content

Updated grabcut example to show the background in a transparant way#20200

Merged
opencv-pushbot merged 1 commit intoopencv:3.4from
rmeertens:update_grabcut
Jul 16, 2021
Merged

Updated grabcut example to show the background in a transparant way#20200
opencv-pushbot merged 1 commit intoopencv:3.4from
rmeertens:update_grabcut

Conversation

@rmeertens
Copy link
Copy Markdown
Contributor

@rmeertens rmeertens commented Jun 2, 2021

Hey all, I was playing around with the Grabcut example (https://docs.opencv.org/3.4/d8/d83/tutorial_py_grabcut.html) and was annoyed that the background is completely black, instead of showing the original image in a transparant way.

I updated the code to make it transparant. I also made the application a bit more interactive, so you don't have to press 'n' any time you add some foreground or background, but it automatically runs at least one cycle.

Image of the result:
Screenshot 2021-06-02 at 14 14 18

Let me know if you have any feedback :)

@rmeertens
Copy link
Copy Markdown
Contributor Author

Update: fixed whitespace. Hopefully my build passes this time :)

@asmorkalov
Copy link
Copy Markdown
Contributor

Thanks for useful improvement. This patch should go into 3.4 branch first. We will merge changes from 3.4 into master regularly (weekly/bi-weekly).

So, please:

  • change "base" branch of this PR: master => 3.4 (use "Edit" button near PR title)
  • rebase your commits from master onto 3.4 branch. For example:
    git rebase -i --onto upstream/3.4 upstream/master
    (check list of your commits, save and quit (Esc + "wq" + Enter)
    where upstream is configured by following this GitHub guide and fetched (git fetch upstream).
  • push rebased commits into source branch of your fork (with --force option)

Note: no need to re-open PR, apply changes "inplace".

@asmorkalov asmorkalov added category: samples pr: needs rebase Rebase patch (and squash fixup commits) on the top of target branch labels Jun 4, 2021
@rmeertens rmeertens changed the base branch from master to 3.4 June 4, 2021 12:03
@rmeertens
Copy link
Copy Markdown
Contributor Author

Done. Thanks for the great explanation, that was really helpful!

@asmorkalov asmorkalov removed the pr: needs rebase Rebase patch (and squash fixup commits) on the top of target branch label Jun 4, 2021
@asmorkalov asmorkalov self-requested a review June 7, 2021 09:07

Mat black (binMask.rows, binMask.cols, CV_8UC3, cv::Scalar(0,0,0)) ;
Mat white (binMask.rows, binMask.cols, CV_8UC3, cv::Scalar(255,255,255)) ;
add(black, white, black, binMask);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

black.setTo(Scalar::all(255), binMask)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great proposal, changed!

Comment on lines 110 to 113
image->copyTo( res );
if( isInitialized ){
getBinMask( mask, binMask);
image->copyTo( res );
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we call image->copyTo( res ); twice?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question... Removed :)

@rmeertens rmeertens force-pushed the update_grabcut branch 4 times, most recently from b4163fe to 5b2e7ca Compare June 8, 2021 16:12
@asmorkalov
Copy link
Copy Markdown
Contributor

I think that nextIter call everywhere is bad idea. I observe sample state machine corruption in new version of the sample. For example, if you just click on image, but not select box the sample crashes with grabcut.cpp:385: error: (-215:Assertion failed) !bgdSamples.empty() && !fgdSamples.empty() in function 'initGMMs' and sigsegv. The nextIter call sets isInitialized to true and unblocks grabcut and your showImage logic even with empty box.

@rmeertens
Copy link
Copy Markdown
Contributor Author

I can go over the logic this weekend to make it a bit better :)

@asmorkalov
Copy link
Copy Markdown
Contributor

@rmeertens Friendly reminder.

1 similar comment
@asmorkalov
Copy link
Copy Markdown
Contributor

@rmeertens Friendly reminder.

@rmeertens
Copy link
Copy Markdown
Contributor Author

Thanks for the reminder! Just back from a two-week trip, will update it this week!

@rmeertens
Copy link
Copy Markdown
Contributor Author

Finally had time to go over this pull request again! I now actually verify that we are in the correct state when calling nextIter. The other problem I found, which was the actual reason for the crash, is that clicking produces an empty rectangle. Whenever someone tries to draw an empty rectangle we just go back to the state 'rectangle not set'. Hope this helps :)

Let me know if you find any other bugs/problems!

Copy link
Copy Markdown
Contributor

@asmorkalov asmorkalov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works for me without crashes. 👍

@opencv-pushbot opencv-pushbot merged commit cc2592f into opencv:3.4 Jul 16, 2021
@alalek alalek mentioned this pull request Jul 16, 2021
@alalek alalek mentioned this pull request Oct 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants