Ask Your Question
0

Mask in Grabcut algorithm Opencv

asked 2017-07-09 01:21:46 -0600

arqam gravatar image

updated 2017-07-09 02:12:43 -0600

berak gravatar image

While going through the grabcut algorithm from here : http://docs.opencv.org/3.2.0/d8/d83/t... I am interested in the second part of the algorithm where whichever background/foreground don't come correctly are then stroked manually with a particular color, and accordingly a mask is created. This is what we call as a part of matting process in image processing.

So I want to know about the parameter mask in grabcut. Suppose I have an image and I don't want hair in that image. So what will the mask contain for that image. (Here I am not considering rect version).

So if I stroke a part of hair with black color(any color), then how will I create the mask to pass in grabcut function.

Also, in here it says the following :

The mask is initialized by the function when mode is set to GC_INIT_WITH_RECT. Its elements may have one of following values:
GC_BGD defines an obvious background pixels.
GC_FGD defines an obvious foreground (object) pixel.
GC_PR_BGD defines a possible background pixel.
GC_PR_FGD defines a possible foreground pixel.

Which elements are they talking about in here. And the first line saying that mask is initialised when mode is set to GC_INIT_WITH_RECT is correct or should it be GC_INIT_WITH_MASK ?

If Java solution then, that will be best.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-07-09 01:53:18 -0600

berak gravatar image

updated 2017-07-09 01:58:32 -0600

both watershed and grabcut require an "initial guess" . so the process here is:

  1. you mark an initial rectangle(from mouse, or e.g.boundingRect() from a contour), and call grabcut with GC_INIT_WITH_RECT . this will produce an initial mask, where anything inside the choosen rect is set to GC_FGD.

  2. you refine the mask, by marking additional regions as possible fg or bg pixels in that mask. (that means, you have to access the mask's pixels. (which will be painful from java, promised !)

  3. you call grabcut again, with GC_INIT_WITH_MASK, so it can sort out, which of your selected fg/bg pixels to apply

  4. iterate 2. and 3. , until it looks ok. then : isolate all mask pixels with GC_FGD, and put them into a CV_8U Mat. this finally is the mask you can use for "matting" (NOT the grabcut mask)

it may be hard to find, but there are some questions about doing this from java on this site..

edit flag offensive delete link more

Comments

Yeah, so basically point 2 is the pain for me. I found a question actually having the same problem as mine in stackoverflow : https://stackoverflow.com/questions/3... But I guess this guy has followed the same process, so first is he right in his approach? and second, any possible solution for his question?

arqam gravatar imagearqam ( 2017-07-09 02:04:40 -0600 )edit

is at least the difference beween GC_INIT_WITH_RECT and GC_INIT_WITH_MASK clear now ?

berak gravatar imageberak ( 2017-07-09 02:07:26 -0600 )edit
1

@berak, GC_INIT_WITH_RECT we will mostly be using when just the rectangle we give to check the output by grabcut and GC_INIT_WITH_MASK for when we make changes in the mask and that mask is considered in grabcut. Even python code and modified image if its there for second process, it will be helpful for me.

arqam gravatar imagearqam ( 2017-07-09 02:13:27 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-07-09 01:21:46 -0600

Seen: 1,145 times

Last updated: Jul 09 '17