vav
vav

Reputation: 63

OpenCV grabCut only outputs mask's values as background

Where the BLUE color represents user's mask for foreground and the GREEN color represents user's mask for background, I get the below result. Actually only the mask's background marks are outputted as background:

Screenshot http://s7.postimg.org/xwa8z5nez/maradona_problem.png]

The blue marks are saved in the mask as 0, while the green ones are saved as 1.

This is my code. Can you please help?

def run_grabcut():
    global output
    global mask
    bgdmodel = np.zeros((1, 65),np.float64)
    fgdmodel = np.zeros((1, 65),np.float64)
    cv2.grabCut(img, mask, None, bgdmodel, fgdmodel, 1, cv2.GC_INIT_WITH_MASK)
    mask2 = np.where((mask == 0), 255, 0).astype('uint8')
    cv2.bitwise_and(img ,img , output, mask = mask2)

Upvotes: 0

Views: 1228

Answers (1)

tfv
tfv

Reputation: 6259

In the line

mask2 = np.where((mask == 0), 255, 0).astype('uint8')

you are mapping all background pixels (mask ==0) to value 255 and all other types (e.g. foreground; mask ==1) to value 0

Adding

 mask3 = np.where((mask == 1), 127, 0).astype('uint8')

would create a separate mask for foreground. Is this what you are looking for?

Or do you want to also include the potential backgrounds (mask ==2)

 mask2 = np.where((mask==0) + (mask==2),255,0).astype('uint8')

Upvotes: 1

Related Questions