mask :
Input/output 8-bit single-channel mask. 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.

rect : ROI containing a segmented object. The pixels outside of the ROI are marked as obvious background. The parameter is only used when mode==GC_INIT_WITH_RECT .

bgdModel : Temporary array for the background model. Do not modify it while you are processing the same image.

fgdModel : Temporary arrays for the foreground model. Do not modify it while you are processing the same image.

iterCount : Number of iterations the algorithm should make before returning the result. Note that the result can be refined with further calls with mode==GC_INIT_WITH_MASK or mode==GC_EVAL .

mode :
Operation mode that could be one of the following:

GC_INIT_WITH_RECT The function initializes the state and the mask using the provided rectangle. After that it runs iterCount iterations of the algorithm.

GC_INIT_WITH_MASK The function initializes the state using the provided mask.
Note that GC_INIT_WITH_RECT and GC_INIT_WITH_MASK can be combined. Then, all the pixels outside of the ROI are automatically initialized with GC_BGD .

GC_EVAL The value means that the algorithm should just resume.

Grab Cut Code

We create a mask image similar to the loaded image:

mask = np.zeros(img.shape[:2],np.uint8)

Then, we create fgdModel and bgdModel. Then, we run the grabcut algorithm for 5 iterations with cv2.GC_INIT_WITH_RECT mode since we are using rectangle. It modifies the mask image:

cv2.grabCut(img,mask,rect,bgdModel,fgdModel,5,cv2.GC_INIT_WITH_RECT)

In this new mask image, pixels will be marked with four flags denoting background/foreground as specified above. So we modify the mask such that all 0-pixels and 2-pixels are put to 0 (ie background) and all 1-pixels and 3-pixels are put to 1(ie foreground pixels):

mask2 = np.where((mask==2)|(mask==0),0,1).astype('uint8')

Now our final mask is ready, and we can just multiply it with input image to get the segmented image: